0% found this document useful (0 votes)
78 views

Assignment 3.... Neha Kumari (CSE-20-48)

The document contains 7 programming assignments: 1) A program to find the factorial of a given number. 2) A program to find GCD and LCM of two integers. 3) A program to check if a number is prime. 4) A program to check if a number is a palindrome. 5) A program to display all prime numbers in a range. 6) A program to check if a number is a Peterson number. 7) A program to check if a number is an Armstrong number. 8) Programs to calculate the sum of 4 different series with N terms.

Uploaded by

Aparannha Roy
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)
78 views

Assignment 3.... Neha Kumari (CSE-20-48)

The document contains 7 programming assignments: 1) A program to find the factorial of a given number. 2) A program to find GCD and LCM of two integers. 3) A program to check if a number is prime. 4) A program to check if a number is a palindrome. 5) A program to display all prime numbers in a range. 6) A program to check if a number is a Peterson number. 7) A program to check if a number is an Armstrong number. 8) Programs to calculate the sum of 4 different series with N terms.

Uploaded by

Aparannha Roy
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
You are on page 1/ 9

Assignment-3

Name:- Neha Kumari


Roll:- CSE/20/125

1. Wite a program to find the factorial of a given number.


= #include<stdio.h>

int main()

int a,b,c=0;

printf("Enter the number :");

scanf("%d",&b);

while(b>0)
{

a=b%10;

c=c+a;

b=b/10;

printf("Sum of the total number %d",c);

return 0;

2. Write a C program to find G.C.D and L.C.M of any two integers.


= #include<stdio.h>

int main()

int num1,num2,gcd,lcm,i;
printf("Enter the first number");

scanf("%d",& num1);
printf("Enter the second number");
scanf("%d",&num2);

for(i=1;i<num1 && i<=num2;++i)

if(num1%i==0 && i<=num2%i==0)

{
gcd=i;

lcm= (num1*num2)/gcd;

printf("GCD=%d and LCM=%d",gcd,lcm);

return 0;

3. Write a C program to check whether a given number is prime or


not.
= #include<stdio.h>

int main()

int n,i,m=0,flag=0;

printf("Enter the number to check prime:");

scanf("%d",&n);

m=n/2;

for(i=2;i<=m;i++)

if(n%i==0)

printf("Number is not prime");

flag=1;

break;

if(flag==0)
printf("Number is prime");

return 0;

4. Write a C program to check whether a given number is


palindrome or not.
= #include<stdio.h>

int main()

int s=0,x,n,r;

printf("Enter the number :");

scanf("%d",&n);

x=n;
while(n>0)

r=n%10;

s=s*10+r;

n=n/10;

printf("%d\n",s);

if(s==x)

printf("The number is pallindrome");

else

printf("The number is not pallindrome");

return 0;

}
5. Write a C program to display all prime numbers within the
range.
=
#include<stdio.h>

int main(){

int r,a,b,c = 0;

printf("Enter the range: ");

scanf("%d",&r);

a = 1;

while(a <=r){

b= 1; c=0;

while(b<= a){

if(a%b == 0)
c = c+ 1;

b++;

if(c==2)

printf("%d is a prime number\n",a);

a++;

return 0;

6. Write a C program to check whether the given number is


Peterson or not.
= #include<stdio.h>

int factorial(int x);

int main(){

int num,num1,remainder,ans = 0;

printf("Enter the check number :");


scanf("%d",&num);

num1 = num;

while(num >0){
remainder = num % 10;

ans += factorial(remainder);

num /= 10;

if(num1 == ans){
printf("The number is a peterson number");

else{

printf("The number is not peterson number");

return 0;

int factorial(int x){

if(x == 0 || x == 1){

return 1;

else{

return x * factorial(x-1);

7. Write a C program to check whether the given number is


Armstrong or not.
= #include<stdio.h>

#include<math.h>

int main()

int s=0,x,n,r;

printf("Enter the number :");

scanf("%d",&n);

x=n;

while(n>0)
{

r=n%10;

s=s+pow(r,3);

n=n/10;

}
printf("%d\n",s);

if(s==x)

printf("The number is armstrong");

else

printf("The number is not armstrong");

return 0;

8. Write a C program to calculate the sum of the following series:


i) S= 1 × 2^3+ 2 × 3^3+ 3 × 4^3+ ……N terms
ii) S= x – 𝑥^3/ 3! + 𝑥^5/ 5! - ……………………..N terms
iii) S = 1 - 2 + 3 - 4 + 5 - …………N terms.
iv) S= 1/2 + 3/4 + 5/6 + 7/8 + ……N terms
= i) #include<stdio.h>

#include<math.h>

int main()

{ int i,j,n,sum=8;
printf("Enter the value of 'N' of series:- ");

scanf("%d",&n);

printf("1x2^3");

for(i=2;i<=n;i++)

{ sum=sum+(i*pow((i+1),3));
printf("+%dx%d^3",i,i+1);

printf(" = %d",sum);

return 0;

ii) #include<stdio.h>

#include<math.h>

int fact(int);

int main()

int i,x,j=3,n,sum=0;

printf("Enter the value of 'N' of series:- ");

scanf("%d",&n);

printf("\nEnter the Value of 'X' of series:-");

scanf("%d",&x);

printf("%d",x);

sum=x;

for(i=2;i<=n;i++)

{ if(i%2==0)

{ sum=sum-pow(x,j)/fact(j);
printf("-(%d^%d)/%d!",x,j,j);

else

{ sum=sum+pow(x,j)/fact(j);

printf("+(%d^%d)/%d!",x,j,j);

}
j+=2;

printf(" = %d",sum);

return 0;

}
int fact(int a)

{ int i,f;

for(i=1;i<=a;i++)

f=f*i;

return f;

iii) #include<stdio.h>

int main()

{ int i,n,sum=0;

printf("Enter the value of 'N' of series:- ");

scanf("%d",&n);

printf("1");

sum=1;

for(i=2;i<=n;i++)

{ if(i%2==0)

{ printf("-%d",i);

sum=sum-i;
}

else

{ printf("+%d",i);

sum=sum+i;

}
printf(" = %d",sum);

return 0;

}
iv) #include<stdio.h>

int main()

{ int i,a=1,b=2,n;

float sum=0;

printf("Enter the value of 'N' of series:- ");


scanf("%d",&n);

printf("(%d/%d)",a,b);

sum=1/2;

for(i=2;i<=n;i++)

{ a+=2;

b+=2;

sum=sum+((float)a/(float)b);

printf("+(%d/%d)",a,b);

printf(" = %.3f",sum);

return 0;

You might also like