C Programming Examples

Here we covered basic C programming examples, like how to use datatype to create a program, how to take input from users, swapping two numbers without using a third variable, finding a person’s height, calculating simple interest, the area of a circle, etc., which will help the learner to learn the C programming language very easily. We can also provide PDF format to download that C program. To know What is C programming language?

C Programming Examples (PDF)
C Programming Examples Download (PDF)

Following are the list of basic C programming examples:

Table of Content
  1. Write a program to print Hello World.
  2. Program to find ASCII value of a given character.
  3. Add two Integer Number.
  4. Swap two number using third variable.
  5. Swap two number without using third variable.
  6. Calculate the Average of two number.
  7. Calculate the Cube of a number.
  8. Calculate the Area of a Circle.
  9. Calculate the Area of a Rectangle.
  10. Calculate the Area of a Square.
  11. Calculate the Simple Interest.
  12. Convert Meters into Kilometres.
  13. Convert Kilometres into Meters.
  14. Convert Miles into Kilometres.
  15. Convert Feet into Meters.
  16. Convert temperature from Fahrenheit to Celsius.
  17. Convert temperature from Celsius to Fahrenheit.
  18. Person height from Inches to Centimeter.
  19. Convert Kilogram to Gram.
  20. Convert Kilogram to Pound.
  21. Convert Gram to Pound.

1. Write a program to print Hello World.

Program
#include<stdio.h> void main() { printf("Hello World"); }
Output
Hello World

2. Program to find ASCII value of a given character.

Program
#include<stdio.h> void main() { char ch; printf("Enter a Character: "); scanf("%c", &ch); printf("ASCII value of %c is %d", ch, ch); }
Output
Enter a Character: Z ASCII value of Z is 90 Note: Take input from %c means character format and output is %d integer format.


3. Add two Integer Number.

Program
#include<stdio.h> void main() { int num1, num2, add; printf("Enter first number = "); scanf("%d", &num1); printf("Enter second number = "); scanf("%d", &num2); add = num1 + num2; printf("Addition is = %d", add); }
Output
Enter first number = 10 Enter second number = 22 Addition is = 32

4. Swap two number using third variable.

Program
#include<stdio.h> void main() { int num1, num2, temp; printf("Enter first number = "); scanf("%d", &num1); printf("Enter second number = "); scanf("%d", &num2); temp = num1; num1 = num2; num2 = temp; printf("After Swapping Value is - "); printf("\nFirst number = %d", num1); printf("\nSecond number = %d", num2); }
Output
Enter first number = 22 Enter second number = 65 After Swapping Value is - First number = 65 Second number = 22

5. Swap two number without using third variable.

Program
#include<stdio.h> void main() { int a, b; printf("Enter a first number = "); scanf("%d", &a); printf("Enter second number = "); scanf("%d", &b); a = a + b; b = a - b; a = a - b; printf("After Swapping Value is - "); printf("\nFirst number = %d", a); printf("\nSecond number = %d", b); }
Output
Enter a first number = 7 Enter second number = 3 After Swapping Value is - First number = 3 Second number = 7

6. Calculate the Average of two number.

Program
#include<stdio.h> void main() { int num1, num2; float avg; printf("Enter first number = "); scanf("%d", &num1); printf("Enter second number = "); scanf("%d", &num2); avg = (float)(num1 + num2)/2; printf("Average of %d and %d is: %f",num1,num2,avg); }
Output
Enter first number = 8 Enter second number = 3 Average of 8 and 3 is: 5.500000 Note: Here the result of the two numbers is converted into float datatype.

7. Calculate the Cube of a number.

Formula: Cube of Number = Number * Number * Number

Program
#include<stdio.h> void main() { int number; printf("Enter a number = "); scanf("%d", &number); number = number * number * number; printf("Cube of a number is = %d", number); }
Output
Enter a number = 5 Cube of a number is = 125

8. Calculate the Area of a Circle.

Formula: Area of Circle = 3.14 * Radius * Radius

Program
#include<stdio.h> void main() { float radius, area; printf("Enter the Radius of Circle = "); scanf("%f", &radius); area = 3.14 * radius * radius; printf("Area of Circle is = %f", area); }
Output
Enter the Radius of Circle = 5 Area of Circle is = 78.500000

9. Calculate the Area of a Rectangle.

Formula: Area of Rectangle = Length of Rectangle * Width of Rectangle

Program
#include<stdio.h> void main() { float length, width, area; printf("Enter Length of Rectangle = "); scanf("%f", &length); printf("Enter Width of Rectangle = "); scanf("%f", &width); area = length * width; printf("Area of Rectangle is = %f ", area); }
Output
Enter Length of Rectangle = 3.5 Enter Width of Rectangle = 6.8 Area of Rectangle is = 23.800001


10. Calculate the Area of a Square.

Formula: Area of Square = Side * Side

Program
#include<stdio.h> void main() { int side, area; printf("Enter the Side of Square = "); scanf("%d", &side); area = side * side; printf("Area of Square is = %d", area); }
Output
Enter the Side of Square = 4 Area of Square is = 16

11. Calculate the Simple Interest.

Formula: Simple Interest = (Principal Amount * Rate of Interest * Time) / 100

Program
#include<stdio.h> void main() { float principal_amount, time, rate, simple_interest; printf("Enter Principal Amount = "); scanf("%f", &principal_amount); printf("Enter Time = "); scanf("%f", &time); printf("Enter Rate of Interest = "); scanf("%f", &rate); simple_interest = (principal_amount * time * rate) / 100; printf("Simple Interest is = %f", simple_interest); }
Output
Enter Principal Amount = 3453 Enter Time = 2 Enter Rate of Interest = 6.9 Simple Interest is = 476.514038

12. Convert Meters into Kilometres.

Formula: Divide the length value by 1000.

Program
#include<stdio.h> void main() { float meter, km; printf("Enter the Distance (in meters) = "); scanf("%f", &meter); km = meter / 1000; printf("%.2f meters = %.2f Kilometres", meter, km); }
Output
Enter the Distance (in meters) = 786 786.00 meters = 0.79 Kilometres

13. Convert Kilometres into Meters.

Formula: Multiply the distance value by 1000.

Program
#include<stdio.h> void main() { float meter, km; printf("Enter the Distance (in kilometres) = "); scanf("%f", &km); meter = km * 1000; printf("%.2f km = %.2f meters", km, meter); }
Output
Enter the Distance (in kilometres) = 78.3 78.30 km = 78300.00 meters

14. Convert Miles into Kilometres.

Formula: Multiply the distance value by 1.60934.

Program
#include<stdio.h> void main() { float miles, kilometres; printf("Enter distance in Miles = "); scanf("%f", &miles); kilometres = miles * 1.60934; printf("%.2f Miles = %.2f Kilometre", miles, kilometres); }
Output
Enter distance in Miles = 43 43.00 Miles = 69.20 Kilometre

15. Convert Feet into Meters.

Formula: Divide the value by 3.281.

Program
#include<stdio.h> void main() { float feet, meter; printf("Enter distance in Feet = "); scanf("%f", &feet); meter = feet / 3.281; printf ("%.2f Feet = %.2f Meter”, feet, meter); }
Output
Enter distance in Feet = 45 45.00 Feet = 13.72 Meter

16. Convert temperature from Fahrenheit to Celsius.

Program
#include<stdio.h> void main() { float celsius,fahrenheit; printf("Enter Temperature in Fahrenheit = "); scanf("%f",&fahrenheit); celsius = (fahrenheit - 32)*5/9; printf("Celsius = %f",celsius); }
Output
Enter Temperature in Fahrenheit = 120 Celsius = 48.888889

17. Convert temperature from Celsius to Fahrenheit.

Program
#include<stdio.h> void main() { float celsius, fahrenheit; printf("Enter Temperature in Celsius = "); scanf("%f", &celsius); fahrenheit =((celsius*9)/5)+32; printf("Fahrenheit = %f", fahrenheit); }
Output
Enter Temperature in Celsius = 48.888889 Fahrenheit = 120.000000

18. Person height from Inches to Centimeter.

Formula: Multiply the length value by 2.54.

Program
#include<stdio.h> void main() { float inch, cm; printf("Enter length in Inch: "); scanf("%f", &inch); cm = inch * 2.54; printf("Equivalent length in Centimeters = %.2f", cm); }
Output
Enter length in Inch: 4 Equivalent length in Centimeters = 10.16

19. Convert Kilogram to Gram.

Formula: Multiply the weight by 1000.

Program
#include<stdio.h> void main() { float kg, gram; printf("Enter weight in Kilogram: "); scanf("%f", &kg); gram = kg*1000; printf("Equivalent weight in Gram = %.2f", gram); }
Output
Enter weight in Kilogram: 300 Equivalent weight in Gram = 300000.00

20. Convert Kilogram to Pound.

Formula: Multiply the weight by 2.205

Program
#include<stdio.h> void main() { float kg, pound; printf("Enter weight in Kilogram: "); scanf("%f", &kg); pound = kg*2.205; printf("Equivalent weight in pound = %.2f", pound); }
Output
Enter weight in Kilogram: 6 Equivalent weight in pound = 13.23

21. Convert Gram to Pound.

Formula: Divide the weight by 454

Program
#include<stdio.h> void main() { float gram, pound; printf("Enter weight in Gram: "); scanf("%f", &gram); pound = gram/454; printf("Equivalent weight in pound = %.2f", pound); }
Output
Enter weight in Gram: 260 Equivalent weight in pound = 0.57

Note:

  • All the programs are compiled and executed in Dev C++ code editor and save with .c extension.
  • %.2f is used to print fractional values only up to 2 decimal places. You can also use %f to print fractional values normally up to six decimal places.

More Topics

To Download C Programming Examples Click Here