Friday, November 21, 2014

C equation pro



Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers


#include
#include
void main()
{
int numarray[10];
int i,sum=0;
int *ptr;
printf("\n Enter 10 elements: ");
for(i=0;i<10 i="" span="">
scanf("%d",&numarray[i]);
ptr=numarray;
for(i=0;i<10 i="" span="">
{
sum=sum+*ptr;
ptr++;
}
printf("The sum of array elements :%d",sum);
return(0);
}

Output

Enter 10 elements : 11   12      13      14      15      16      17      18      19      20
The sum of array elements is 155

C Program for Exponent Series


#include
#include
void main()
{
int n,count;
float x, term,sum;
printf("\n Enter value of x: ");
scanf("%f",&x);
n=term=sum=count=1;
while(n<=100)
{
term=term*x/n;
sum=sum+term;
count=count+1;
if(term
n=999;
else
n=n+1;
}
printf("\n Terms =%d sum = %f", count,sum);
return 0;
}

Output

Enter value of x : 0
Terms =2 sum=1.000000
Enter value of x : 0.1
Terms =5 sum=1.105171
Enter value of x : 0.5
Terms =7 sum=1.648720
Enter value of x : 0.75
Terms =8 sum=2.116997
Enter value of x : 0.99
Terms =9 sum=2.691232
Enter value of x : 1
Terms =9 sum=2.718279

Program to convert temperature from degree centigrade to Fahrenheit


#include
#include
int main()
 {
    float celsius, fahrenheit;
    printf("\nEnter temp in Celsius : ");
    scanf("%f", &celsius);
    fahrenheit = (1.8 * celsius) + 32;
    printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
     return (0);
}

Output

Enter temp in Celsius : 32
Temperature in Fahrenheit : 89.59998

C Program to find the simple interest


#include
#include 
int main()
 {
   int amount, rate, time, si;
   printf("\nEnter Principal Amount : ");
   scanf("%d", &amount);
   printf("\nEnter Rate of Interest : ");
   scanf("%d", &rate);
   printf("\nEnter Period of Time   : ");
   scanf("%d", &time);
   si = (amount * rate * time) / 100;
   printf("\nSimple Interest : %d", si);
   return(0);
}

Output

Enter Principal Amount : 500
Enter Rate of interest : 5
Enter Period of Time   : 2
Simple Interest : 50

C Program to find sum of two numbers


#include
#include 
int main()
 {
   int a, b, sum;
   printf("\nEnter two no: ");
   scanf("%d %d", &a, &b);
   sum = a + b;
   printf("Sum : %d", sum);
   return(0);
}

Output

Enter two no: 5 6
Sum : 11

To obtain solution of second order quadratic equation

#include
#include
#include
int main()
{
   float a, b, c;
   float desc, root1, root2;
   printf("\nEnter the Values of a : ");
   scanf("%f", &a);
   printf("\nEnter the Values of b : ");
   scanf("%f", &b);
   printf("\nEnter the Values of c : ");
   scanf("%f", &c);
   desc = sqrt(b * b - 4 * a * c);
   root1 = (-b + desc) / (2.0 * a);
   root2 = (-b - desc) / (2.0 * a);
   printf("\nFirst Root : %f", root1);
   printf("\nSecond Root : %f", root2);
   return (0);
}

Output

Enter the Values of a : 1
Enter the Values of a : -5
Enter the Values of a : 6
First Root : 3.000000
Second Root : 2.000000

Find Factorial of Number Using Recursion

#include
#include
int fact(int);
int main()
{
   int factorial, num;
   printf("Enter the value of num :");
   scanf("%d", &num);
   factorial = fact(num);
   printf("Factorial is %d", factorial);

   return (0);
}
int fact(int n)
{
   if (n == 0)
{
      return (1);
   }
   return (n * fact(n - 1));
}

No comments:

Post a Comment