C Exercises: Print the square of each one of the even values from 1 to a specified value
C Basic Declarations and Expressions: Exercise-30 with Solution
Write a C program to find and print the square of all the even values from 1 to a specified value.
C Code:
#include <stdio.h>
int main() {
int x, i; // Declare variables for user input and loop counter
printf("Input an integer: ");
scanf("%d", &x); // Prompt user for an integer
printf("List of square of each one of the even values from 1 to a %d :\n", x);
for(i = 2; i <= x; i++) { // Loop through numbers from 2 to x
if((i%2) == 0) { // Check if the number is even
printf("%d^2 = %d\n", i, i*i); // Print the square of the even number
}
}
return 0;
}
Sample Output:
List of square of each one of the even values from 1 to a 4 : 2^2 = 4 4^2 = 16
Flowchart:
C Programming Code Editor:
Previous: Write a C program that read 5 numbers and sum of all odd values between them.
Next: Write a C program to check a given integer is positive even, negative even, positive odd or negative odd. Print even if the number is 0.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/basic-declarations-and-expressions/c-programming-basic-exercises-30.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics