Summation between 1-100 using c programming.

Table of content :

  1. Summation between 1-100
  2. Summation all even number between 1-100
  3. Summation all odd number between 1-100
  4. Summation all even and odd number between 1-100

1) Summation between 1-100

For summation between 1-100 we need a loop and a variable which will initialize by 0
And this variable will update when loop will continue. Code given below

#include<stdio.h>

int main()
{
    int sum = 0;
    int i;
    for(i=0; i<=100; i++)
    {
       sum = sum+i;
    }
    printf("SUM:%d",sum);
    return 0;
}

Output :
SUM:5050

Loop Worked like below

When i = 0   sum: 0

When i = 1   sum: 1

When i = 2   sum: 3

When i = 3   sum: 6

When i = 4   sum: 10

When i = 5   sum: 15

………..

……….

When i = 100  sum: 5050
 

2) Summation all even number between 1-100

We already able to summation between 1-100. Did you noticed how loop work ? see below
 

for(i=0; i<=100; i++)

Here i++ means i=i+1 , That means “i” will always increment by one. Loop worked like
0,1,2,3,4,5,6,7……….100.

This values  0,1,2,3…..100 has added with value sum. Then we got output for samutation between 1-100.


Now for Summation all even number between 1-100, we need all even number from loop. So, our loop should be
0,2,4,6…….100. Like that. So, If we increment value of “i” by 2, our job will done. Let’s see the example .
 

#include<stdio.h>

int main()
{
    int sum = 0;
    int i;
    for(i=0; i<=100; i=i+2)
    {
       sum = sum+i;
    }
    printf("SUM:%d",sum);
    return 0;
}

Output :
SUM:2550

Loop Worked like below

When i = 0   sum: 0

When i = 2   sum: 2

When i = 4  sum: 6

When i = 6   sum: 12

………..

……….

When i = 100  sum: 2550
 

3) Summation all odd number between 1-100

For summation all odd number between 1-100 we just need start out loop from 1 and increment “i” by 2. So, if we start our loop from one and increment “i” by 2 loop will run like below.
1,3,5,7……..99

So , we can write the program like below

#include<stdio.h>

int main()
{
    int i;
    int sum = 0;
    for(i=1;i<=100;i=i+2)
    {
         sum = sum+i;
    }
    printf("%d",sum);
    return 0;
}

Output :
SUM:2500

Loop Worked like below

When i = 1   sum: 1

When i = 3   sum: 4

When i = 5   sum: 9

When i = 7   sum: 16

………..

……….

When i = 99  sum: 2500

4) Summation all even and odd number between 1-100 

For summation all even and odd number between 1-100, we need a knowledge about modulus. So, what a modulus do ? See below example of modulus

8%2 = 0

7%2 = 1

Modulus actually give us remainder. So, If we modulus any even number by 2, we will always get remainder 0 and if we modulus any odd number by 2 we will always get remainder 1. So, we can write below program to get summation all even and odd number between 1-100.

#include<stdio.h>

int main()
{
    int even = 0,odd = 0;
    int i;
    for(i=0; i<=100; i++)
    {
       if(i%2==1)
         odd = odd+i;
       else
         even = even+i;
    }
    printf("Odd:%d\n",odd);
    printf("Even:%d",even);
    return 0;
}

Output :

Odd:2500

Even:2550

Loop Worked Like below

i:0, odd:0, even:0
i:1, odd:1, even:0
i:2, odd:1, even:2
i:3, odd:4, even:2
i:4, odd:4, even:6
i:5, odd:9, even:6
i:6, odd:9, even:12
i:7, odd:16, even:12
i:8, odd:16, even:20
i:9, odd:25, even:20
.
.
i:99, odd:2500, even:2450
i:100, odd:2500, even:2550

 

Leave a Reply

Your email address will not be published. Required fields are marked *