C program to find the squares of natural numbers using a do while statement-
Natural numbers are all the positive integers that span from 1 to ∞.
The Square of a number is the product of that integer with itself.
//C program to find the squares of natural numbers //using a do while statement #include <stdio.h> void printSquares(int n) { int sq = 1, i = 1; //Check number is positive and greater than 0 if (n < 1) { printf("Please enter number greater than 0!"); } // Calculate and print squares do { sq = i * i; printf("Square of %d = %d\n", i, sq); i++; } while (i <= n); } int main() { int num; printf(" Enter the number:"); scanf("%d", &num); printSquares(num); return 0; }
$ gcc square.c $ ./a.out Enter the number:-2 Please enter number greater than 0! $ ./a.out Enter the number:10 Square of 1 = 1 Square of 2 = 4 Square of 3 = 9 Square of 4 = 16 Square of 5 = 25 Square of 6 = 36 Square of 7 = 49 Square of 8 = 64 Square of 9 = 81 Square of 10 = 100 $ |
If you like this post C program to find the squares of natural numbers using a do while statement, please share your feedback. Thank you!
also see
C Programming language |
Go Programming language |
Linked List | Array |
Simplification | Queue |
DBMS | Reasoning |
Aptitude | HTML |