Skip to main content

C Language Programs for beginners............



Simple C Programs



1. Program to Find Sum and Average of Three Real Numbers.

#include<conio.h>
#include<stdio.h>
void main()
{
float a, b, c, sum, avg;
clrscr();
printf("\nEnter value of three numbers: ");
scanf("%f %f %f", &a, &b, &c);
sum = a + b + c;
avg = sum / 3;
printf("\nSum = %.2f", sum);
printf("\nAverage = %.2f", avg);
getch();
}


  
2. Program to Find Area of Square and Circumference of a Circle.

#include<stdio.h>
#include<conio.h>
#define PI 3.142
void main()
{
float len, r, area, circum;
clrscr();
printf("\nEnter length of a square: ");
scanf("%f", &len);
area = len * len;
printf("\nEnter radius of a circle: ");
scanf("%f", &r);
circum = 2 * PI * r;
printf("\nArea of square = %.2f", area);
printf("\nCircumference of circle = %.2f", circum);
getch();
}



3.Program to find Sphere Surface Area and Volume of a Sphere.

/* Sphere Surface Area = 4 * PI * r * r
Volume of Sphere = (4/3) * PI * r * r * r
*/
#include<stdio.h>
#include<conio.h>
#define PI 3.142
void main()
{
float r, area, vol;
clrscr();
printf("\nEnter radius of Sphere: ");
scanf("%f", &r);
area = 4 * PI * r * r;
vol = (4/3) * PI * r * r * r;
printf("\nSphere Surface Area = %.2f", area);
printf("\nVolume of Sphere = %.2f", vol);
getch();
}


4. Program to Find Area of a Triangle using Hero Formula.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, s, area;
clrscr();
back:
printf("\nEnter three sides of a triangle: ");
scanf("%f %f %f", &a, &b, &c);
if (a==0 || b==0 || c==0)
{
printf("\nValue of any side should not be equal to zero\n");
goto back;
}
if (a+b
{
printf("\nSum of two sides should not be less than third\n");
goto back;
}
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("\n\nArea of triangle: %.2f", area);
getch();
}




5. Program to Find Simple Interest and Compound Interest.


/* SI = (p * r * t) / 100
   CI = p * pow((1 + r/100), t) - p
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p, r, t, si, ci;
clrscr();
printf("\nEnter priciple, rate and time: ");
scanf("%f %f %f", &p, &r, &t);
si = (p * r * t) / 100;
ci = p * pow((1 + r/100), t) - p;
printf("\n\nSimple Interest: %.2f", si);
printf("\n\nCompound Interest: %.2f", ci);
getch();
}


6. Program to Convert Temperature from Degree Centigrade to Fahrenheit.




/* f = (1.8*c) + 32 */


#include<stdio.h>
#include<conio.h>
void main()
{
float c, f;
clrscr();
printf("\nEnter temperature in degree Centigrade: ");
scanf("%f", &c);
f = (1.8*c) + 32;
printf("\n\nTemperature in degree Fahrenheit: %.2f", f);
getch();
}


7. Program to Convert Time in Seconds to Hours Minutes and Seconds.




#include<stdio.h>
#include<conio.h>
void main()
{
long sec, hr, min, t;
clrscr();
printf("\nEnter time in seconds: ");
scanf("%ld", &sec);
hr = sec/3600;
t = sec%3600;
min = t/60;
sec = t%60;
printf("\n\nTime is %ld hrs %ld mins %ld secs", hr, min, sec);
getch();
}


8. Program to Swap Values of Two Variables using Third Variable.




#include<stdio.h>
#include<conio.h>
main()
{
int a, b, temp;
clrscr();
printf("\nEnter any two numbers: ");
scanf("%d %d", &a, &b);
printf("\n\nBefore Swapping:\n");
printf("\na = %d\t b=%d", a,b);
temp = a;
a = b;
b = temp;
printf("\n\nAfter Swapping:\n");


printf("\na = %d\t b=%d", a,b);


getch();
}


9. Program to Swap Values of Two Variables Without using Third Variable.




#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, temp;
clrscr();
printf("\nEnter any two numbers: ");
scanf("%d %d", &a, &b);
printf("\n\nBefore Swapping:\n");
printf("\na = %d\t b=%d", a,b);


a = a + b;
b = a - b;
a = a - b;
printf("\n\nAfter Swapping:\n");
printf("\na = %d\t b=%d", a,b);
getch();
}


10. Program to Calculate the Net Salary.


/*
Basic salary of an employee is input through the keyboard. The DA
is 25% of the basic salary while the HRA is 15% of the basic
salary. Provident Fund is deducted at the rate of 10% of the gross
salary(BS+DA+HRA).
Program to Calculate the Net Salary.
*/
#include<stdio.h>
#include<conio.h>
void main()
{
float basic_sal, da, hra, pf, gross_sal, net_sal;
clrscr();
printf("\nEnter basic salary of the employee: Rs. ");
scanf("%f", &basic_sal);
da = (basic_sal * 25)/100;
hra = (basic_sal * 15)/100;
gross_sal = basic_sal + da + hra;
pf = (gross_sal * 10)/100;
net_sal = gross_sal - pf;
printf("\n\nNet Salary: Rs. %.2f", net_sal);
getch();
}


Control Statements programs in next post........................

Comments

  1. Great remarkable things here. I'm very satisfied to peer your article. Thank you so much and I'm taking a look ahead to touch you. Will you kindly drop me a e-mail?
    ECCO Men's Track II Low Gore-Tex Oxford

    ReplyDelete
  2. I am continuously browsing online for tips that can aid me. Thank you!
    Sorel Women's Helen Of Tundra Boot

    ReplyDelete

Post a Comment

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }