BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to print cube of a number upto an integer

Last Updated: May 18, 2024 by Chaitanya Singh | Filed Under: C Programs

In this article, we will write a C Program to print cube of a number upto an integer. For example, if the user enters a number 2 then program should print the cube of each number from 1 till the entered number. The output should look like this:

Enter an integer: 2
Cube of 1 is 1
Cube of 2 is 8

C Program to display cube of a number upto a given integer

The explanation of the program is provided at the end of this code.

#include <stdio.h>

int main() {
//n: is the number till which the cube of every number is printed
//i: is the loop counter variable
int n, i;

// Prompt the user to enter an integer
printf("Enter an integer: ");
scanf("%d", &n);

// Loop to calculate and print cube of every number from 1 to n
for(i = 1; i <= n; i++) {
printf("Cube of %d is %d\n", i, i * i * i);
}

return 0;
}

Output:

Enter an integer: 7
Cube of 1 is 1
Cube of 2 is 8
Cube of 3 is 27
Cube of 4 is 64
Cube of 5 is 125
Cube of 6 is 216
Cube of 7 is 343

Explanation of the program:

  1. C header files: In this program, we have included only one header file <stdio.h>, which is required for using the printf and scanf functions.
  2. Main Function: The main functions contains the following statements.
    • Variable Declaration: Here, we have two variables, n and i. The variable n is to hold the user entered number and i is the loop counter variable.
    • User Input: The program prompts the user to enter an integer using printf. The scanf function reads the input and stores it in the variable n.
    • Loop to Calculate Cubes: A for loop iterates from 1 to n. Inside the loop, the cube of the current number i is calculated as i * i * i and printed using printf.
    • Return Statement: The return 0 statement represents the successful execution of the program.

Top Related Articles:

  1. C Program to find Palindrome numbers in a given range
  2. C Program to read and print employee details using structure
  3. C Program to Read the First Line From a File
  4. C Program to concatenate two strings without using strcat
  5. C Program to print current date and time

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap