C Programming Basic Problem with Solution
1. Program to print "Hello World!".
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World!");
getch();
}
Output :
Hello World!
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();
}
#include<conio.h>
void main()
{
int a,b,ans;
clrscr();
a=10;
b=20;
ans=a+b;
printf("Addition is : %d",ans);
getch();
}
Output :
Addition is : 30
3. Program to accept values of two numbers and print their addition.
#include<stdio.h>
#include<conio.h>
int main()
{
{
int a,b,c;
clrscr();
printf("Enter your first number : ");
clrscr();
printf("Enter your first number : ");
scanf("%d",&a);
printf("Enter your second number : ");
scanf("%d",&b);
c=a+b;
printf("Sum of two number = %d",c);
return 0;
return 0;
}
Output :
Enter your first number : 10
Enter your second number : 7
Sum of Two Number = 17
4. Program to accept values of two numbers and print their subtraction.
#include<stdio.h>
#include<conio.h>
int main()
{
{
int a,b,c;
clrscr();
printf("Enter your first number : ");
clrscr();
printf("Enter your first number : ");
scanf("%d",&a);
printf("Enter your second number : ");
scanf("%d",&b);
c=a-b;
printf("Sub of two number = %d",c);
return 0;
return 0;
}
Output :
Enter your first number : 10
Enter your second number : 7
Sub of two number = 3
5. Program to accept values of two numbers and print their multiplication.
#include<stdio.h>
#include<conio.h>
int main()
int main()
{
int a,b,c;
clrscr();
printf("Enter your first number : ");
clrscr();
printf("Enter your first number : ");
scanf("%d",&a);
printf("Enter your second number : ");
scanf("%d",&b);
c=a*b;
printf("Mul. of two number = %d",c);
return 0;
return 0;
}
Output :
Enter your first value : 10
Enter your second value : 7
Mul. of two number = 70
6. Program to accept values of two numbers and print their division.
#include<stdio.h>
#include<conio.h>
int main()
{
{
int a,b;
float c;
clrscr();
printf("Enter your first number : ");
float c;
clrscr();
printf("Enter your first number : ");
scanf("%d",&a);
printf("Enter your second number : ");
scanf("%d",&b);
c=a/b;
printf("Div of two number = %f",c);
return 0;
return 0;
}
Output :
Enter your first value : 14
Enter your second value : 7
Div of two number = 2.0000
7. Program to print simple interest.
#include<stdio.h>
Enter Radius: 14
Area of the given radius is : 615.4400
अगर ऊपर दिए गए Solution में आपको कोई Doubt है , तो निचे Comment करे हमारी Team जल्द ही आपकी सहायता करेंगे।
#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/100;
printf("Simple Interest : %f", interest);
getch();
}
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/100;
printf("Simple Interest : %f", interest);
getch();
}
Output :
Enter value of P: 50
Enter value of R: 10
Enter value of N:10
Simple Interest : 50.0000
8. 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 : %f",area);
getch();
}
void main()
{
float area,radius;
clrscr();
printf("Enter Radius:");
scanf("%f",&radius);
area=3.14*radius*radius;
printf("Area of the given radius is : %f",area);
getch();
}
Output :
Area of the given radius is : 615.4400
9. 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();
}
#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();
}
Output :
Enter Number: 5
Square: 25
Cube: 125
10. 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();
}
#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();
}
Output :
Enter 1st number: 5
Enter 2nd number: 3
Before Swapping..
A=5, B=3
After Swapping..
A=3, B=5
11. 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();
}
#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();
}
Output :
Enter 1st number: 15
Enter 2nd number: 27
Largest value is: 27
12. 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();
}
#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();
}
Output :
Enter number: -8
Number is negative
13. 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();
}
#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();
}
Output :
Enter number:78
Number is even
14. Program to accept three numbers from user and print them in ascending and descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter three 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);
printf("\n Ascending order :%d %d %d",c,a,b);
}
else
{
printf("\n Descending order :%d %d %d",b,c,a);
printf("\n Ascending order :%d %d %d",a,c,b);
}
}
else if(c>=a&&c>=b)
{
if(a>=b)
{
printf("\n Descending order :%d %d %d",c,a,b);
printf("\n Ascending order :%d %d %d",b,a,c);
}
else
{
printf("\n Descending order :%d %d %d",c,b,a);
printf("\n Ascending order :%d %d %d",a,b,c);
}
}
getch();
}
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter three 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);
printf("\n Ascending order :%d %d %d",c,a,b);
}
else
{
printf("\n Descending order :%d %d %d",b,c,a);
printf("\n Ascending order :%d %d %d",a,c,b);
}
}
else if(c>=a&&c>=b)
{
if(a>=b)
{
printf("\n Descending order :%d %d %d",c,a,b);
printf("\n Ascending order :%d %d %d",b,a,c);
}
else
{
printf("\n Descending order :%d %d %d",c,b,a);
printf("\n Ascending order :%d %d %d",a,b,c);
}
}
getch();
}
Output :
Enter three numbers: 7
9
4
Descending order : 9 7 4
Ascending order : 4 7 9
15. 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);
#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: %f and %f", r1, r2);
}
else if (determinant == 0)
{
r1 = r2 = -b / (2 * a);
printf("Roots are: %f and %f", r1, r2);
}
else
{
real = -b / (2 * a);
imag = sqrt(-determinant) / (2 * a);
printf("Roots are: %f and %f", real, imag);
}
getch();
}
Output :
Enter coefficients a, b and c: 4
7
3
Roots are: -0.750000 and -1.000000
16. Program to accept roll number and take 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);
#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;
avg=total/3.0;
printf("\nTotal is : %d", total);
printf("\nAverage is : %f", avg);
printf("\nAverage is : %f", 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");
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();
}
Output :
Enter Roll Number : 35
Enter marks for three subjects : 65
87
79
Total is : 221
Average is : 77.000000
Grade : B
अगर ऊपर दिए गए Solution में आपको कोई Doubt है , तो निचे Comment करे हमारी Team जल्द ही आपकी सहायता करेंगे।
STAY HOME, STAY SAFE 😊
3 टिप्पणियाँ
SO EASY SIR
जवाब देंहटाएंTHANK-U @ codemasterjii
How easy 🤔
हटाएंShukriya @Arif Ahmad if u have any doubt please comment.
जवाब देंहटाएंअगर ऊपर दिए गए पाठ में आपको कोई Doubt है , तो Comment करे हमारी Team जल्द ही आपकी सहायता करेंगी।