Friday, November 18, 2011

Calculate the Sum


Write a C program to calculate the following Sum:Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!*/
#include <stdio.h>
#include <math.h>
void main()
{
int counter,f_coun;
float sum=0,x,power,fact;
clrscr();
printf("<-----------------------PROGRAM FOR SUM OF EQ. SERIES----------------------->");
printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!");
printf("\n\n\n\tENTER VALUE OF X : ");
scanf("%f",&x);
for(counter=0, power=0; power<=10; counter++,power=power+2)
{
fact=1;
//CALC FACTORIAL OF POWER VALUE
for(f_coun=power; f_coun>=1; f_coun--)
        fact *= f_coun;
//EQ. FOR SUM SERIES
sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
}
printf("SUM : %f",sum);
getch();
}

Thursday, September 8, 2011

Calculate Electricity Bill with if-else condition



Calculate Electricity Bill with if-else condition
<=100 Rs.4/units
> 100 and <=300 Rs.4.50/units
>300 and <=500 Rs.4.75/units
>500 Rs.5/units

#include<stdio.h>
#include<conio.h>
void main ()
{
int unit, total;
clrscr ();
printf("Enter Total Units:");
scanf ("%d", &unit);
if (unit<=100)
{
total=unit*4;
}
else if (unit>100 && unit<=300)
{
total=unit*4.5;
}
else if (unit >300 && unit<=500)
{
total=unit*4.75;
}
else
{
total=unit*5;
}
printf("Total: %d", total);
getch ();
}


Method 2:(Coded by: Manpreet Singh Dhindsa, Patiala)

#include<stdio.h>
#include<conio.h>
void main()
{
int units;
float total_bill;
clrscr();
printf("Enter the no. of units");
scanf("%d",&units);
if(units <= 100)
total_bill = units * 4;
else if(units <= 300)
total_bill = units * 4.5;
else if(units <= 500)
total_bill = units * 4.75;
else
total_bill = units * 5;
printf("the bill to be paid is = %f", total_bill);
getch();
}





Question by: Simi Sandhu @ C Programming Facebook Page