Name:-Sastik Kumar Das: Pps Lab - 5
Name:-Sastik Kumar Das: Pps Lab - 5
Name:-Sastik Kumar Das: Pps Lab - 5
= #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:-
= #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:-
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:
= #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:
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:-
= #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:-
= #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:-