C Programming language

adplus-dvertising
C Program example: Print all even numbers from 1 to 100
Previous Home Next
Print all even numbers from 1 to 100

Here we are including two header files <stdio.h> and <conio.h>. To include these files we are using #include<stdio.h> and #include<conio.h> .Here we are using one predefined function. That is main() method. The return type of this function is void .That mean this method does not return any value. This method is necessary to execute any C program. Nearly all languages must have a main() method which execute first by complier.

In this we are using three more methods. These are clrscr(), printf("") and getch(). The clrscr() method is used to clear the screen ,the printf ("") is used to print messages or values on screen e.g. in this example printf("Print all even numbers from 1 to 100\n"); is used to print Print all even numbers from 1 to 100 on screen. In the printf() method we are also using \n for new line. This mean the next value will print at new line. The getch() method is used to get character from keyboard.

In this we are using for loop to iterate from 1 to 100 and if is used to check the condition (values from 1 to 100 is divisible by two or not).

Here in example we are
  • Declaring int i as integer type.
  • Then we call clrscr() function to clear the screen.
  • Then print the messages and using for loop iterate from 1 to 100.
  • In for loop we also checking the divisibility by two by checking modulo is equals to Zero or not.
  • As we know when any number is divisible by two then its remainders Zero . And modular operator is used to check remainder. So we are checking the remainder in if (i%2==0). If it is Zero it mean number is drivable by 2 (So print the number) otherwise the number is not divisible by 2.
/*Print all even numbers from 1 to 100 */

#include<stdio.h>
#include<conio.h>
void main()
{
	int i;
	clrscr();
	printf("Print  all even numbers from 1 to 100\n");
	printf("Even Numbers are:\n");
	for(i=1;i<=100;i++)
	{
		if(i%2==0)
		printf("%d ",i);
	}
	getch();
}
Output:

Print all even numbers from 1 to 100

Even Numbers are:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Previous Home Next