0% found this document useful (0 votes)
13 views5 pages

Chapter 5 Programs

The document contains multiple C programs that perform various mathematical operations, including checking if a number is prime, calculating the sum of digits, reversing an integer, checking for palindromes, identifying Armstrong numbers, converting between decimal and binary, and evaluating mathematical series. Each program prompts the user for input and outputs the result of the calculation. The programs utilize standard input/output functions and basic control structures in C.

Uploaded by

siddiquieayan786
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)
13 views5 pages

Chapter 5 Programs

The document contains multiple C programs that perform various mathematical operations, including checking if a number is prime, calculating the sum of digits, reversing an integer, checking for palindromes, identifying Armstrong numbers, converting between decimal and binary, and evaluating mathematical series. Each program prompts the user for input and outputs the result of the calculation. The programs utilize standard input/output functions and basic control structures in C.

Uploaded by

siddiquieayan786
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

//Program to check whether any integer is prime or not

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,c=0;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++; // count of factors
}
if(c==2)
printf("%d is prime",n);
else
printf("%d is not prime",n);
getch();
}
//Program to display the sum of digits present in any integer
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
printf("Enter n:");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum of digits=%d",sum);
getch();
}
//Program to display the reverse of any integer
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
printf("Enter n:");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse=%d",sum);
getch();
}
//Program to check palindrome number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,x;
printf("Enter n:");
scanf("%d",&n);
x=n;
while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(sum==x)
printf("Palindrome");
else
printf("Not Palindrome");

getch();
}
//Program to check armstrong number 153=1^3+5^3+3^3=153
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,r,sum=0,x;
printf("Enter n:");
scanf("%d",&n);
x=n;
while(n!=0)
{
r=n%10;
sum=sum+pow(r,3);
n=n/10;
}
if(sum==x)
printf("Armstrong");
else
printf("Not Armstrong");

getch();
}

//Program to convert decimal number into binary form

#include<stdio.h>
void main()
{
int n,i,rem,sum=0;
printf("Enter a decimal number:");
scanf("%d",&n);
i=0;
while(n!=0)
{
rem=n%2;
sum=sum+rem*pow(10,i);
i++;
n=n/2;
}
printf("Equivalent Binary=%d",sum);
}
//Program to convert binary number into decimal
#include<stdio.h>
void main()
{
int n,i,rem,sum=0;
printf("Enter a number in binary form:");
scanf("%d",&n);
i=0;
while(n!=0)
{
rem=n%10;
sum=sum+rem*pow(2,i);
i++;
n=n/10;
}
printf("Equivalent Decimal=%d",sum);
}

//Evaluate S=1/1^2 + 1/2^2 + 1/3^2 + .....upto nth term[General term= 1/i^2 where
i=1,2,3...n]
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
float term,sum=0;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
term= 1/pow(i,2);
printf("%f\t",term);
sum=sum+term;
}
printf("\nSum=%f",sum);
}

//Evaluate S=x^1/1^2 + x^2/2^2 + x^3/3^2 + .....upto nth term[General term= x^i/i^2


where i=1,2,3...n]
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
float x,term,sum=0;
printf("Enter n and x:");
scanf("%d%f",&n,&x);
for(i=1;i<=n;i++)
{
term= pow(x,i)/pow(i,2);
printf("%f\t",term);
sum=sum+term;
}
printf("\nSum=%f",sum);
}

//Evaluate sine series: S=x - x^3/3! + x^5/5! - x^7/7! +.....upto nth term
[General term= (-1)^(i+1)*x^(2i-1)/(2i-1)! where i=1,2,3...n]
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
float x,term,sum=0,fact;
printf("Enter n and x:"); //x in radian
scanf("%d%f",&n,&x);
for(i=1;i<=n;i++)
{
fact=1;
for(j=1;j<=(2*i-1);j++)
{
fact=fact*j;
}
term=pow(-1,i+1)*pow(x,2*i-1)/fact;
//printf("%f\t",term);
sum=sum+term;
}
printf("\nSum=%f",sum);
}

You might also like