Printing Even numbers to a number

Program to print all even numbers upto n

// Write a program to print all even numbers upto n
// Ex: 4 -> 0 2 4, 7-> 0 2 4 6
#include
int main(){
    int n,i;
    printf("Enter a integer:"); // input from user
    scanf("%d",&n);
    while(n<0){
         printf("Please enter positive integer only:");
         scanf("%d",&n);      
    }
    printf("Printing even numbers upto %d\n",n); // printing all even numbers
    for(i=0;i<=n;i=i+2){
         printf("%d ",i);
    }
    getch();
}

No comments:

Post a Comment