C Programming Examples with Output
1. Write a program to check whether the given number is prime or not
Prime Number – Prime numbers are those numbers which has no positive divisor other than 1 and itself.
Example:
#include<stdio.h>
int main()
{
int i,n,p=0;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
p=1;
break;
}
}
if(p==0)
{
printf("Number is prime");
}
else
{
printf("Number is not prime");
}
return 0;
}
Output:
enter the number 17
Number is prime
2. Write a program to calculate the Factorial.
Factorial – It is the product of all positive integers less than or equal to n. It is denoted by n!.
Example:
#include<stdio.h>
int main()
{
int i,n,f=1;
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial of number is=%d",f);
}
Output:
enter the number 5
Factorial of number is=120
3. Write a program to check whether the given number is palindrome or not.
Palindrome Number – Palindrome numbers are those numbers which reads the same backward or forward.
Example:
#include<stdio.h>
int main()
{
int r,n,p=0,temp;
printf("enter the number");
scanf("%d",&n);
temp=n;
while(n!=0)
{
r=n%10;
p=p*10+r;
n=n/10;
}
if(p==temp)
{
printf("Number is palindrome");
}
else
{
printf("Number is not palindrome");
}
return 0;
}
Output:
enter the number 111
number is palindrome
4. Write a program to check whether the number is Armstrong or not.
Armstrong Number – Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.
Example:
#include<stdio.h>
int main()
{
int n,r,p=0,temp;
printf("enter the number ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
p=p+(r*r*r);
n=n/10;
}
if(temp==p)
{
printf("Number is Armstrong");
}
else
{
printf("Number is not Armstrong");
}
return 0;
}
Output:
enter the number 153
Number is Armstrong
5. Write a program to display the Fibonacci Series.
Fibonacci Series – A series in which next number is occurred by adding two previous numbers.
Example:
#include<stdio.h>
int main()
{
int i=0,j=1,k,p,n;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n%d %d",i,j);
for(p=2;p<n;++p)//loop starts from 2 because 0 and 1 are already printed
{
k=i+j;
printf(" %d",k);
i=j;
j=k;
}
return 0;
}
Output:
Enter the number of elements:12
0 1 1 2 3 5 8 13 21 34 55 89