Print squares of all even numbers up to a given value
Print squares of all even numbers up to a given value
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:
For more Practice: Solve these Related Problems:
- Write a C program to print the cubes of all odd numbers from 1 up to a user-specified limit.
- Write a C program to calculate and display the squares of numbers divisible by 3 up to a given value.
- Write a C program to compute the factorial of each even number within a specified range.
- Write a C program to print the powers of two for all numbers between 1 and a given upper limit.
Go to:
PREV : Sum all odd values among 5 inputs.
NEXT : Check if an integer is positive/negative and even/odd.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.