Name -Vikram Mishra
Roll no.-2K20/B7/46
Branch-Computer Engineering
C Program to print “Hello world”
#include<stdio.h>
int main()
printf("Hello world");
return 0;
Output:
C Program to print Sum and Average of two Numbers
#include<stdio.h>
int main()
float a,b;
float sum;
float avg;
printf("Enter two numbers \t");
scanf("%f%f",&a,&b);
sum = a+b;
printf("The sum of numbers is %f\n",sum);
avg = sum/2;
printf("The average of numbers is %f",avg);
return 0;
Output:
C Program to find Simple Interest
#include<stdio.h>
int main()
float principle;
float time;
float rate;
float interest;
printf("Enter value of principle:\t");
scanf("%f",&principle);
printf("Enter value of time in years:\t");
scanf("%f",&time);
printf("Enter rate of interest per annum:\t");
scanf("%f",&rate);
interest= (principle * time * rate)/100;
printf("The value of simple interest is %f",interest);
return 0;
}
Output:
C Program to check if number is positive or not
#include<stdio.h>
int main()
int n;
printf("Enter integer");
scanf("%d",&n);
if(n % 2==0)
printf("%d is even",n);
else
printf("%d is odd",n);
return 0;
Output:
C Program to check if given number is positive or negative
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
if(num != 0)
if(num > 0)
printf("The given number is positive");
else
printf("The number is negative");
return 0;
}
Output:
C program to find greatest of three numbers
#include<stdio.h>
int main()
int a,b,c;
printf("Enter numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
printf("The greatest number is %d",a);
else
printf("The greatest number is %d",c);
else
if (b>c)
printf("The greatest number is %d",b);
else
printf("The greatest number is %d",c);
return 0;
Output:
C Program to count from 1 to n
#include<stdio.h>
int main()
int n;
printf("Enter number\t");
scanf("%d",&n);
int count = 1;
while (count<=n)
printf("%d\n",count);
count = count + 1;
return 0;
}
Output:
C Program to find sum of first n Natural numbers
#include<stdio.h>
int main()
int n;
printf("Enter number\t");
scanf("%d",&n);
int a=1;
int sum=0;
while(a<=n)
sum= sum+a;
a = a+1;
printf("The sum of N natural number is %d",sum);
return 0;
}
Output:
C Program to find sum of all even natural numbers from 1 to n
#include<stdio.h>
int main()
int n;
printf("Enter number\t");
scanf("%d",&n);
int sum = 0;
int a = 2;
while(a<=n)
printf("%d\n",a);
sum = sum + a;
a = a+2;
printf("The sum of Even numbers is %d",sum);
return 0;
}
Output:
C Program to find the factorial of a given number
#include<stdio.h>
int main()
int num;
printf("Enter any number\t");
scanf("%d",&num);
int temp = num;
int ans = 1;
while(num != 0)
ans = ans * num;
num = num - 1;
printf("The factorial of a number %d is %d",temp, ans);
return 0;
}
Output:
C Program to check whether a number is prime or not
#include<stdio.h>
int main()
int n;
printf("Enter number\t");
scanf("%d",&n);
int div = 1;
int fact = 0;
while(div<=n)
if(n % div==0)
{ fact = fact + 1;
div = div + 1;
if(fact == 2)
{
printf("The number is prime");
else
printf("The number is not prime");
return 0;
Output:
C Program to calculate the sum of the digits of a number
#include <stdio.h>
int main()
int num;
printf("Enter number\t");
scanf("%d",&num);
int temp=num;
int sum=0;
while(num!=0)
{
int rem= num % 10;
sum = sum + rem;
num = num/10;
printf("The sum of digits of %d is %d",temp,sum);
return 0;
Output:
C Program to find the Reverse of a number
#include <stdio.h>
int main()
int n;
printf("Enter number\n");
scanf("%d",&n);
int temp = n;
int ans=0;
while(n != 0)
{
int rem = n % 10;
ans = ans*10 + rem;
n=n/10;
printf("The Reverse of %d is %d",temp,ans);
return 0;
Output:
C Program to count number of digits in a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
int temp = num;
int nod = 0;
while(num != 0)
{
nod = nod + 1;
num = num/10;
printf("Number of Digits in %d is %d",temp, nod);
return 0;
Output:
C Program to print Fibonacci series upto nth Fibonacci number
#include <stdio.h>
int main()
int num;
printf("Enter any number\n");
scanf("%d",&num);
int a=0;
int b=1;
int count = 1;
while(count <= num+1)
printf("%d\t",a);
int sum =a + b;
a=b;
b=sum;
count = count + 1;
return 0;
Output:
C Program to find HCF of two numbers
#include<stdio.h>
int main()
int a,b;
printf("Enter two numbers\t");
scanf("%d%d",&a,&b);
int min;
if(a>b)
min=b;
else
min=a;
int count = 1;
int ans;
while(count<=min)
if(a % count==0 && b % count==0)
ans = count;
count = count + 1;
printf("The HCF of %d and %d is %d",a,b,ans);
return 0;
Output:
C Program to find LCM of two numbers
#include <stdio.h>
int main()
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
int ans = 0;
int max = 0;
if (a>b)
max = a;
else
max = b;
while(max <= a*b)
if(max % a ==0 && max % b ==0)
ans = max;
break;
max = max + 1;
printf("The LCM of %d and % d is %d",a,b,ans);
return 0;
}
Output:
C Program to find Power of a number
#include <stdio.h>
int main()
int base, exponent;
printf("Enter Base\n");
scanf("%d",&base);
printf("Enter exponent\n");
scanf("%d",&exponent);
int res = 1;
int count = 1;
while(count <= exponent)
res = res*base;
count = count + 1;
printf("Your ans is %d",res);
return 0;
Output:
C Program to Rotate a number given no. of times
#include <stdio.h>
int main()
int num;
int rot;
printf("Enter number\n");
scanf("%d",&num);
printf("Enter rot\n");
scanf("%d",&rot);
int temp = num;
int nod = 0;
while(temp!=0)
{
nod = nod + 1;
temp /= 10;
int mul = 1;
int count = 1;
while(count <= nod - 1)
mul *= 10;
count += 1;
int r = 1;
while(r <= rot)
int rem = num % 10;
int rest = num / 10;
int ans = rem * mul + rest;
printf("The rotation of a number is %d\n", ans);
num = ans;
r=r+1;
return 0;
}
Output:
C Program to convert a number from decimal to binary
#include<stdio.h>
int main()
int decimal;
printf("Enter any Integer?\t");
scanf("%d",&decimal);
int flag = decimal;
int ans = 0;
int mul = 1;
while(decimal != 0)
int rem = decimal % 2;
ans = ans + rem * mul;
decimal = decimal/2;
mul = mul * 10;
printf("The binary of an integer %d is %d",flag,ans);
return 0;
Output:
C Program to convert binary to decimal
#include<stdio.h>
int main()
int binary;
printf("Enter any binary digits?\t");
scanf("%d",&binary);
int flag = binary;
int ans = 0;
int mul = 1;
while(binary != 0)
int rem = binary % 10;
ans = ans + rem * mul;
binary = binary/10;
mul = mul * 2;
}
printf("The decimal of an binary digits %d is %d",flag,ans);
return 0;
Output:
C Program to display factors of a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
int count = 1;
while(count<=num)
if(num % count == 0)
printf("%d is a factor of %d\n",count,num);
count = count + 1;
}
return 0;
Output:
C Program to find First and Last digit of a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
int temp = num;
int nod = 0;
while(num != 0)
nod = nod + 1;
num = num/10;
}
int count = 1;
int mul=1;
while(count <= nod-1)
mul = mul * 10;
count+=1;
int fd = temp / mul;
int ld = temp % 10;
printf("The first digit of a %d is %d\n",temp,fd);
printf("The last digit of a %d is %d",temp,ld);
return 0;
Output:
C Program to find Sum of first and last digits of a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
int temp = num;
int nod = 0;
while(num != 0)
nod = nod + 1;
num = num/10;
int count = 1;
int mul=1;
while(count <= nod-1)
mul = mul * 10;
count+=1;
int fd = temp / mul;
int ld = temp % 10;
printf("The Sum of first and last digit of a %d is %d\n",temp,fd + ld);
return 0;
Output:
C Program to swap first and last digit of a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:\t");
scanf("%d",&num);
int temp = num;
int nod = 0;
while(num != 0)
nod = nod + 1;
num = num/10;
}
int count = 1;
int mul=1;
while(count <= nod-1)
mul = mul * 10;
count+=1;
int fd = temp / mul;
int ld = temp % 10;
printf("The first digit of a %d is %d\n",temp,fd);
printf("The last digit of a %d is %d\n",temp,ld);
int flag = fd;
fd = ld;
ld = flag;
printf("Digits after swapping are \t%d and \t%d",fd,ld);
return 0;
}
Output:
C Program to print multiplication Table of a number
#include<stdio.h>
int main()
int num;
printf("Enter any number:");
scanf("%d",&num);
int count = 1;
while(count <= 10)
int ans = num * count;
printf("%d * %d = %d\n",num, count,ans);
count = count + 1;
return 0;
}
Output:
C Program to Print all prime numbers between 1 to n
#include<stdio.h>
int main()
int num;
printf("Enter any number:");
scanf("%d",&num);
for(int i=1; i<=num; i++)
int count = 1;
int fact = 0;
while (count<=i)
if(i % count == 0)
fact = fact + 1;
count = count + 1;
if(fact == 2)
printf("Prime numbers are %d\n",i);
}
return 0;
Output:
C Program to find sum of prime numbers between 1 to n
#include<stdio.h>
int main()
int num;
printf("Enter any number:");
scanf("%d",&num);
int sum = 0;
for(int i=1; i<=num; i++)
int count = 1;
int fact = 0;
while (count<=i)
if(i % count == 0)
fact = fact + 1;
count = count + 1;
if(fact == 2)
{ printf("Prime numbers are %d\n",i);
sum += i;
printf("Sum of these prime numbers is %d",sum);
return 0;
Output:
C Program to find binary of all the numbers between 1 to n
#include<stdio.h>
int main()
int num;
printf("Enter any number:");
scanf("%d",&num);
for(int i = 1; i<=num; i++)
int j=i;
int mul = 1;
int sum = 0;
while(j != 0)
int rem = j % 2;
sum = sum + rem * mul;
mul = mul * 10;
j = j/2;
printf("The binary of %d is %d\n",i,sum);
return 0;
}
Output: