C Program To Find Sum of All Even Numbers From 1 To N, using For loop

Lets write a C program to find sum of all the even numbers from 1 to N, using for loop.

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

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

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window 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 Find Even Numbers Between Range using For Loop

You can also watch the video for 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 From 1 To N, using For loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using For loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. For loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of for loop we increment the value of count by 1.

Inside for loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits for loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using For loop

#include<stdio.h>

int main()
{
    int count, num, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &num);

    printf("Even numbers from 1 To %d are:\n", num);
    for(count = 1; count <= num; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }

    }

    printf("Sum of even numbers from 1 To %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter the limit
5
Even numbers from 1 To 5 are:
2
4
Sum of even numbers from 1 To 5 is 6

Output 2:
Enter the limit
10
Even numbers from 1 To 10 are:
2
4
6
8
10
Sum of even numbers from 1 To 10 is 30

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 From 1 To N, using While loop

Lets write a C program to find sum of all the even numbers from 1 to N, using while loop.

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

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

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window 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

Video Tutorial: C Program To Find Sum of All Even Numbers From 1 To N, using While loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using While loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. While loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of while loop we increment the value of count by 1.

Inside while loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits while loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using While loop

#include<stdio.h>

int main()
{
    int num, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &num);

    while(count <= num)
    {
        if(count%2 == 0)
        {
            sum = sum + count;
        }
        count++;
    }
    printf("Sum of Even numbers from 1 to %d is %d\n", num, sum);
    return 0;
}

Output 1:
Enter the limit
5
Sum of Even numbers from 1 to 5 is 6

Output 2:
Enter the limit
20
Sum of Even numbers from 1 to 20 is 110

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

Even or Odd Number using Ternary Operator and without using Modular Division: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, without using modular division(%) operator and by using Ternary Operator / Conditional Operator.

Related Read:
Even or Odd Number without using Modular Division: C Program
Ternary Operator / Conditional Operator In C

Please visit the links I’ve posted above without fail before watching the video posted below.

Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter a integer number\n");
    scanf("%d", &n);

    ( (n/2)*2 == n ) ?
    (printf("%d is Even\n", n)) :
    (printf("%d is Odd\n", n));


    return 0;
}

Output 1:
Enter a integer number
10
10 is Even

Output 2:
Enter a integer number
5
5 is Odd

Even or Odd Number using Ternary Operator and without using Modular Division: C Program


[youtube https://www.youtube.com/watch?v=4-AoNsgYJQ4]

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


Note: Division of an integer number by 2(which is also an integer number) always returns integer number.

Example 1:
If user enters n = 2;
Applying n = 2 to ( (n/2)*2 == n ).
( (2/2)*2 == 2 )
( (1)*2 == 2 )
( 2 == 2 ) // true

So user entered value, that is, 2 is even number.

Example 2:
If user enters n = 3;
Applying n = 3 to ( (n/2)*2 == n ).
( (3/2)*2 == 3 )
( (1)*2 == 3 )
( 2 == 3 ) // false

2 is not equal to 3. So user entered value, that is, 3 is odd number.
In above example, 3/2 gives back 1 and not 1.5 as 3 is divided by integer which returns only the integer part and discards the decimal part.

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

Even or Odd Number using Ternary Operator: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, using Ternary / Conditional Operator.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Ternary Operator / Conditional Operator In C

Even or Odd Number: C Program

Even or Odd Number without using Modular Division: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    (n % 2 == 0) ?
    (printf("%d is Even number\n", n)) :
    (printf("%d is Odd  number\n", n));

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Even or Odd Number using Ternary Operator: C Program


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

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


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd number.

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

Even or Odd Number without using Modular Division: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, without using modular division(%) operator.

Related Read:
Even or Odd Number: C Program (using Modular Division Operator)
Basic Arithmetic Operations In C
Relational Operators In C

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Note:
1. Even numbers are of the form 2 * n;
2. Odd numbers are of the form (2 * n + 1);

You can substitute n value in above forms and check the resulting numbers for even or odd.

Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    if( (n/2)*2 == n )
    {
        printf("%d is even number\n", n);
    }
    else
    {
        printf("%d is odd number\n", n);
    }

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

C Program To Check Even or Odd Number, without using Modular Division Operator


[youtube https://www.youtube.com/watch?v=qc-wK_amMzs]

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


Note: Division of an integer number by 2(which is also an integer number) always returns integer number.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is even or odd.

User entered integer number is divided by 2. And then it is multiplied by 2. If the final result is equal to the original user entered value, then the user entered value is even orelse its odd.

Example 1:
If user enters n = 2;
Applying n = 2 to ( (n/2)*2 == n ).
( (2/2)*2 == 2 )
( (1)*2 == 2 )
( 2 == 2 ) // true

So user entered value, that is, 2 is even number.

Example 2:
If user enters n = 3;
Applying n = 3 to ( (n/2)*2 == n ).
( (3/2)*2 == 3 )
( (1)*2 == 3 )
( 2 == 3 ) // false

2 is not equal to 3. So user entered value, that is, 3 is odd number.
In above example, 3/2 gives back 1 and not 1.5 as 3 is divided by integer which returns only the integer part and discards the decimal part.

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