9Google AdSense

while example in C

#include <stdio.h>
#include <stdlib.h>

//print "Hello" n times

int main()
{
    int number,i=1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("Hello\n");
       i = i+1;
    }

    return 0;
}
==========================



==========================

#include <stdio.h>
#include <stdlib.h>

//print value of a variable n times

int main()
{
    int number,i=1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("value of i is %d\n", i );
       i = i+1;
    }

    return 0;
}


========================

#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n number

int main()
{
    int number,sum=0,i = 1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+1;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

==========================

#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n odd number

int main()
{
    int number,sum=0,i = 1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+2;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

==========================
#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n even number

int main()
{
    int number,sum=0,i = 2;

    scanf("%d",&number);

    printf("Calculate sum of even number between 2 to %d\n",number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+2;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

No comments: