program to print only even numbers from 1 to n using for loop ?


Q.) W.A.P to print only even numbers from 1 to n using for loop ?
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
  int i,n,sume=0;
   printf("enter the value of n :");
   scanf("%d",&n);
   for(i=1;i<=n;i++)
   {
      if(n%2==0)
      {
	      sume=sume+i;
      }
   }
     printf("sum of even no. from 1 to n is : %d",sume);
     getch();
 }
Output
enter the value of n :10    /* say n = 10 */.
sum of even no. from 1 to n is : 30

Program Explanation


Step 1: Include header files (#include<stdio.h> and #include<conio.h>).

Step 2: Start with main function with return type.

Step 3: parenthesis to start and end the program { }.

Step 4: declare variables with data type i.e, 'n' is an integer type so we use "int" data type. We also declare 'i' as "int" because it also has an integer value which we use for the iteration of the for loop. We declared 'sume' = 0 so that it won't take any garbage value while the execution of program also 'sume' is of integer type so it is also declared in "int".

Step 5: Use output function printf() to print the output on the screen.

Step 6: Use input function scanf() to get input from the user.

Step 7: A for loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied.

Step 8: We also created an if-block inside for loop, using arithmetic operator(i.e, %) and assignment operator(i.e, ==), to check the condition and perform the operation to find even no.

  • At first iteration, i is 1. The test condition (i<=n) is satisfied as (1<=n). i.e, say n = 10 which is greater than 1. Since this condition is satisfied, the control enters the if-block checks the condition and after the condition is satisfied it performs the operation (sume = sume + i), and comes out of the if-block, after the execution of the statement inside the if-block, the control comes out of the if-block and goes through the for-loop where the value of 'i' is incremented by 1 as of i++.
  • Now, the value of 'i' is 2. Again, the test condition (i<=n) is satisfied as (2<=n). Since the condition is satisfied, the control enters the if block inside the loop and checks the expression if the condition is satisfied, again it performs the operation inside if block(sume = sume + i), and comes out of the if-block, and goes through for loop and increments the value of 'i' by 1 as of i++.
  • Again, the same iteration the value of 'i' is 3, the test condition (i<=n) is satisfied as (3<=n). Hence, the control enters the if-block and checks the expression if the condition is satisfied it performs the operation inside the if-block and comes out of the if-block and again it goes through for loop and the value of 'i' is incremented by 1 as of i++.
  • Again and again, the same condition is checked and the process is repeated until the value of 'i' becomes greater than 'n', Once the value of 'i' becomes greater than 'n', it comes out of the loop and iteration stops.
  • After the iteration stops, it finally prints the statement given outside the loop.

Step 8: using getch() function to hold the screen and show the output on the screen.