C Language Programs
1. Program to print "Hello World!!".
2. Program to assign values of two numbers and print their addition.
3. Program to accept values of two numbers and print their addition.
4. Program to print simple interest.
5. Program to accept value of radius and print area of a circle.
6. Program to accept a number from user and print it’s square & cube.
7. Program to accept two values of a & b and swap their values.
8. Program to accept two number and print largest among them.
9. Program to accept a number and check whether the number is Positive, Negative or Zero.
10. Program to check whether the number is even or odd.
11. Program to accept three numbers from user and print them in ascending and decending order.
12. Program to find the roots of a quadratic equation.
13. Program to accept rollnumber and marks of three subjects from user and print total marks, average and grade.
14. Program to print numbers from 1 to n using while loop.
15. Program to print numbers from n to 1 using Do While loop.
14. Program to print numbers from 1 to n using while loop.
15. Program to print numbers from n to 1 using Do While loop.
16. Program to print first n even numbers.
17. Program to accept a number and print that number in reverse order.
Ex:- 1024
Output:- 4201
Ex:- 1024
Output:- 4201
18. Program to accept a number and print sum of it’s digits.
19. Program to take a number from user and check whether it is Armstrong number or not.
20. Program to take number from user and print table of that number.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World!!");
getch();
}
2. Program to assign values of two numbers and print their addition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ans;
clrscr();
a=10;
b=20;
ans=a+b;
printf("Addition is : %d",ans);
getch();
}
3. Program to accept values of two numbers and print their addition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ans;
clrscr();
printf("Enter 1st number:");
scanf("%d",&a);
printf("Enter 2nd number:");
scanf("%d",&b);
ans=a+b;
printf("Addition is : %d",ans);
getch();
}
4. Program to print simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
float interest, p, r, n;
clrscr();
printf("Enter value of P: ");
scanf("%f",&p);
printf("Enter value of R: ");
scanf("%f",&r);
printf("Enter value of N: ");
scanf("%f",&n);
interest=p*r*n/100f;
printed("Simple Interest : %f", interest);
getch();
}
5. Program to accept value of radius and print area of a circle.
#include<stdio.h>
#include<conio.h>
void main()
{
float area,radius;
clrscr();
printf("Enter Radius:");
scanf("%f",&radius);
area=3.14*radius*radius;
printf("Area of the given radius is : %6.2f",area);
getch();
}
6. Program to accept a number from user and print it’s square & cube.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sqre,cube;
clrscr();
printf("Enter Number: ");
scanf("%d",&n);
sqre=n*n;
cube=n*n*n;
printf("\nSquare: %d\nCube: %d",sqre,cube);
getch();
}
7. Program to accept two values of a & b and swap their values.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
printf("\nBefore Swapping..\n A=%d, B=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\nAfter Swapping..\n A=%d, B=%d",a,b);
getch();
}
8. Program to accept two number and print largest among them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
if(a>b)
printf("Largest value is: %d",a);
else
printf("Largest value is: %d",b);
getch();
}
9. Program to accept a number and check whether the number is Positive, Negative or Zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
if(n>0)
printf("Number is positive");
else if(n<0)
printf("Number is negative");
else
printf("Number is Zero");
getch();
}
10. Program to check whether the number is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter number: );
scanf("%d",&n);
if(n%2==0)
printf("Number is even");
else
printf("Number is odd");
getch();
}
11. Program to accept three numbers from user and print them in ascending and decending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter numbers:");
scanf("%d%d%d",&a,&b,&c);
if((a>=b)&&(a>=c))
{
if(b>=c)
{
printf("\n Descending order:
%d %d %d",a,b,c);
printf("\n Ascending order :
%d %d %d",c,b,a);
}
else
{
printf("\n Descending order :
%d %d %d",a,c,b);
printf("\n Ascending order :
%d %d %d",b,c,a);
}
}
else if((b>=a)&&(b>=c))
{
if(a>=c)
{
printf("\n Descending order :
%d %d %d",b,a,c);
}
}
getch();
}
12. Program to find the roots of a quadratic equation.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, determinant, real;
double r1, r2, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f", &a, &b, &c);
determinant = b * b - 4 * a * c;
if (determinant > 0)
{
r1 = (-b + sqrt(determinant)) / (2 * a);
r2 = (-b - sqrt(determinant)) / (2 * a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else if (determinant == 0)
{
r1 = r2 = -b / (2 * a);
printf("Roots are: %.2f and %.2f", r1, r2)
}
else
{
real = -b / (2 * a);
imag = sqrt(-determinant) / (2 * a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
getch();
}
13. Program to accept rollnumber and marks of three subjects from user and print total marks, average and grade.
#include<stdio.h>
#include<conio.h>
void main()
{
int RollNum, m1, m2, m3, total;
float avg;
clrscr();
printf("Enter Roll Number : ");
scanf("%d",&RollNum);
printf("Enter marks for three subjects : ");
scanf("%d%d%d", &m1, &m2, &m3);
total=m1+m2+m3;
avg=total/3.0;
printf("\nTotal is : %d", total);
printf("\nAverage is : %5.2f", avg);
if(avg>80)
printf("\nGrade : A");
else if((avg>60)&&(avg<=80))
printf("\nGrade : B");
else if((avg>40)&&(avg<=60))
printf("\nGrade : C");
else if((avg>=33)&&(avg<=40)
printf("\nGrade : D");
else
printf("\nGrade : Fail");
getch();
}
14. Program to print numbers from 1 to n using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
while(i<=n)
{
printf("%d\t",i);
i++;
}
getch();
}
15. Program to print numbers from n to 1 using Do While loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
i=n;
do
{
printf("%d\t",i);
i--;
}while(i>=1);
getch();
}
16. Program to print first n even numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2, n;
//to print odd numbers set variable i=1
clrscr();
printf("Enter n : ");
scanf("%d", &n);
while(i<=n)
{
printf("%d\t",i);
i=i+2;
}
getch();
}
17. Program to accept a number and print that number in reverse order.
Ex:- 1024
Output:- 4201
#include<stdio.h>
#include<conio.h>
void main()
{
int reminder, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
while(n>0)
{
reminder=n%10;
printf("%d", reminder);
n=n/10;
}
getch();
}
18. Program to accept a number and print sum of it’s digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int reminder, sum=0, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
while(n>0)
{
reminder=n%10;
sum=sum+reminder;
n=n/10;
}
printf("Sum of digits : %d",sum);
getch();
}
19. Program to take a number from user and check whether it is Armstrong number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=2, temp, rem, sum=0, n;
clrscr();
printf("Enter n : ");
scanf("%d", &n);
temp = n;
while(n>0)
{
rem = n%10;
sum = sum+(rem*rem*rem);
n = n/10;
}
if(temp==sum)
printf("Entered number is an Armstrong Number");
else
printf("Entered number is not an Armstrong Number");
getch();
}
20. Program to take number from user and print table of that number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=1; i<=10; i++)
printf("%d × %d = %d\n", n, i, n*i);
getch();
}
THANK YOU
0 Comments