0% found this document useful (0 votes)
6 views8 pages

C Programs for Math Calculations

The document contains a series of C programming tasks that include computing the factorial of a positive integer, calculating the sum of squares of natural numbers, finding the area and volume of a sphere, displaying the Fibonacci series, and evaluating sine and cosine functions using power series. Additionally, it includes programs to sort arrays in ascending and descending order, separate odd and even integers, find the largest and smallest numbers in a set, and calculate the value of π. Each task is accompanied by sample code for implementation.

Uploaded by

jitjkkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

C Programs for Math Calculations

The document contains a series of C programming tasks that include computing the factorial of a positive integer, calculating the sum of squares of natural numbers, finding the area and volume of a sphere, displaying the Fibonacci series, and evaluating sine and cosine functions using power series. Additionally, it includes programs to sort arrays in ascending and descending order, separate odd and even integers, find the largest and smallest numbers in a set, and calculate the value of π. Each task is accompanied by sample code for implementation.

Uploaded by

jitjkkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write and execute a program in C to compute the factorial of a Positive


integer including Zero

INPUT
#include<stdio.h>
void main()
{
int f=1,n,a;
Printf("\n enter number");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
f=f*a;
}
printf("factorial=%d",f);
}

2. Write and execute a program in C to calculate sum


of squares of n natural numbers

INPUT
#include<stdio.h>
void main()
{
int i,n,sum=0;
printf("\n enter n= ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i*i;
}
printf("%d\n",sum);
}
3. Write and execute a program in C to find the area and the volume of a
Sphere by varying the radius.

INPUT
#include<stdio.h>
int main()
{
float r,area,volume,pi=3.14;
printf("enter the radius of sphere:");
scanf("%f,&r);
area=4*pi*r*r;
volume=4*pi*r*r*r/3;
printf("the surface area of sphere is %f and volume is
%f",area,volume);
return 0;
}

4. Write and execute a program in C to display


Fibonacci series.

INPUT

#include<stdio.h>
void main()
{
int first=0,second=1,count=0,fibo,n;
printf("enter range:");
scanf("%d",&n);
while(count<n)
{
if(count<=1)
{
fibo=count;
}
else
{
fibo=first+second;
first=second;
second=fibo;
}
printf("%d",fibo);
count++;
}
}
5. Write and execute a program in C to find the value of Sine function
using power series

Input

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,temp=1;
float x,deg,nr,dr,term,s=0;
clrscr();
printf("\n\n\n enter the value x in degree");
scanf("%f",&deg);
print("enter the value for n:");
scanf("%d",&n);
x=deg*3.14159/180;
nr=x;
dr=1;
s=x;
for(i=2;i<=n;i++)
{
nr=nr*x*x;
dr=dr*(i*2-2)*(i*2-1);
temp=temp*(-1);
temp=nr/dr*temp;
s=s+term;
}
printf("\nsin(%f)=%f",deg,s);
getch();
}

6. Write and execute a program in C to find the value of


Cosine function using power series

Input
#include<stdio.h>
#define PI 3.1415
void main()
{
printf("\nEnter angle in degree ");
float angle_degree;
scanf("%f",&angle_degree);
float angle_radian=angle_degree*PI/180;
float ans=1;
float temp=1;
int acc=100;
int i;
for(i=1;i<=2*acc;i+=2)
{
temp=temp*(-1)*angle_radian*angle_radian/(i*(i+1));
ans=ans+temp;
}
printf("cos(%f)=%f\n",angle_degree,ans);
}

7. Write and execute a program in C to find the value of ex

Input
#include<stdio.h>
#include<conio.h>
void main()
{
float power;
printf("\nenter the power of e ");
scanf("%f",&power);
int acc=100;
float ans=1;
float temp=1;
int i;
for(i=1;i<=acc;i++)
{
temp=(temp*power)/i;
ans=ans+temp;
}
printf("%f",ans);
getch();
}

8. Write and execute a program in C to sort elements of an array of elements


in ascending order.

INPUT
#include<stdio.h>
#include<conio.h>
void main()
{
int data[100],i,n,step,temp;
clrscr();
printf("enter the number of elements to be sorted");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d,enter element",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
{
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1])
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}

}}
printf("in asending order");
for(i=0;i<n;++i)
printf("%d ",data[i]);
getch();
}

9. Write and execute a program in C to sort elements of an array of elements


in ascending order.

INPUT

#include<stdio.h>
#include<conio.h>
void main()
{
int data[100],i,n,step,temp;
clrscr();
printf("enter the number of elements to be sorted");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d,enter element",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
{
for(i=0;i<n-step-1;++i)
{
if(data[i]<data[i+1])
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}

}}
printf("in descending order");
for(i=0;i<n;++i)
printf("%d ",data[i]);
getch();
}

10. Write and execute a program in C to separate odd and even integers in
arrays.

Input
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],i,num;
clrscr();
printf("\nenter the size of an array\n");
scanf("%d",&num);
printf("enter the elements of the array\n");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
printf("even numbers in the array are:");
for(i=0;i<num;i++)
{
if(array[i]%2==0)
{
printf("%d\t",array[i]);
}
}
printf("\n odd numbers in the array are:");
for(i=0;i<num;i++)
{
if(array[i]%2!=0)
{
printf("%d\t",array[i]);
}
}
getch();
}

11. Write and execute a program in C to find the largest and smallest in a
given set of numbers

Input
#include<stdio.h>
#include<conio.h>
void main()
{
float a[50],large,small;
int n,i;
printf("\n How many numbers:\n");
scanf("%d",&n);
printf("Give %d numbers:\n",n);
for(i=0;i<n;++i)
scanf("%f",&a[i]);
large=a[0];
small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("\n largest number=%2f",large);
printf("\n smallrst number=%2f",small);
getch();
}

12. Write and execute a program in C to calculate value of π.

Input
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
double p=0;
clrscr();
for(i=0;i<=100;i++)
{
if(i%2==0)
{
p=p+(4.0/(2*i+1));
}
else
{
p=p-(4.0/(2*i+1));
}
}
printf("%f",p);
getch();
}

You might also like