You are on page 1of 17

PPS LAB – 5

Name:- Aparannha Roy


Roll:- CSE/20/48

1) Write a C program to check whether the 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;
}
Output:-

2) Write a C program to find the G.C.D of two given integers.

= #include<stdio.h>
int main()
{
int num1,num2,gcd;
printf("Enter the two integer numbers\n");
scanf("%d %d",&num1,&num2);
if(num1==0)
{
printf("gcd=%d",num2);
}
while(num2!=0)
{
if(num1>num2)
{
num1=num1-num2;
}
else
{
num2=num2-num1;
}
}
gcd=num1;
printf("gcd=%d",gcd);
return 0;
}

Output:-

3) Write a program to check whether the 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);
}
}
Output:-

4) Write a C program to check whether the 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;
}
Output:

5) Write a program to display all the prime, Peterson and


Armstrong numbers in between 2 to 500 using Switch case.

= #include <stdio.h>
#include <math.h>
int factorial(int x)
{
if (x == 0 || x == 1)
{
return 1;
}
else
{
return x * factorial(x - 1);
}
return 0;
}
int main()
{
int a, b, c, r, num, remainder, ans = 0;
int opr;
long r1;
int num1, count = 1, rem, sum;
printf("Enter the operation: ");
scanf("%d", &opr);
if (opr <= 3)
{
switch (opr)
{
case 1:
printf("Enter the range: ");
scanf("%d", &r);
a = 2;
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++;
}
break;
case 2:

printf("Enter the range: ");


scanf("%ld", &r1);
a = 2;
while (a <= r1)
{
int temp = a;
int sum = 0;
int cd;
while (temp != 0)
{
cd = temp % 10;
sum += factorial(cd);
temp /= 10;
}
if (sum == a)
printf("%d is a peterson number\n", a);
else
printf("%d is not a peterson number\n", a);
a++;
}
break;
case 3:

while (count <= 500)


{
num1 = count;
sum = 0;

while (num1)
{
rem = num1 % 10;
sum = sum + (rem * rem * rem);
num1 = num1 / 10;
}

if (count == sum)
{
printf("%d is a Armstrong number\n", count);
}

count++;
}
return 0;
}
}
}

Output :-
6) Write a Menu Driven program using Switch case. Menu list
containing 5 cases.
Cases are addition, subtraction, division, multiplication and exit.
= #include<stdio.h>
int main()
{
float a1,a2,result;
int n;
int opr;
printf("welcome!!\n");
printf("Enter first no.: and \nEnter sec no.\n");
scanf("%f %f",&a1,&a2);
printf("For addition press '1'\nFor substraction press'2'\nFor division
press'3'\nFor multiplication press'4'\n For exit press'5'");
scanf("%d",&opr);
switch(opr)
{
case 1 :
result=a1+a2;
break;
case 2 :
result=a1-a2;
break;
case 3 :result=a1/a2;
break;
case 4 :
result=a1*a2;
break;
case 5 :
printf("Exit--");
break;
default:
{
printf("You have choosen wrong button");
}
}
printf("\n Result is:%f\n",result);
return 0;
}

Output:-

7) Write a program to calculate the sum of all positive integer numbers


among 10 integer numbers using continue control statement.
= #include<stdio.h>
int main(){
int n[10], sum = 0;
printf("Enter 10 integers: ");
for(int i =0; i< 10 ;i++){
scanf("%d", &n[i]);
if(n[i] > 0){
sum += n[i];
}
else{
continue;
}
}
printf("The sum of the positive integer is: %d",sum);

return 0;
}
Output:-

8) Write a program to display the factorial value of the given


number.

= #include<stdio.h>
int main()
{
int a=1,number,i;
printf("Enter the number :\t");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
a=a*i;

printf(" %d *",i);
}
printf("\nThe factorial of %d is %d",number,a);
return 0;
}

Output:-

9) Write a program to generate Fibonacci series up to N terms .

= #include<stdio.h>
#include<stdlib.h>
int fibonnaci();
int main()
{
fibonnaci();
return 0;
}
int fibonnaci()
{
int i=0,f1,f2,n,z;
printf("Enter the series : ");
scanf("%d",&n);
f1=0;
f2=1;
fflush(stdin);
printf("%d",f1);
printf("\n%d\n",f2);
while(i<(n-2))
{
printf("%d\n",z);
z=f1+f2;
f1=f2;
f2=z;
i++;
}
}

Output:-
10) Write a program to display all the quadratic roots of the
equation.

= #include<stdio.h>
int main()
{
int a,b,c;
float y;
printf("Enter the value of a , b , c :-\n");
scanf("%d %d %d",&a,&b,&c);
printf("The quadratic equation is\n %dx^2+%dx+%d",a,b,c);
y=(b*b)-(4*a*c);
if(y==0)
{
printf("\nThe roots of this equation is real and equal %f",y);
}
else if(y>0)
{
printf("\nThe roots of this equation is real and unequal %f",y);
}
else if(y<0)
{
printf("\nThe roots of this equation is imaginary %f",y);
}
return 0;
}

Output:-
11) Write a program to check whether a given number (at least 3 digits)
is palindrome or not. Also display the reverse of the given number.

= #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;
}

Output:-

You might also like