C Program

C Code

C Code

1. Enter a value from integer variable and print it on screen.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x=10;
    clrscr();
    printf("X : %d",x);
    return 0;
}
        

2. Input two numbers and print it's addition.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter X : ");
    scanf("%d",&x);
    printf("\nEnter Y : ");
    scanf("%d",&y);
    printf("\nX + Y = %d",x+y);
    return 0;
}
        

3. Input two number and print it's square and cube.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    clrscr();
    printf("Enter X : ");
    scanf("%d",&x);
    printf("Square = %d",x*x);
    printf("\nCube = %d",x*x*x);
    return 0;
}
        

4. Input two number and print it's addition, subtraction, multiplication, divion.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    float z;
    clrscr();
    printf("Enter X : ");
    scanf("%d",&x);
    printf("\nEnter Y : ");
    scanf("%d",&y);
    z=(float)x/y;
    printf("\nX + Y = %d",x+y);
    printf("\nX - Y = %d",x-y);
    printf("\nX × Y = %d",x*y);
    printf("\nX ÷ Y = %.2f",z);
    return 0;
}
        

5. Calculate the area of circle.

#include <stdio.h>
#include <conio.h>
#define PI 3.1416
int main()
{
    int r;
    clrscr();
    printf("Enter Radius : ");
    scanf("%d",&r);
    printf("\nArea of Circle : %.2f",PI*r*r);
    return 0;
}
        

6. Calculate the area of triangle.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter Length : ");
    scanf("%d",&x);
    printf("Enter Breadth : ");
    scanf("%d",&y);
    printf("\nArea of Triangle = %.2f",0.5*x*y);
    return 0;
}
        

7. verify the formula of Simple Interest.

#include <stdio.h>
#include <conio.h>
int main()
{
    int pr,year,rate;
    float interest;
    clrscr();
    printf("Enter Principle Amount : ");
    scanf("%d",&pr);
    printf("\nEnter Rate of interest : ");
    scanf("%d",&rate);
    printf("\nEnter No. of year : ");
    scanf("%d",&year);
    interest=(float)(pr*year*rate)/100;
    printf("\nSimple interest = %.2f",interest);
    return 0;
}
        

8. verify the formula of Compound Interest.

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

	int n=4,t,p;

	float r,result,sum,z;

	clrscr();

	printf("Enter Principal Amount : ");

	scanf("%d",&p);

	printf("Enter Rate of interest : ");

	scanf("%f",&r);

	printf("Enter Total Time Period in year\n");

	scanf("%d",&t);

	r=r/100;

	sum=(float) 1+(r/n);

	z=pow(sum,(n*t));

	result=(float) p*z;

	

	printf("\nCompound interest : %f",result);

	printf("\nProfit : %f",result-p);

}
        

9. Write a program to enter length and bredth of rectangle amd find its perimeter.

#include <stdio.h>
#include <conio.h>
int main()
{
    int l,b;
    clrscr();
    printf("Enter Length : ");
    scanf("%d",&l);
    printf("\nEnter Breadth : ");
    scanf("%d",&b);
    printf("\n Perimeters of Rectangle = %d",2*(l+b));
    return 0;
}
        

10. Write a C program enter radius of a circle and find its diameter , circumference and area.

#include <stdio.h>
#include <conio.h>
#define PI 3.1416
int main()
{
    int r;
    clrscr();
    printf("Enter Radius : ");
    scanf("%d",&r);
    printf("\nDiameter : %d",2*r);
    printf("\nCircumference = %.2f",PI*2*r);
    printf("\nArea of circle = %.2f",PI*r*r);
    return 0;
}
        

11. Input a rupee and print its value converted into dollar.

#include <stdio.h>
#include <conio.h>
int main()
{
    int ru;
    float d;
    clrscr();
    printf("Enter Indian Rupee : ");
    scanf("%d",&ru);
    printf("\nConverting INR to USD....\n");
    d=(float)ru/82;
    printf("\nU.S. Dollar : %.2f",d);
    return 0;
}
        

12. Write a C program to enter Length in centimeter and convert it into meter and kilometer.

#include <stdio.h>
#include <conio.h>
int main()
{
    int cm;
    float m,km;
    clrscr();
    printf("Enter length in cm : ");
    scanf("%d",&cm);
    m=(float)cm/1000;
    printf("\nMeter : %.fm",m);
    km=(float)m/1000;
    printf("\nKilometer = %.2fkm",km);
    return 0;
}
        

13. write a c program to enter days and convert it to week and year

#include <stdio.h>
#include <conio.h>
int main()
{
    int w,d;
    float y;
    clrscr();
    printf("Enter days : ");
    scanf("%d",&d);
    y=(float)d/365;
    w=d/7;
    printf("\nWeeks : %d",w);
    printf("\nYears : %.2f",y);
    return 0;
}
        

14. Write a C program to find power of any number xy (x^y).

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y,i,z;
    clrscr();
    printf("Enter No. : ");
    scanf("%d",&x);
    int a=x;
    printf("Enter it's power : ");
    scanf("%d",&y);
    for(i=1;i<y;i++)
    {
        
        z=x*a;
        x=z;
    }
    printf("\nPower of X : %d",x);
    return 0;
}
        

15. Write a C program to enter marks of five subjects and calculate total, average and percentage.

#include <stdio.h>
#include <conio.h>
int main()
{
    int c,e,m,h,cf;
    clrscr();
    printf("Enter Marks of C : ");
    scanf("%d",&c);
    printf("Enter Marks of English : ");
    scanf("%d",&e);
    printf("Enter Marks of Maths : ");
    scanf("%d",&m);
    printf("Enter Marks of HVHL : ");
    scanf("%d",&h);
    printf("Enter Marks of C/F : ");
    scanf("%d",&cf);
    int total=c+e+m+h+cf;
    int avg=total/5;
    float per=((float)total/500)*100;
    printf("\nTotal Marks = %d",total);
    printf("\nAverage Marks = %d",avg);
    printf("\nPercentage = %.2f",per);
    return 0;
}
        

16. Input a Number of Chairs and its Total Cost and Prints the Cost of Each chair.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    float z;
    clrscr();
    printf("Enter no. of chairs : ");
    scanf("%d",&x);
    printf("\nEnter total price of chairs : ");
    scanf("%d",&y);
    z=(float)y/x;
    printf("\nprice of one chair = %.2f",z);
    return 0;
}
        

17. Input 2 numbers and print the maximum among them.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    printf("\nEnter no. Y : ");
    scanf("%d",&y);
    if(x>y)
    {
        printf("\nX is more = %d",x);
    }
    else
    {
        printf("\nY is more = %d",y);
    }
    return 0;
}
        

18. Input 2 numbers and print the maximum among them. (Without If statement)

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    printf("\nEnter no. Y : ");
    scanf("%d",&y);
    (x>y)?printf("\nX is more = %d",x):printf("\nY is more = %d",y);
    return 0;
}
        

19. Input 2 numbers and print the minimum among them.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    printf("\nEnter no. Y : ");
    scanf("%d",&y);
    if(x<y)
    {
        printf("\nX is minimum = %d",x);
    }
    else
    {
        printf("\nY is minimum = %d",y);
    }
    return 0;
}
        

20. Input 2 numbers and print the minimum among them. (Without If statement)

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    printf("\nEnter no. Y : ");
    scanf("%d",&y);
    (x<y)?printf("\nX is minimum = %d",x):printf("\nY is minimum = %d",y);
    return 0;
}
        

21. Input 3 numbers and print the maximum among them.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x,y,z;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    printf("\nEnter no. Y : ");
    scanf("%d",&y);
    printf("\nEnter no. Z : ");
    scanf("%d",&z);
    if(x>y)
    {
        if(x>z)
        {
            printf("\nX is more = %d",x);
        }
        else
        {
            printf("\nZ is more = %d",z);
        }
    }
    else if(y>z)
    {
        printf("\nY is more = %d",y);
    }
    else 
    {
        printf("\nZ is more = %d",z);
    }
    return 0;
}
        

22. Input a number & print the number is odd or even.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    if(x%2==0)
    {
        printf("\nEVEN");
    }
    else
    {
        printf("\nODD");
    }
    return 0;
}
        

23. input a number and check whether the number is positive, negative or 0

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    if(x>0)
    {
        printf("\n%d is Positive",x);
    }
    else if(x > 0)
    {
        printf("\n%d is Negetive",x);
    }
    else
    {
        printf("\n%d is  Neither negative nor positive",x);
    }
    return 0;
}
        

24. Write a C program to check whether a number is divisible by 5 and 11 or not.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    clrscr();
    printf("Enter no. X : ");
    scanf("%d",&x);
    if(x%5==0 && x%11==0)
    {
        printf("\n%d is divisible by 5 and 11",x);
    }
    else
    {
        printf("\n%d is not divisible by 5 and 11",x);
    }
    return 0;
}
        

25. Write a C program to enter Cost price and selling price and calculate profit or loss.

#include <stdio.h>
#include <conio.h>
int main()
{
    int cp,sp,profit;
    clrscr();
    printf("Enter Cost Price : ");
    scanf("%d",&cp);
    printf("\nEnter Selling Proce : ");
    scanf("%d",&sp);
    profit=sp-cp;
    if(profit>0)
    {
        printf("\nProfit = %d",profit);
    }
    else
    {
        printf("\nLoss = %d",profit*(-1));
    }
    return 0;
}
        

26. Input the age of 5 student and print the number of student having age less than or equal to 21 and greater than 21

#include <stdio.h>
#include <conio.h>
int main()
{
    int s1,s2,s3,s4,s5,age=0,ag=0;
    clrscr();
    printf("Enter age of student 1 : ");
    scanf("%d",&s1);
    (s1>21)?age++:ag++;
    printf("Enter age of student 2 : ");
    scanf("%d",&s2);
    (s2>21)?age++:ag++;
    printf("Enter age of student 3 : ");
    scanf("%d",&s3);
    (s3>21)?age++:ag++;
    printf("Enter age of student 4 : ");
    scanf("%d",&s4);
    (s4>21)?age++:ag++;
    printf("Enter age of student 5 : ");
    scanf("%d",&s5);
    (s5>21)?age++:ag++;
    printf("\nStudents whose age is more than 21 = %d",age);
    printf("\nStudents whose age is less than or equal to 21 = %d",ag);
    return 0;
}
        

27. Write a C program to input marks of three subjects Physics,Chemistry, Biology Mathematics and Computer, Calculate total, percentage and grade according to following: equal to 21 and greater than 21
Percentage >= 70% : Distinction
Percentage >= 60% and Percentage < 70% : First Class
Percentage >= 50% and Percentage < 60% : Second Class
Percentage >= 40% and Percentage < 50% : Pass Class
Otherwise : Fail

#include <stdio.h>
#include <conio.h>
int main()
{
    int phy,che,bio,math,comp,total,per;
    clrscr();
    printf("Enter Marks of Physics : ");
    scanf("%d",&phy);
    printf("\nEnter Marks of Chemistry : ");
    scanf("%d",&che);
    printf("\nEnter Marks of Biology : ");
    scanf("%d",&bio);
    printf("\nEnter Marks of Mathematics : ");
    scanf("%d",&math);
    printf("\nEnter Marks of Computer : ");
    scanf("%d",&comp);
    total=phy+che+bio+math+comp;
    getch();
    printf("\nTotal Marks = %d",total);
    per=total/5;
    printf("\nPercentage = %d%",per);
    if(per>=90)
    {
        printf("\nGrade = Distinction");
    }
    else if(per>=70)
    {
        printf("\nGrade = First class");
    }
    else if(per>=60)
    {
        printf("\nGrade = Second class");
    }
    else if(per>=35)
    {
        printf("\nGrade = Pass");
    }
    else
    {
        printf("\nGrade = Fail");
    }
    getch();
    return 0;
}
        

28. Write a C program to input basic salary of an employee and calculate its Gross salary according to following: Basic Salary >= 1O000 : HRA = 20%, DA = 80%
Basic Salary >= 20000 : HRA = 25%, DA = 90%
Basic Salary >= 30000 : HRA =30%. DA = 95%

#include <stdio.h>
#include <conio.h>
int main()
{
    int base;
    float hra,da,gross;
    clrscr();
    printf("Enter Basic Salary : ");
    scanf("%d",&base);
    if(base>20000)
    {
        hra=0.30*base;
        da=0.95*base;
    }
    else if(base>10000)
    {
        hra=0.25*base;
        da=0.90*base;
    }
    else
    {
        hra=0.20*base;
        da=0.80*base;
    }
    gross=base+hra+da;
    printf("\nHRA = %.2f",hra);
    printf("\nDA = %.2f",da);
    printf("\nGross Salary = %.2f",gross);
    return 0;
}
        

29. Write a C program to input electricity unit charges and calculate total electricity bill
according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

#include <stdio.h>
#include <conio.h>
int main()
{
    int unit;
    float charge,price,bill;
    clrscr();
    printf("Enter Electricity unit : ");
    scanf("%d",&unit);
    if(unit>250)
    {
        price=1.5*unit;
    }
    else if(unit>150)
    {
        price=1.20*unit;
    }
    else if(unit>50)
    {
        price=0.75*unit;
    }
    else
    {
        price=0.50*unit;
    }
    
    charge=price*0.20;
    printf("\nUsed electriciy   %.2f(₹)",price);
    printf("\nExtra Charge.     +%.2f(₹)",charge);
    printf("\nTotal bill        =%.2f(₹)",price+charge);
    return 0; 
}
        

Post a Comment

Post a Comment (0)

Previous Post Next Post