C Programs
C Programs
Damodare
Program 1
//Program to check number is strong or not.
/* If sum of Factorials of all the digits of entered
number is equal to entered number then, entered
number is Strong number.*/
#include <stdio.h>
#include <stdlib.h>
int main()
int r,i;
printf("Enter a number\n");
scanf("%ld",&no);
num=no;
while(no!=0)
r=no%10;
for(i=r;i>0;i--)
fact=fact*i;
no=no/10;
sum=sum+fact;
fact=1;
if(sum==num)
else
return 0;
Program 2
//Program to check number is prime or not.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,i,j,m;
scanf("%d",&a);
for(i=2;i<a;i++)
if(a%i==0)
m=1;
break;
for(j=2;j<a;j++)
if(a%i!=0)
m=2;
break;
if(m==1)
else
printf("%d is prime\n",a);
return 0;
Program 3
//Program to separate any 4 digit number into
digits in reverse order.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,b,c,d,e,f;
scanf("%d",&a);
b=a%10;
c=((a-b)%100)/10;
e=a/1000;
f=e*1000;
d=(a-f)/100;
printf("%d\t%d\t%d\t%d\t",b,c,d,e);
return 0;
Program 4
//Program to check number is magic or not.
/*If multiplication of sum of digits of entered
number and reverse of that sum is equal to
entered number then, entered number is Magic
number.*/
#include<stdio.h>
#include<stdlib.h>
int main()
int a,b,c,d,e,f,g,h,i,j,k;
scanf("%d",&a);
b=a%10;
c=((a-b)%100)/10;
e=a/1000;
f=e*1000;
d=(a-f)/100;
printf("%d\t%d\t%d\t%d\n",b,c,d,e);
g=b+c+d+e;
printf("g=%d\n",g);
h=g/10;
i=g%10;
j=h+i*10;
printf("j=%d\n",j);
k=g*j;
printf("k=%d\n",k);
if(k==a)
else
return 0;
Program 5
//Program to reverse the 2 digit entered number.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,b,c,d,rev;
scanf("%d",&a);
b=a/10;
c=a%10;
d=b+c*10;
rev=d;
return 0;
Program 6
//Program to check 2 digit entered number is
palindrome or not.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,i,j,rev;
scanf("%d",&a);
i=a/10;
j=a%10;
rev=i+j*10;
if(a==rev)
else
return 0;
Program 7
//Program to check 3 digit entered number is
palindrome or not.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,i,j,k,rev;
scanf("%d",&a);
i=a%10;
j=(a-i)%100/10;
k=a/100;
rev=k+j*10+i*100;
if(a==rev)
else
return 0;
}
10 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
Program 8
//Program to check 4 digit entered number is
palindrome or not.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,i,j,k,l,rev;
scanf("%d",&a);
i=a%10;
j=(a-i)%100/10;
k=(a-i-j*10)%1000/100;
l=a/1000;
rev=l+k*10+j*100+i*1000;
if(a==rev)
else
return 0;}
11 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
#include <stdlib.h>
int main()
int a,b,c;
scanf("%d",&a);
while(a!=0)
b=a%10;
a=a/10;
c=b;
printf("%d",c);
return 0;
Program 10
//Program to count digits of the entered number.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,b,c,i=0;
scanf("%d",&a);
while(a!=0)
b=a%10;
if(a!=0)
i++;
a=a/10;
return 0;
Program 11
//Program to check entered number is Harshad
number or not.
/*If entered number is divisible by its digits’
sum then, is said to be Harshad number.*/
#include <stdio.h>
#include <stdlib.h>
int main()
int a,b,c,i=0;
scanf("%d",&a);
c=a;
while(a!=0)
b=a%10;
a=a/10;
i=i+b;
if(c%i==0)
else
return 0;
Program 12
//Program to check entered number is
Automorphic number or not.
/*If last digit of entered number and its square is
same then, entered number will be Automorphic
number.*/
#include <stdio.h>
#include <stdlib.h>
int main()
int a,b,c,d;
printf("Enter a number\n");
scanf("%d",&a);
c=a*a;
b=a%10;
d=c%10;
if(b==d)
else
return 0;
Program 13
//Program to check entered number is
Armstrong number or not.
/*If sum of power of each digit in entered
number is equal to entered number then, that
entered number is Armstrong number.*/
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
int a,b,c,d=0,e;
printf("Enter a number\n");
scanf("%d",&a);
b=a%10;
e=a;
while(a!=0)
c=a%10;
if(a!=0)
d=d+pow(c,b);
a=a/10;
printf("%d",d);
if(d==e)
else
return 0;
Program 14
//Program to check entered year is leap or not.
#include<stdio.h>
#include<stdlib.h>
int main()
long int y;
scanf("%ld",&y);
if(y%4==0)
if(y%100!=0)
else if(y%400==0)
else
return 0;
Program 15
//Program to check entered number is perfect
number or not.
/*If sum of factors of entered number is equal to
entered number then, the entered number is
called as Perfect number.*/
#include<stdio.h>
#include<stdlib.h>
void main()
int num,div,i,rem,sum=0,no;
printf("Enter a number\n");
scanf("%d",&num);
no=num;
for(i=2;i<=num;i++)
div=num/i;
rem=num%i;
if(num%i==0)
printf("%d\t",div);
sum=sum+div;
if(sum==no)
else
Program 16
//Program to check entered number is Abundant
number or not.
/*If sum of factors of entered number is greater
than entered number then, entered number will
be an Abundant number.*/
#include<stdio.h>
#include<stdlib.h>
void main()
int num,i,k,h,add=0;
printf("Enter a number\n");
scanf("%d",&num);
for(i=num;i>1;i--)
k=num/i;
h=num%i;
if(h==0)
add=add+k;
if (add>num)
else
Program 17
//Program to print prime numbers in given
range.
//Program to print all prime numbers in given range.
#include<stdio.h>
#include<stdlib.h>
void main()
int str,end,i,j,m;
printf("Enter first\n");
scanf("%d",&str);
scanf("%d",&end);
for(i=str;i<=end;i++)
for(j=2;j<i;j++)
if(i%j==0)
m=1;
break;
else
24 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
m=5;
if(m==5)
printf("%d\t",i);
Program 18
//Program to print Armstrong numbers in given
range.
//Program to print Armstrong numbers in given range.
#include<stdio.h>
#include<stdlib.h>
int main()
int str,end,i,k,last,digit;
scanf("%d",&str);
scanf("%d",&end);
for(k=str;k<=end;k++)
last=k%10;
i=k;
while(i!=0)
digit=i%10;
add=add+pow(digit,last);
i=i/10;
26 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
//printf("\nsum=%ld\t",add);
if(add==k)
printf("%d\t",k);
add=0;
if(1)
printf("\nNot Exist\n");
return 0;
Program 19
//Program to print nth term in arthematic series.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,d,b,n,tn;
scanf("%d\n%d",&a,&b);
d=b-a;
if(d==0)
printf("Something is Wrong\n");
scanf("%d",&n);
tn=a+(n-1)*d;
return 0;
Program 20
//Program to print value of tn in arthematic
series.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,d,b,n,tn;
scanf("%d\n%d",&a,&b);
d=b-a;
if(d==0)
printf("Something is Wrong\n");
scanf("%d",&tn);
n=(tn-a)/d+1;
return 0;
Program 21
//Program to print sum of n terms in arthematic
series.
#include<stdio.h>
#include<stdlib.h>
int main ()
int a,b,Sn,d,n;
scanf("%d\n%d",&a,&b);
d=b-a;
if(d==0)
printf("Something is Wrong");
scanf("%d",&n);
Sn=n*(2*a+(n-1)*d)/2;
printf("Sum = %d",Sn);
return 1;
Program 22
//Program to print nth term in Geometric series.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,r,tn,b,n,p;
scanf("%d",&a);
scanf("%d",&r);
if(r==0)
scanf("%d",&b);
r=b/a;
printf("ratio = %d\n",r);
scanf("%d",&n);
tn=pow(r,n-1)*a;
if(r!=5)
else
tn=tn+1;
return 0;
Program 23
//Program to print sum of n terms in Geometric
series.
#include<stdio.h>
#include<stdlib.h>
int main()
int a,r,Sn,b,n,p;
scanf("%d",&a);
scanf("%d",&r);
if(r==0)
scanf("%d",&b);
r=b/a;
printf("ratio = %d\n",r);
scanf("%d",&n);
Sn=a*(1-pow(r,n))/(1-r);
printf("Sum = %d",Sn);
return 0;}
33 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
Program 24
//Program to covert decimal number to binary
number.
#include<stdio.h>
#include<stdlib.h>
int main()
int de,r,i=1;
scanf("%d",&de);
while(de!=0)
r=de%2;
de=de/2;
bi=bi+r*i;
i=i*10;
return 0;
Program 25
//Program to covert binary number to decimal
number.
#include<stdio.h>
#include<stdlib.h>
int main()
int de=0,i,k=0,r;
scanf("%ld",&bi);
ib=bi;
while(ib!=0)
ib=ib/10;
k++;
printf("k=%d\n",k);
for(i=0;i<=k;i++)
r=bi%10;
p=pow(2,i);
de=de+r*p;
bi=bi/10;
35 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
return 0;
Program 26
//Program to covert decimal number to octal
number.
#include<stdio.h>
#include<stdlib.h>
int main()
int de,i=1,r;
scanf("%d",&de);
while(de!=0)
r=de%8;
oct=oct+r*i;
i=i*10;
de=de/8;
return 5;
Program 27
//Program to covert octal number to decimal
number.
#include<stdio.h>
#include<stdlib.h>
int main()
int de=0,k=0,r,i,p;
scanf("%d",&oct);
octa=oct;
while(octa!=0)
octa=octa/10;
k++;
for(i=0;i<=k;i++)
r=oct%10;
p=pow(8,i);
de=de+r*p;
oct=oct/10;
}
38 SKN Sinhgad College of Engineering, Pandharpur
Programs by Shubhankar C. Damodare
return 4;
Program 28
//Swapping of 2 values without using 3rd
variable.
#include <stdio.h>
#include <stdlib.h>
int main()
int a,b;
scanf("%d\n%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d\tb=%d",a,b);
return 0;
Program 29
//* Pattern.
#include <stdio.h>
#include <stdlib.h>
int main()
int i,j;
for(j=1;j<6;j++)
for(i=1;i<6;i++)
printf("*");
printf("\n");
return 0;
Program 30
//* Pattern.
#include <stdio.h>
#include <stdlib.h>
int main()
int i,j;
for(j=1;j<6;j++)
for(i=0;i<j;i++)
printf("*");
printf("\n");
return 0;
Program 31
//Number Pattern.
#include <stdio.h>
#include <stdlib.h>
int main()
int i,j;
for(i=2;i<11;i++)
for(j=1;j<i;j++)
printf("%d",j);
printf("\n");
return 0;