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;
}

If-Else Example

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

//checking student passed or failed in exam
int main()
{
    int mark;
    scanf("%d",&mark);
    if(mark>=50)
        printf("You are passed");
    else
        printf("You are failed");

    return 0;
}

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

//checking student grade
int main()
{
    int mark;
    scanf("%d",&mark);
    if(mark>=80)
        printf("Grade : A+");
    else if(mark>=70)
        printf("Grade: A");
    else if(mark>=60)
        printf("Grade: A-");
    else if(mark>=50)
        printf("Grade: B");
    else if(mark>=40)
        printf("Grade: B-");
    else
        printf("Grade: Fail");

    return 0;
}

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

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

//check a number is even or odd

int main()
{
    int number;
    scanf("%d",&number);
    if(number == 0)
        printf("Given number is zero");
    else if(number%2==0)
        printf("%d is a even number",number);
    else
        printf("%d is a odd number",number);
    return 0;
}

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



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

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

//check a number is positive or negative

int main()
{
    int number;
    scanf("%d",&number);
    if(number == 0)
        printf("Given number is zero");
    else if(number>0)
        printf("%d is a positive number",number);
    else
        printf("%d is a negative number",number);
    return 0;
}

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

//voting age check

int main()
{
    int age;
    printf("Enter Your Age :");
    scanf("%d",&age);
    if(age>=18)
    {
         printf("you can vote");
    }
    else
    {
         printf("you are not Eligible for voting");
    }
    return 0;
}

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;
}




Write a program to find the sum of first n natural numbers where n is entered by user (For-Loop)

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


int main()
{
    int number, sum = 0, i;
    printf("Enter a number: ");
    scanf("%d",&number);

    for(i=1;i<=number;i=i+1)
    {
        printf("%d(sum)",sum);
        sum = sum + i;
        printf(" + %d(i) = %d(sum)\n",i,sum);

    }
    printf("Sum: %d",sum);


    return 0;
}


Output:

check whether a number entered by user is even or odd in C using If-Else

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

//Write a C program to check whether a number entered by user is even or odd
int main()
{
    int number;
    printf("Enter a number :");
    scanf("%d",&number);

    if(number%2==0)
        printf("%d is a even number",number);
    else
        printf("%d is a odd number",number);


    return 0;
}

Output:



Checking a number is postive or negative in C using If-Else

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

int main()
{
    int number;
    //number = 50;

    scanf("%d",&number);

    if(number == 0)
        printf("Given number is %d",number);
    else if(number>0)
        printf("%d is a Positive number",number);
    else
        printf("%d is a Negative number",number);

    return 0;
}

Output: