0% found this document useful (0 votes)
5 views18 pages

C Programming Basics: Sample Programs

The document contains a series of C programming exercises that cover various topics such as calculating averages, volumes, areas, and performing mathematical operations. Each exercise includes a complete C program with input prompts and output statements. The programs demonstrate fundamental programming concepts including loops, conditionals, and functions.

Uploaded by

gtf22t2bm2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

C Programming Basics: Sample Programs

The document contains a series of C programming exercises that cover various topics such as calculating averages, volumes, areas, and performing mathematical operations. Each exercise includes a complete C program with input prompts and output statements. The programs demonstrate fundamental programming concepts including loops, conditionals, and functions.

Uploaded by

gtf22t2bm2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMMING IN C

1. Write a C program to find the average of any two numbers given by the user.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
float c;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
c=(a+b)/2;
printf("Average of two numbers= %.2f",c);
return 0;
}
2. Write a C program to calculate the average of three numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
float d;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter second number: ");
scanf("%d", &c);
d=(a+b+c)/3;
printf("Average of two numbers= %.2f",d);
return 0;
}
3. Write a C program to calculate the volume of a cylinder.
#include<stdio.h>
#include<conio.h>
int main()
{
float r, h, v;
printf("Enter radius and height: \n");
scanf("%f %f", &r, &h);
v=(22/7)×r×r×h;
printf("Volume of cylinder= %.2f",v);
return 0;
}

4. Write a C program to calculate distance travelled by a body.

#include<stdio.h>
#include<conio.h>
int main()
{
float u, a, s;
int t;
printf("Enter initial velocity: ");
scanf("%f", &u);
printf("Enter acceleration: ");
scanf("%f", &a);
printf("Enter time: ");
scanf("%d", &t);
s=(u*t)+(a*t*t)/2;
printf("Distance travelled by body= %.2f", s);

return 0;
}

5. Write a C program using to get radius of circle and then print its area.

#include<stdio.h>
#include<conio.h>
int main()
{
float r, a;
printf("Enter radius: ");
scanf("%f", &r);
a=(22/7)*r*r;
printf("Area of circle= %.2f", a);
return 0;
}

6. Write a C program to calculate and print the volume of a box.

#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,h,v;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
v=l*b*h;
printf("Volume of box= %d", v);
return 0;
}

7. Write a C program to get radius of a circle and then print its circumference.

#include<stdio.h>
#include<conio.h>
int main()
{
float r, c;
printf("Enter radius: ");
scanf("%f", &r);
c=2×(22/7)×r;
printf("Circumference of circle= %.2f", c);
return 0;
}

8. Write a C program to calculate the area of four walls.

#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,h,a;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a=2×h×(l+b);
printf("Area of four walls= %d", a);
return 0;
}

9. Write a C program in QBASIC to find the total surface area of a box.

#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,h,a;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a=2×((l×b)+(b×h)+(h×l));
printf("Total surface area of box= %d", a);
return 0;
}

10. Write a C program to calculate and print the simple interest.

#include<stdio.h>
#include<conio.h>
int main()
{
float p, t, r, i;
printf("Enter principal: ");
scanf("%f", &p);
printf("Enter time: ");
scanf("%f", &t);
printf("Enter rate: ");
scanf("%f", &r);
i = (p*t*r)/100;
printf("Simple Interest = %.2f", i);
return 0;
}

11. Write a C program to get temperature in celsius from the user and then print the temperature
in fahrenheit.

#include<stdio.h>
#include<conio.h>
int main()
{
float c, f;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
f = ((c × 9)/5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", c, f);
return 0;
}

12. Write a C program to find the area of triangle.


#include<stdio.h>
#include<conio.h>
int main()
{
int b,h;
float a;
printf("Enter base: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a=(b×h)/2;
printf("Area of triangle= %.2f", a);
return 0;
}

13. Write a C program to get temperature in fahrenheit from the user and then print the
temperature in celcius.

#include <stdio.h>
#include<conio.h>
int main()
{
float c, f;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
c = (f - 32) * 5 / 9;
printf("%.2f Fahrenheit = %.2f Celsius", f, c);
return 0;
}

14. Write a C program to convert USD(dollar) into NC (NEPALI currency) using DECLARE
FUNCTION.

#include<stdio.h>
#include<conio.h>
int main()
{
int d, n;
printf("Enter currency in dollar: ");
scanf("%d", &d);
n=d×100;
printf("$ %d = Rs. %d ", d, n);

return 0;
}

15. Write a C program to convert NC (NEPALI currency) into IC (Indian Currency) using DECLARE
SUB.

#include<stdio.h>
#include<conio.h>
int main()
{
float i, n;
printf("Enter currency in Nepali Rupees: ");
scanf("%f", &n);
i=n/1.6;
printf("Nrs. Rs %.2f = IC Rs. %.2f ", n, i);

return 0;
}
16. WRITE A C PROGRAM to display whether the given number is positive, negative or zero.

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if(n>0)
printf("%d is positive number",n);
else if(n<0)
printf("%d is negative number",n);
else
printf("%d is zero number",n);
return 0;
}

17. Write a C program to test whether the given number is completely divisible by 13 or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if(n%2==0)
printf("%d is divisible by 13",n);
else
printf("%d is not divisible by 13",n);
return 0;
}

18. WRITE A C PROGRAM to check whether the given number is divisible by 3 and 5 or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if(n%3==0 && n%5==0)
printf("%d is divisible by 3 and 5",n);
else
printf("%d is not divisible by 3 and 5",n);
return 0;
}

19. WRITE A C PROGRAM to enter age and display whether a person can vote or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter your age: ");
scanf("%d", &a);
if(a>=18)
printf("You can vote");
else
printf("You cannot vote");
return 0;
}

20. Write a C program to display the smaller among two numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("Enter any two numbers:\n ");
scanf("%d %d", &a, &b);
if(a<b)
printf("The smaller number is %d", a);
else
printf("The smaller number is %d", b);
return 0;
}

21. Write a C program to input three different and find the greatest number.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter any three numbers:\n ");
scanf("%d %d %d" , &a, &b, &c);
if(a>b && a>c)
printf("The greatest number is %d", a);
else if(b>a && b>c)
printf("The greatest number is %d", b);
else
printf("The greatest number is %d", c);
return 0;
}

22. WRITE A C PROGRAM to display middle number among three different numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter any three numbers:\n ");
scanf("%d %d %d" , &a, &b, &c);
if((a>b && a<c) || (a<b && a>c))
printf("The middle number is %d", a);
else if((b>a && b<c) || (b<a && b>c))
printf("The middle number is %d", b);
else
printf("The middle number is %d", c);
return 0;
}

23. Write a C program to check whether the given number is odd or even.

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if(n%2==0)
printf("%d is even number", n);
else
printf("%d is odd number", n);
return 0;
}

24. WRITE A C PROGRAM to check whether the given year is leap year or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int y;
printf("Enter the year: ");
scanf("%d", &y);
if((y%2==0) && (y%100 !=0) || (y%400==0))
printf("%d is leap year", y);
else
printf("%d is not leap year", y);
return 0;
}

25. WRITE A C PROGRAM to check whether the given number is perfect square or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, b;
float a;
printf("Enter any number: ");
scanf("%d", &n);
a=sqrt(n);
b=a;
if(a==b)
printf("%d is a perfect square number", n);
else
printf("%d is not a perfect square number", n);
return 0;
}

26. Write a C program to print the following series 9,7,5,…1.

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=9;i>=1;i=i-2)
{
printf("%d \n", i);
}
return 0;
}

27. Write a C program to print the series 1,1,2,3,5,8… upto ten terms.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,i;
a=1;
b=1;
for(i=1;i<=10;i++)
{
printf("%d \n", a);
c=a+b;
a=b;
b=c;
}
return 0;
}

28. Write a C program to print natural numbers from 1 to 5.

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
i=1;
while(i<=5)
{
printf("%d \n", i);
i++;
}
return 0;
}

29. Write a C program to print the first ten odd numbers.

#include<stdio.h>
#include<conio.h>
int main()
{
int i, a;
i=1;
a=1;
while(i<=10)
{
printf("%d \n", a);
a=a+2;
i++;
}
return 0;
}

30. Write a C program to print the sum of digits of a given number.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, r;
s=0;
printf("Enter any number: ");
scanf("%d", &n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("Sum of digits= %d",s);
return 0;
}

31. WRITE A C PROGRAM to display product of digits.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, p, r;
p=1;
printf("Enter any number: ");
scanf("%d", &n);
while(n>0)
{
r=n%10;
p=p*r;
n=n/10;
}
printf("Product of digits= %d",p);
return 0;
}

32. WRITE A C PROGRAM to reverse the given digits.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, r;
s=0;
printf("Enter any number: ");
scanf("%d", &n);
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
printf("Reverse digits= %d",s);
return 0;
}

33. WRITE A C PROGRAM to check whether the given number is prime or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, i=1, c=0;
printf("Enter any number: ");
scanf("%d", &n);
for(i=2;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
printf("%d is prime number", n);
else
printf("%d is not prime number", n);

return 0;
}

34. WRITE A C PROGRAM to check whether the given number is palindrome or not.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, s, r, a;
s=0;
printf("Enter any number: ");
scanf("%d", &n);
a=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(a==s)
{
printf("%d is palindrome number", a);
}
else{
printf("%d is not palindrome number",a);
}
return 0;
}
35. Write a C program to print the multiplication table of any input number up to tenth terms.
#include<stdio.h>
#include<conio.h>
int main()
{
int n, i=1, c=0;
printf("Enter any number: ");
scanf("%d", &n);
for(i=1;i<=10;i++)
{
c=n*i;
printf("%d X %d = %d \n", n,i,c);
}

return 0;
}

You might also like