Simple C Programs for Beginners with Output

We will write simple C programs using the knowledge of data-types, operators, printf and scanf functions.

Prerequisites for writing all below programs:-

Table Of Contents

Simple C programs on conversions

Here we will develop simple C programs on conversions like inches to the centimeter, kilograms to grams.

C Program to Convert Inches to Centimetre

Program description:- Write a C program to convert inches to the centimeter. Take input or inches from the end-user using the scanf function.

Formula: 1 inch = 2.54 cm
To convert inches to centimeters, multiply the number of inches by 2.54, which is how many centimeters are in 1 inch. Then, just swap out the word “inches” for “centimeters”

#include<stdio.h>
int main()
{
   double inch, cm;

   printf("Enter Inches: ");
   scanf("%lf",&inch);

   cm = 2.54*inch;
   printf("Centimeters= %.2lf\n",cm);

   return 0;
}

Output for different test-cases:-

Enter Inches: 10
Centimeters=25.40

Enter Inches: 12.5
Centimeters=31.75

C Program to Convert Kilograms to Grams

Program description:- Write a C program to convert kilograms to grams. Take input or kilograms from the end-user using the scanf function.

Formula: 1Kg = 1000 grams
Once we have 1,000 grams, we have 1 kilogram. A dictionary has a mass of about one kilogram.

#include<stdio.h> 
int main()
{
   double kg, gm;

   printf("Enter Kilograms: ");
   scanf("%lf",&kg);

   gm = 1000*kg;
   printf("Grams=%.2lf\n",gm);

   return 0;
}

Output for different test-cases:-

Enter Kilograms: 5
Grams=5000.00

Enter Kilograms: 3.95
Grams=3950.00

Simple C Programs for Finding the Circumference

Now we will develop simple C programs for finding the circumference of the circle, rectangle, and triangle.

C Program to Find the Circumference of a Circle

Program:- Write a program to find the circumference of a circle. Take the radius of the circle as input from the user.

The formula for, the circumference of the circle = 2πr; where r is the radius.

#include<stdio.h> 
#define PI 3.14
int main()
{
   float radius, area;

   printf("Enter the radius of the circle: ");
   scanf("%f",&radius);

   area = 2 * PI * radius;
   printf("Area of the circle having radius %.2f = %.2f",
                                         radius, area );

   return 0;
}

Output:-

Enter the radius of the circle: 10
Area of the circle having radius 10.00 = 62.80

The circumference of the circle is given as 2πr, where r is the radius of the circle. So, we define a macro PI which is before the main function. Now, whenever PI is encountered in the program then it will replace with 3.14

The radius of the circle is stored in the variable “circle” and the circumference is calculated as 2*PI*radius.

C Program to Find the Circumference of a Triangle

Program:- Write a program to find the circumference of a triangle. Take all the sides of the triangle as input from the user.

The formula for, the circumference of the triangle = a+b+c; where a, b, and c are sides of the triangle.

#include<stdio.h>
int main()
{
  float a, b, c, result;

  printf("Enter three sides of the triangle: ");
  scanf("%f %f %f", &a, &b, &c);

  result = a + b + c;
  printf("circumference = %.2f", result);

  return 0;
}

Output:-

Enter three sides of the triangle: 9 15 22
circumference = 46.00

The circumference of the triangle is given as the sum of all three sides. In this program, we stored sides of the triangle using scanf() in the variables a, b, and c. Now, the circumference will be a+b+c.

Simple C Programs to Find the Circumference of a Rectangle

Program:- Write a program to find the circumference of a rectangle. Take the length and width of the rectangle as input from the user.

The formula for circumference of a Rectangle = 2*(length + width).

#include<stdio.h>
int main()
{
  float len, wid, result;

  printf("Enter length and width of rectangle: ");
  scanf("%f %f", &len, &wid);

  result = 2*(len + wid);
  printf("Circumference = %.2f", result);

  return 0;
}

Output:-

Enter length and width of rectangle: 20 15
Circumference = 70.00

The circumference of the triangle is given as 2*(length + width). In this program, we stored length and width value in the variables len and wid using scanf() function. Now, the circumference is calculated as 2*(len+wid) and the result is stored in the variable “result”.

Simple C Programs on Finding the Volume

Let’s develop some simple C programs on finding the volume of a cube, rectangular box, and cylinder.

C Program to Find the Volume of a Cube

Program description:- Write a C program to find the volume of a cube by accepting the length of the side.

Formula,
The Surface area of a cube, A=6*a*a;
where a is the length of the side.

Since a cube has six faces, all you have to do is calculate the area of one face and multiply by 6 to find the total surface area. The mathematical formula that results from this discussion is: For a cube with sides of length L, the surface area A = 6*L*L

#include<stdio.h>
int main()
{
  double length, area;

  printf("Enter length: ");
  scanf("%lf",&length);

  area = 6*length*length;
  printf("Area=%.2f",area);

  return 0;
}

Output for different test-cases:-

Enter length: 12.5
Area=937.50

Enter length: 10
Area=600.00

C Program to Find the Volume of a Cube Using pow() Function

The pow() function defines inside “math.h” and it returns the power of a number.

#include<stdio.h>
#include<math.h>
int main()
{
  double length, area;

  printf("Enter length: ");
  scanf("%lf",&length);

  area = 6*pow(length,2);
  printf("Area=%.2f",area);

  return 0;
}

If you are using Linux/Unix and getting an error like undefined reference to `pow' even included math.h then read this, this will solve your problem Undefined reference to sqrt (or other mathematical functions) even includes math.h header

C Program to Find Volume of a Rectangular Box

Program description:- Write a C program to find the volume of a rectangular box by accepting length, breadth, and height.

Formula:- V = w*h*l
Where l is the length, w is the width and, h is the height of the rectangular box.

The volume of a rectangle equals its length * width * height. If the box is a rectangular prism or a cube, the only information we need is the box’s length, width, and height. We can then multiply them together to get volume. This formula is often abbreviated as V = l*w*h.

#include<stdio.h>
int main()
{
  double len, wid, height, volume;

  printf("Enter length, width, and height: ");
  scanf("%lf %lf %lf",&len, &wid, &height);

  volume = len * wid * height;
  printf("Volume=%.2f", volume);

  return 0;
}

Output for different test-cases:-

Enter length, width, and height: 10 12 15
Volume=1800.00

Enter length, width, and height: 2.3 5.5 3.9
Volume=49.33

C Program to Find Area and Volume of a Cylinder

Program Description:- Write a C program to find the area and volume of a cylinder by accepting radius and height.

Formula:-
V = area * h
Or, V = π*r²*h

A cylinder is a three-dimensional object with two round flat bases and one curved side. It has a curved surface in the middle. The base and the top surface are identical. That means the bases are always parallel and congruent to each other. It has no vertices.

To find the volume of a cylinder, Calculate the area of the base (which is a circle) by using the equation πr² where r is the radius of the circle. Then, multiply the area of the base by the height of the cylinder to find the volume.

#include<stdio.h>
int main()
{
  double height, radius, area, volume;

  printf("Enter radius and height: ");
  scanf("%lf %lf",&radius, &height);

  area = 3.14 * (radius* radius);
  volume = area * height;

  printf("Area=%.2f \t Volume=%.2f", area, volume);

  return 0;
}

Output for different test-cases:-

Enter radius and height: 10 10
Area=314.00 Volume=3140.00

Enter radius and height: 15 12
Area=706.50 Volume=8478.00

Three-Digit Integer Number in Reverse Order

Problem description:- Write a C program to display a three-digit integer number in reverse order without using the loop.

For example, if the integer number is 234 then it’s reverse is 432. Here we will write a C program to display only for a three-digit integer number. After the learning loop statements (for loop, while loop), you can reverse any number with any length.

#include<stdio.h>
int main()
{
  int n = 487;

  printf("%d reverse is %d",n, n%10);
  n = n/10;

  printf("%d%d",n%10,n/10);

  return 0;
}

Output:-

487 reverse is 784

In the above program, number%10 gives the last digit of the number and number/10 removes the last digit of the number. To reverse the number first we find the last digit of the number and display it. Then, we remove the last digit, again find the last digit, and number/10 give the last digit because it is only a three-digit number. Finally, the reverse of the number is displayed on the screen.

C Program to Display Equivalent Octal and Hexadecimal Value

Program Description:- Write a C program to display equivalent octal and hexadecimal value. Enter an integer number and display its equivalent value in octal and hexadecimal.

#include<stdio.h>
int main()
{
  int num;

  printf("Enter a number: ");
  scanf("%d",&num);

  printf("Octal Value = %o\n",num);
  printf("Hexadecimal Value = %X\n",num);

  return 0;
}

Output for different test-cases:-

Enter a number: 10
Octal Value = 12
Hexadecimal Value = A

Enter a number: 150
Octal Value = 226
Hexadecimal Value = 96

Octal and Hexadecimal values are displayed by using conversion characters %o and %X. Octal is displayed by %o. Hexa-decimal is given by using %x or %X. %x converted into the small case but %X converted to capital case. For example, %x converted 2748 as “abc” and %X converted to “ABC”.

Finding the Number of Conversion Characters in scanf Function

The scanf() function returns the number of input fields that are successfully scanned and stored. For example, if you read one integer 10, one double 9.5 and one character ‘a’ from console then scanf() returns integer value 3.

Finding the number of conversion characters in scanf()

#include<stdio.h>
int main()
{
  int a, b, c;
  printf("Enter two integer number:");
  c = scanf("%d %d", &a, &b );

  printf("a=%d, b=%d, c=%d\n", a, b, c);
  return 0;
}

Output:-

Enter two integer number:10 20
a=10, b=20, c=2

In this program, we took three variables a, b, and c. Variables a and b stores the input value from the console using scanf() function and later the function returns the number of input entered on the console to the variable c, Then the variable c stores the number of input from the console.

Simple C Programs to Find the Total Cost of the Vehicle

Program Description:- Write a program to calculate the total cost of the vehicle by adding cost with:
a. Excise duty (15%)
b. Sales tax (10%)
c. Octori (5%)
d. Road tax (1%)
Input the basic cost

#include<stdio.h>
int main()
{
  double amount, tax, cost;
  int exciseDuty, salesTax, octori, roadTax;
  exciseDuty=15, salesTax=10, octori=5, roadTax=1;

  printf("Enter total amount: ");
  scanf("%lf",&amount);
  tax = amount*(exciseDuty+salesTax+octori+roadTax)/100;
  cost = amount + tax;

  printf("Total Cost = %.lf",cost);

  return 0;
}

Output for different test-cases:-

Enter total amount: 25000
Total Cost = 32750

Enter total amount: 95500
Total Cost = 125105

Find out the Age in Terms of Years, Months and Days

Problem description:- Read the age in the number of days and find out the age in terms of years, months, and days.

Example:- If input (age in the number of days) is 100 days then the output should be 0 years, 3 months and 1 day.

For simplicity use:-
1 year = 365 days, and
1 month = 30 days

Don’t include leap year and, 28, 29 & 31 days in a month. And also don’t use loops and conditional statements.

#include<stdio.h>
int main()
{
  int totalday, year, month, day;
  int rem;

  printf("Enter the age in days: ");
  scanf("%d",&totalday);

  year = totalday / 365;
  rem = totalday % 365;

  month = rem / 30;
  day = rem % 30;

  printf("The age is %d year %d month %d days\n", 
                year, month, day);

  return 0;
} 

Output for different test-cases:-

Enter the age in days: 500
The age is 1 year 4 month 15 days

Enter the age in days: 1012
The age is 2 year 9 month 12 days

Explanation:- In this program, we take five variables. The variable totalday stores the age in terms of days entered from the user. The year is simply calculated as totalday / 365, and it is stored in the variable “year”. For calculating month and days, we need the remaining day and it is calculated as totalday%365. Now, months and days are calculated as rem/30 and rem%30.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply