C Program To Find Sum of All Even Numbers Between Range, using For loop

Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user, using for loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 14 % 2 == 0. When we divide 14 by 2, it gives a reminder of 0. So number 14 is an even number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end If start value is greater than value present in end, we swap the values of variable start and end.

If user enters start = 5 and end = 14. C program finds all the even numbers between 5 and 14, including 5 and 14. So the even numbers are 6, 8, 10, 12, 14. We add all these even numbers and output the sum to the console window. i.e., 6 + 8 + 10 + 12 + 14 = 50. We out put the value 50 as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers

You can also watch C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers Between Range, using For loop


[youtube https://www.youtube.com/watch?v=9nKzyNnM2CY]

YouTube Link: https://www.youtube.com/watch?v=9nKzyNnM2CY [Watch the Video In Full Screen.]

Logic To Find Sum of All Even Numbers Between Range, using For loop

Step 1: We ask the user to enter start and end value.

Step 2: We initialize count to start and iterate through the for loop until value of count is less than or equal to value of variable end. For each iteration of for loop count value increments by 1.

Step 3: For every iteration we check if value present in variable count is a even number. i.e., count % 2 == 0. If this condition is true, then we add the value present in variable count to the previous value of variable sum.

Step 4: Once the control exits for loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Even Numbers Between Range, using For loop

#include<stdio.h>

int main()
{
    int start, end, temp, count, sum = 0;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);
    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }
    }

    printf("Sum of all the even numbers from %d to %d is %d\n", start, end, sum);

    return 0;
}

Output 1:
Enter start and end values
5
14
Even numbers between 5 and 14 are:
6
8
10
12
14
Sum of all the even numbers from 5 to 14 is 50

Output 2:
Enter start and end values
23
14
Even numbers between 14 and 23 are:
14
16
18
20
22
Sum of all the even numbers from 14 to 23 is 90

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Sum of All Odd Numbers Between Range, using For loop

Lets write a C program to find sum of all odd numbers between range or between 2 integers input by the user, using For loop.

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 15 % 2 != 0. When we divide 15 by 2, it does not give a reminder of 0. So number 15 is an odd number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end If not, we swap the values of variable start and end.

If user enters start = 14 and end = 23. C program finds all the odd numbers between 14 and 23, including 14 and 23. So the odd numbers are 15, 17, 19, 21, 23. We add all these odd numbers and output the sum to the console window. i.e., 15 + 17 + 19 + 21 + 23 = 95. We out put the value 95 as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers

Video Tutorial: C Program To Find Sum of All Odd Numbers Between Range, using For loop


[youtube https://www.youtube.com/watch?v=tUudL6AKPOg]

YouTube Link: https://www.youtube.com/watch?v=tUudL6AKPOg [Watch the Video In Full Screen.]

Logic To Find Sum of All Odd Numbers Between Range, using For loop

Step 1: We ask the user to enter start and end value.

Step 2: Variable count is initialized to start and for loop executes until count is less than or equal to end. For each iteration of the for loop, value of count increments by 1.

Step 3: For every iteration of the for loop, we check if value present in variable count is an odd number. i.e., count % 2 != 0. If this condition is true, then we add the value present in variable count to the previous value of variable sum.

Step 4: Once the control exits for loop, we print the value present in variable sum – which has the sum of all the odd numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Odd Numbers Between Range, using For loop

#include<stdio.h>

int main()
{
    int start, end, temp, count, sum = 0;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Odd numbers from %d to %d are\n", start, end);
    for(count = start; count <= end; count++)
    {
        if(count % 2 != 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }
    }

    printf("Sum of all the Odd numbers from %d to %d is %d\n", start, end, sum);

    return 0;
}

Output 1:
Enter start and end value
5
14
Odd numbers from 5 to 14 are
5
7
9
11
13
Sum of all the Odd numbers from 5 to 14 is 45

Output 2:
Enter start and end value
14
23
Odd numbers from 14 to 23 are
15
17
19
21
23
Sum of all the Odd numbers from 14 to 23 is 95

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Sum of All Odd Numbers Between Two Integers, using While loop

Lets write a C program to find sum of all odd numbers between range or between 2 integers input by the user.

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 13 % 2 != 0. When we divide 13 by 2, it does not give a reminder of 0. So number 13 is an odd number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end

If user enters start = 10 and end = 20. C program finds all the odd numbers between 10 and 20, including 10 and 20. So the odd numbers are 11, 13, 15, 17, 19. We add all these odd numbers and output the sum to the console window. i.e., 11 + 13 + 15 + 17 + 19 = 75. We out put the value 75 as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers
C Program To Find Sum of All Odd Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Odd Numbers Between Range, using While loop


[youtube https://www.youtube.com/watch?v=B_jGvvCh7Ho]

YouTube Link: https://www.youtube.com/watch?v=B_jGvvCh7Ho [Watch the Video In Full Screen.]

Logic To Find Sum of All Odd Numbers Between Two Integers, using While loop

Step 1: We ask the user to enter start and end value.

Step 2: We iterate through the while loop until value of start is less than or equal to value of variable end. Inside while loop we keep incrementing the value of variable start by one for each iteration.

Step 3: For every iteration we check if value present in variable start is an odd number. i.e., start % 2 != 0. If this condition is true, then we add the value present in variable start to the previous value of variable sum.

Step 4: Once the control exits while loop, we print the value present in variable sum – which has the sum of all the odd numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Odd Numbers Between Two Integers, using While loop

#include<stdio.h>

int main()
{
    int start, end, sum = 0;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\nSum of odd numbers from %d to %d is ", start, end);
    while(start <= end)
    {
        if(start % 2 != 0)
        {
            sum = sum + start;
        }
        start++;
    }
    printf("%d\n", sum);

    return 0;
}

Output 1:
Enter start and end value
10
20

Sum of odd numbers from 10 to 20 is 75

Output 2:
Enter start and end value
25
50

Sum of odd numbers from 25 to 50 is 481

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 10 % 2 == 0. When we divide 10 by 2, it give a reminder of 0. So number 10 is an even number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end

If user enters start = 10 and end = 20. C program finds all the even numbers between 10 and 20, including 10 and 20. So the even numbers are 10, 12, 14, 16, 18, 20. We add all these even numbers and output the sum to the console window. i.e., 10 + 12 + 14 + 16 + 18 + 20 = 90. We out put the value 90 as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers
C Program To Find Sum of All Even Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers Between Range, using While loop


[youtube https://www.youtube.com/watch?v=QX3DExBMohQ]

YouTube Link: https://www.youtube.com/watch?v=QX3DExBMohQ [Watch the Video In Full Screen.]

Logic To Find Sum of All Even Numbers Between Two Integers, using While loop

Step 1: We ask the user to enter start and end value.

Step 2: We iterate through the while loop until value of start is less than or equal to value of variable end. Inside while loop we keep incrementing the value of variable start by one for each iteration.

Step 3: For every iteration we check if value present in variable start is a even number. i.e., start % 2 == 0. If this condition is true, then we add the value present in variable start to the previous value of variable sum.

Step 4: Once the control exits while loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

#include<stdio.h>
int main()
{
    int start, end, sum = 0;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\nSum of even no's from %d to %d is ", start, end);
    while(start <= end)
    {
        if(start % 2 == 0)
        {
            sum = sum + start;
        }
        start++;
    }
    printf("%d\n", sum);

    return 0;
}

Output 1:
Enter start and end value
10
20

Sum of even no’s from 10 to 20 is 90

Output 2:
Enter start and end value
25
50

Sum of even no’s from 25 to 50 is 494

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Decision Control Instruction In C: IF

So far we’ve seen that the code in C program gets executed in sequential order i.e., as and when the code appears in the source code. But today lets see how we can change the sequence of or order of execution using decision control instruction like if statement.

General form of if statement

if( some_condition )
statement;

By default, if statement executes only the next instruction present under it. But if you want to execute some block of code, then you must enclose it inside curly braces as shown below.

if( some_condition )
{
statement_1;
statement_2;
some_logic;
function_calls;
etc;
}

in above code, some_condition can be anything from a simple true(non-zero number) or false(0) or any expression returning 0 or any non-zero number.

Statements inside if statements curly braces can be anything from multi-line statements, function calls, some arithmetic operations etc.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
5
Amazon
Apple

Here user entered value for variable a is less than 10. So the code inside if block doesn’t get executed. So printing of Microsoft and Oracle gets skipped.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
14
Microsoft
Oracle
Amazon
Apple

For the same program, user entered value for variable a as 14, which is greater than 10. So all the company names gets printed on the console window as shown in above output section.

We can even remove condition statement inside if statement

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output 1:
IBM
Enter value for a
1
Microsoft
Oracle
Amazon
Apple

Output 2:
IBM
Enter value for a
0
Amazon
Apple

Output 3:
IBM
Enter value for a
-5
Microsoft
Oracle
Amazon
Apple

In above example, when user enters 0 the condition becomes false, so the block of code inside if statement doesn’t get executed. When the user enters any number other than 0, its considered as true, so the code inside if statement gets executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Decision Control Instruction In C: IF


[youtube https://www.youtube.com/watch?v=ih-gHCBoQ0U]

YouTube Link: https://www.youtube.com/watch?v=ih-gHCBoQ0U [Watch the Video In Full Screen.]


For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert