9Google AdSense

For Loop Example

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

//print a statement n times using for

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

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
         printf("Welcome\n");
    }


    return 0;
}

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

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


//print a variable value n times

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

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
         printf("Value of i :%d\n",i);
    }


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



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

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


//print  loop variable value

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

    scanf("%d",&number);

    for(i=number;i>=1;i=i-1)
    {
         printf("value of i is %d\n",i);
    }


    return 0;
}

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

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


//Calculate sum of n number

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

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
        sum = sum + i;
    }

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

    return 0;
}


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

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


//Calculate sum of n even number

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

    scanf("%d",&number);

    for(i = 2;i<=number;i = i+2)
    {

        printf("%d,",i);
        sum = sum + i;
    }

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

    return 0;
}

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

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


//Calculate sum of n odd number

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

    scanf("%d",&number);

    for(i = 1;i<=number;i = i+2)
    {

        printf("%d,",i);
        sum = sum + i;
    }

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

    return 0;
}




No comments: