Some Programs in C

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

Certification in Full Stack Web Development

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

Our Data Science Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 11th Jan 2025
₹65,037
Cohort starts on 18th Jan 2025
₹65,037
Cohort starts on 11th Jan 2025
₹65,037

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.