0% found this document useful (0 votes)
44 views9 pages

Simple C Programs

The document provides examples of C programs to perform basic calculations and operations. It includes programs to calculate the sum and average of numbers, area and perimeter of shapes, simple interest, temperature conversion, and other basic examples.

Uploaded by

AFL2 DLL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

Simple C Programs

The document provides examples of C programs to perform basic calculations and operations. It includes programs to calculate the sum and average of numbers, area and perimeter of shapes, simple interest, temperature conversion, and other basic examples.

Uploaded by

AFL2 DLL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Simple program structure -- > this is only for your understanding

#include <stdio.h>
void main()
{
//write variable declaration

//write statement to read input/ initialize variable directly

// compute the solution using logic/ formula

// display output
}

1. Write a C program to find the sum and average of given three numbers.
#include <stdio.h>
void main()
{
//declaration of variables as int
int a,b,c,sum,avg;

//To read three numbers from keyboard


printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);

//calculate sum and average of three numbers


sum=a+b+c;
avg=sum/3;

//display the result


printf("Sum of three numbers is %d\n",sum);
printf("Average of three numbers is %d\n",sum);
}

2. Write a C program to find the area and perimeter of rectangle.


#include <stdio.h>
void main()
{
//declaration the variables
float l,b, area, peri;
printf(“Enter length\n”);
scanf(“%f”,&l);
printf(“Enter Breadth\n”);
scanf(“%f”,&b);

//calculate area and rectangle


area=l*b;
peri=2*(l+b);

//display the result


printf("Area of rectangle is %f\n",area);
printf("Perimeter of rectangle is %f\n",peri);
}

3. Write a C program to find the area and perimeter of circle. Read the value of
radius from user
#include <stdio.h>
#define pi 3.142
void main()
{
//declaration the variables
float r, area, peri;

//read the value of radius from user (keyboard)


printf(“Enter the radius\n”);
scanf(“%f”,&r);

//calculate area and perimeter of circle


area=pi*r*r;
peri=2*pi*r;

//display the result


printf("Area of circle is %f\n",area);
printf("Perimeter of circle is %f\n",peri);
}

4. Write a C program to find the area and perimeter of triangle. Read the input from
user.
void main()
{
//declaration the variables
float a,b,c,s area, peri;
//read the three side of a triangle from user (keyboard)
printf("Enter the three side of a triangle\n");
scanf("%f%f%f",&a,&b,&c);

//calculate area and perimeter of triangle


s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
peri=a+b+c;

//display the result


printf("Area of triangle is %f\n",area);
printf("Perimeter of triangle is %f\n",peri);
}

5. Write a C program to find the simple interest: SI= (P*T*R)/100.


#include <stdio.h>
void main()
{
//declaration the variables
float p, t, r, si;

//read the principle, time and rate from user (keyboard)


printf("Enter principle amount\n");
scanf("%f",&p); //To read input from keyboard use
printf("Enter Time\n"); printf(“enter the value for p,t,r\n”);
scanf("%f",&t); scanf(“%f%f%f”,&p,&t,&r);
printf("Enter Rate\n");
scanf("%f",&r);

//calculate simple interest


si=(p*t*r)/100;

//display the result


printf("Simple Interest: %f\n",si);
}
6. Write a C program to Convert Fahrenheit to Celsius: C=(f-32)*0.555
#include <stdio.h>
void main()
{
//declaration the variables
float C, F;
//read the priciple, time and rate from user (keyboard)
printf("Enter temperature in Fahrenheit\n");
scanf("%f",&F);

//Convert Fahrenheit to Celsius


C=(F-32)*0.555;

//display the result


printf("%f Fahrenheit = %f Celsius\n",F,C);
}

7. Write a C program to Exchange(Swap) the two numbers


#include <stdio.h>
void main()
{
//declaration the variables
int a, b, temp;
//read two numbers from user (keyboard)
printf("Enter the value of a and b\n");
scanf("%d%d",&a,&b);

printf("Before exchange\n");
printf("a=%d\t b=%d\n",a,b);

//exchange two numbers using temporary variable


temp=a; //assign value of a to temp variable
a=b; //assign value of b to a variable
b=temp; //assign value of temp to b variable

//display the result


printf("After exchange\n");
printf("a=%d\t b=%d\n",a,b);;
}
8. Write a C program to find the ASCII value of a character
In C programming, a character variable holds ASCII value (an integer number between 0 and
127) rather than that character itself. This integer value is the ASCII code of the character.

#include <stdio.h>
void main()
{
char ch;
printf(“enter the character\n”);
ch=getchar(); // or scanf("%c",&ch);
printf(“ASCII value of %c is %d\n”,ch,ch);
}

9. Write a C program to Program to Compute Quotient and Remainder (May 2019)


#include <stdio.h>
void main()
{
int a, b, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &a);
printf("Enter divisor: ");
scanf("%d", &b);

// Computes quotient
quotient = a / b;

// Computes remainder
remainder = a % b;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);
}

10. Write a C program to find the Size of Variables


#include<stdio.h>
void main()
{
int a;
float b;
double c;
char d;
// sizeof evaluates the size of a variable
printf("Size of int: %d bytes\n", sizeof(a));
printf("Size of float: %d bytes\n", sizeof(b));
printf("Size of double: %d bytes\n", sizeof(c));
printf("Size of char: %d byte\n", sizeof(d));
}

11. Write a C program to find the product of two floating-point numbers entered by the
user is calculated and printed on the screen.
#include <stdio.h>
void main()
{
float a, b, product;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);

// Calculating product
product = a * b;

// Result up to 2 decimal point is displayed using %.2lf


printf("Product = %.2f", product);
}

Output:
Enter two numbers: 2.4
1.12
Product = 2.69

12. C Program to accept a three digit number and find the sum of all the digits of
numbers using only arithmetic operators. (SEE Dec2018)

# include <stdio.h>
void main( )
{
int a,b,c,n,sum;
printf (“Enter a Three Digit Number:“);
scanf (“%3d”,&n);

a=n/100;
b=((n%100)/10);
c=n%10;
sum=a+b+c;

printf(“ Sum of Individual Digits of Given Numbers


is %d”, sum);
}

13. Write a program to accept two numbers of two digits each and find the sum and
difference of unit place digits and tens places digits separately of the numbers.
#include <stdio.h>
void main()
{
int n1, n2, sum1,sum2,a1,a2,b1,b2;
printf("Enter a two 2-Digit Numbers:");
scanf("%2d %2d",&n1,&n2);

/* a1,a2 is unit's place digit


b1,b2 is ten's place digit
*/
a1=n1/10;
b1=n1%10;

a2=n2/10;
b2=n2%10;

/* sum1: sum of unit's place digits of two numbers


sum2: sum of ten's place digits of two numbers
*/
sum1=a1+a2;
sum2=b1+b2;
printf("Sum of unit's place: %d\n",sum1);
printf("Sum of ten's place: %d",sum2);
}
14.Write a C program to find the hypotenuse of triangle. (or) Write a C program to
implement Pythagoras theorem.

#include <stdio.h>
#include <math.h>
void main()
{
float c,a,b;
printf("Enter the two sides of triangle\n");
scanf("%f%f",&a,&b);
c=sqrt((a*a)+(b*b));
printf("Hypotenuse of trinagle is %.2f",c);
}

15.Write a C program to convert days to months.

#include <stdio.h>
void main()
{
int days, m,d;
printf("Enter the number of days[min 30 days]\n");
scanf("%d",&days);
m=days/30;
d=days%30;
printf("%d month %d days\n",m,d);
}
16.Write a C program to convert a negative number to a positive number.

#include <stdio.h>
void main()
{
int n,p;
printf("Enter the negative number\n");
scanf("%d",&n);
p=n*-1; //multiply negative number by -1
printf("Positive number=%d\n",p);
}

17.Write a C program to find square root of a number.

#include <stdio.h>
#include <math.h>
void main()
{
float n,res;
printf("Enter the number\n");
scanf("%f",&n);
res=sqrt(n);
printf("Square root of %.2f is %.2f\n",n,res);
}

18.Write a C program to print last digit of a number.

#include <stdio.h>
void main( )
{
int last_digit, n,sum;
printf (“Enter a Number:“);
scanf (“%d”,&n);

last_digit=n%10;

printf(“ Last Digit of Given Numbers is %d”, last_digit);


}

You might also like