#include <stdio.h>int main(){ int num, cube; // Asking for Input printf("Enter an integer: "); scanf("%d", &num); cube = num * num * num; printf("Cube of %d is %d", num, cube); return 0;
Question
#include <stdio.h>int main(){ int num, cube; // Asking for Input printf("Enter an integer: "); scanf("%d", &num); cube = num * num * num; printf("Cube of %d is %d", num, cube); return 0;
Solution
This is a simple C program that calculates the cube of a given integer. Here's a step-by-step explanation:
-
#include <stdio.h>: This line includes the standard input/output library in C. This library allows the program to perform input and output operations. -
int main(): This is the main function where the program execution begins. -
int num, cube;: Here, two integer variables 'num' and 'cube' are declared. -
printf("Enter an integer: ");: This line prints the message "Enter an integer: " on the console. -
scanf("%d", &num);: This line takes an integer input from the user and stores it in the variable 'num'. -
cube = num * num * num;: This line calculates the cube of the number entered by the user by multiplying the number by itself twice and stores the result in the variable 'cube'. -
printf("Cube of %d is %d", num, cube);: This line prints the cube of the number on the console. -
return 0;: This line signifies that the program has executed successfully and it returns 0 to the operating system. -
The program is enclosed in
{}brackets which define the body of the main function.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.