## 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

If you want to know about Decision MakingÂ in C, refer to our C programs blog!

Recommended Videos

Course Schedule

Name Date
Python Course 2021-04-24 2021-04-25
(Sat-Sun) Weekend batch
View Details
Python Course 2021-05-01 2021-05-02
(Sat-Sun) Weekend batch
View Details
Python Course 2021-05-08 2021-05-09
(Sat-Sun) Weekend batch
View Details

Browse Categories