Generate an even squares between 1 to n using for loop in C Programming

CServer Side ProgrammingProgramming

Even square numbers are - 22, 42, 62, 82,………

 = 4, 16, 36, 64, 100, ………

Algorithm

START
Step 1: declare two variables a and n
Step 2: read number n at runtime
Step 3: use for loop to print square numbers
        For a=2; a*a<=n;a+=2
        until the condition satisfy loop will continue and
        Print a*a
STOP

Program 1

 Live Demo

#include<stdio.h>
int main(){
   int a,n;
   printf("enter a number for n:");
   scanf("%d",&n);
   for(a=2;a*a<=n;a+=2) //print even squares that are present in between 1 and n{
      printf("%d\n",a*a);
   }
   return 0;
}

Output

enter a number for n:200
4
16
36
64
100
144
196

Program 2

Following is the program to find even cubes between 1 to n −

 Live Demo

#include<stdio.h>
int main(){
   int a,n;
   printf("enter a number for n:");
   scanf("%d",&n);
   for(a=2;a*a*a<=n;a+=2){
      printf("%d\n",a*a*a);
   }
   return 0;
}

Output

enter a number for n:300
8
64
216
raja
Published on 05-Mar-2021 16:24:37
Advertisements