Accept any 3 numbers and find their squares and cubes.
This is one of the easiest C program. All you have to do is, accept 3 numbers and display their squares and cubes.
Tutorial
Steps
To find Squares and Cubes of 3 numbers
- Display a message to the user and accept 3 numbers. Store these values in three variables n1, n2 and n3.
- Since, we can display the results using printf() statements that is why no need to declare any extra variables to store the results. That’s it. 😎
Flow Chart 🌻
Check how to draw the flow chart for this program.
Code 💻
#include <stdio.h>
#include <conio.h>
int main()
{
float n1=0, n2=0, n3=0;
printf("Please enter 3 numbers:\n");
scanf("%f%f%f", &n1, &n2, &n3);
// Square and Cube of n1
printf("\n\nSquare of n1: %0.2f", n1 * n1);
printf("\n\nCube of n1: %0.2f", n1 * n1 * n1);
// Square and Cube of n2
printf("\n\nSquare of n2: %0.2f", n2 * n2);
printf("\n\nCube of n2: %0.2f", n2 * n2 * n2);
// Square and Cube of n3
printf("\n\nSquare of n3: %0.2f", n3 * n3);
printf("\n\nCube of n3: %0.2f", n3 * n3 * n3);
return 0;
}
Example 😍
Let’s assume that the user enters 3 numbers as 2, 4 and 6. As we know, it is displaying results directly through printf() statements without any extra variables.
First, it will display the squares and cubes of 2 i.e. 4 and 8. Next, it will display the squares and cubes of 4 i.e. 16 and 64. Finally, it will display the squares and cubes of 6 i.e. 36 and 216.
The output will be like this:
Square of n1: 4
Cube of n1: 8
Square of n2: 16
Cube of n2: 64
Square of n3: 36
Cube of n3: 216
This is how we find out the squares and cubes of any 3 numbers. For more C Programs, click here.
Comments