Simple interest
Simple interest
#include < stdio.h >
void main()
{
int amount, rate, time, ans;
printf("\nEnter Principal Amount : ");
scanf("%d", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%d", &rate);
printf("\nEnter Period of Time : ");
scanf("%d", &time);
ans = (amount * rate * time)/100;
/*Simple interest formula*/
printf("\nSimple Interest : %d",ans);
}
Area of Circle
Area of Circle
#include < stdio.h >
#include < math.h >
#define PI 3.142
void main()
{
float radius, area;
printf("Enter the radius of a circle \n");
scanf("%f", &radius);
area = PI * pow(radius, 2);
printf("Area of a circle = %5.2f\n", area);
}
Area of Rectangle
Area of Rectangle
#include < stdio.h >
#include < conio.h >
int main() {
int length, breadth, area;
printf("\nEnter the Length of Rectangle : ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle : ");
scanf("%d", &breadth);
area = length * breadth;
printf("\nArea of Rectangle : %d", area);
return (0);
}
Area of Square
Area of Square
#include < stdio.h >
int main() {
int side, area;
printf("\nEnter the Length of Side : ");
scanf("%d", &side);
area = side * side;
printf("\nArea of Square : %d", area);
return (0);
}
Volume of Cube
Volume of Cube
#include < stdio.h >
void main()
{
float a;
float surface_area,volume;
printf("Enter size of any side of a cube : ");
scanf("%f",&a);
surface_area = 6 * (a * a);
volume = a * a * a;
printf("Surface area of cube is: %.3f",surface_area);
printf("\nVolume of cube is : %.3f",volume);
}
Volume of Cylinder
Volume of Cylinder
#include < stdio.h >
void main()
{
float vol,pie=3.14;
float r,h;
printf("ENTER THE VALUE OF RADIOUS :- ");
scanf("%f",&r);
printf("ENTER THE VALUE OF HEIGHT :- ");
scanf("%f",&h);
vol = pie * r * r * h;
printf("VOLUME OF CYLINDER IS :- %3.2f ",vol);
}
Volume of Sphere
Volume of Sphere
#include < stdio.h >
#include < math.h >
void main()
{
float radius,pie,volume;
pie=3.1416;
printf("Enter the radius:");
if(scanf("%f",&radius)==1)
{
volume=(4/3)*pie*pow(radius,3);
printf("The volume is :%6.2f",volume);
}
else
{
printf("error ,enter correct value");
}
}
Leap year
Leap year
#include < stdio.h >
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
HCF and LCM
HCF and LCM
#include < stdio.h >
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf("Enter two integers\n");
scanf("%ld%ld", &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
/*if 1st no is 0 then 2nd no is gcd
make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd*/
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y) {
x = x - y;
}
else {
y = y - x;
}
}
return x;
}
Reverse number
Reverse number
#include < stdio.h >
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
/*taking unit place digit of no and moving to reverse
Dividing the no to discard unit place digit*/
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
Prime numbers
Prime numbers
#include < stdio.h >
int check_prime(int);
main()
{
int i, n, result;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
printf("First %d prime numbers are :\n", n);
for(i=0; i < n; i++){
result = check_prime(i);
/*if i is prime then it will return 1*/
if ( result == 1 )
printf("%d \n", i);
}
return 0;
}
int check_prime(int a)
{
int c;
/*starting from 2, if no is completely divisible by any no then it is not prime*/
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
Perfect Number
Perfect Number
#include < stdio.h >
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i < n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Calculate the gross salary
#include < stdio.h >
void main()
{
int gross_salary, basic, da, ta;
printf("Enter basic salary : ");
scanf("%d", &basic);
da = (10 * basic)/100;
ta = (12 * basic)/100;
gross_salary = basic + da + ta;
printf("\nGross salary : %d",gross_salary);
}
Temperature conversion
Temperature conversion
#include < stdio.h >
void main()
{
int from , to;
float value;
printf("Temperature Conversion\n");
printf("Enter no of Unit to covert from : \n 1.celcius\n 2. Farenheit\n 3. Kelvin");
scanf("%d",&from);
printf("Enter no of Unit to covert to : \n 1.celcius\n 2. Farenheit\n 3. Kelvin");
scanf("%d",&to);
printf("Enter The value to convert: ");
scanf("%f",&value);
/*converting given value from specified unit to Kelvin*/
switch(from) {
case 1:
value= value + 273.15; break;
case 2:
value= (value+459.67)*5/9; break;
case 3: break;
default: break;
}
/*converting value from Kelvin to specified unit*/
switch(to) {
case 1:
value= value-273.15; break;
case 2:
value= value*9/5-459.67; break;
case 3: break;
default: break;
}
printf("Converted Value : %f", value);
}
Print integer
#include < stdio.h >
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
//takes an integer from user
printf("Integer that you have entered is %d\n", a);
return 0;
}
Area of triangle
#include < stdio.h >
void main()
{
int height, base;
float ans;/*ans may come in fractions*/
printf("Enter height and base");
scanf("%d %d",&height, &base);
ans= (1/2)*height*base;
/* mathematical formula*/
printf("Area if triangle is %f",ans);
}
Odd or Even
#include < stdio.h >
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
/*if n is completely divisible by 2 then prints even otherwise n is odd*/
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Add subtract multiply divide
#include < stdio.h >
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second;
//typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
Add n numbers
#include < stdio.h >
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d",&value);
sum = sum + value;
/*adding each no in sum*/
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
Greatest of 3 numbers
#include < stdio.h >
void main()
{
int a,b,c;
printf("enter any three numbers:\n");
scanf("%d%d%d",&a, &b, &c);
if(a>b&&a>c)
/*if a is greater than b & c*/
printf("greatest number is: %d",a);
else if(b>c)
/*if not a then if b is greater than c*/
printf("greatest number is: %d",b);
else
/*if a & b are not greater*/
printf("greatest number is: %d",c);
}
Swapping two numbers
#include < stdio.h >
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
/*using temp to swap
storing x to temp and y to x then moving temp to y*/
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}
Nested If Else
#include < stdio.h >
void main()
{
int marks;
printf("Enter your marks : ");
scanf("%d",&marks);
if(marks>100)
/*marks greater than 100*/
printf("Not valid marks");
else if(marks>=80)
/*marks between 80 & 99*/
printf("your grade is A");
else if(marks >=70)
/*marks between 70 & 79*/
printf("your grade is B");
else if(marks>=50)
/*marks between 50 & 69*/
printf("your grade is C");
else if(marks>=35)
/*marks between 35 & 49*/
printf("your grade is D");
else
/*marks less than 35*/
printf("your grade is E");
}
Calculate percentage
#include < stdio.h >
void main()
{
int s1, s2, s3, s4, s5, sum, total = 500;
float per;
printf("\nEnter marks of 5 subjects : ");
scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);
per = (sum * 100)/500;
/* percentage formula*/
printf("\nPercentage : %f", per);
}
Addition of two no
#include < stdio.h >
int main()
{
int first, second, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
sum = first + second ;
/*Adding contents of first and second and storing in sum*/
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
Hello World
#include < stdio.h >
//compiler to include std input output header file.
int main()
{
printf("Hello world\n");
//prints Hello world on user screen
return 0;
//return the value zero to the OS.
}
Add digits
#include < stdio.h >
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
/*stores unit place digit to remainder*/
sum = sum + remainder;
n = n / 10;
/*dividing no to discard unit place digit*/
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
Add 'n' Number
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d",&value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}