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

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

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

For Example: 7 % 2 != 0. When we divide 7 by 2, it doesn’t give a reminder of 0. So number 7 is odd number.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program

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


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

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

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

Since we are checking for odd 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 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 odd numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Odd 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("Odd 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 odd numbers from 1 To %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter the limit
5
Odd numbers from 1 To 5 are:
1
3
5
Sum of odd numbers from 1 To 5 is 9

Output 2:
Enter the limit
10
Odd numbers from 1 To 10 are:
1
3
5
7
9
Sum of odd numbers from 1 To 10 is 25

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 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 Odd Numbers Between Range using For Loop

Lets write a C program to generate odd numbers between 2 integer values input by the user using For loop.

Related Read:
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers

Note 1: An odd number is an integer that is not exactly divisible by 2.

Note 2: Odd numbers are of the form (2 * number + 1);

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

Video Tutorial: C Program To Find Odd Numbers Between Range using For Loop


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

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


In above c program, we ask the user to input 2 integer value and store it in variables start and end. If value of start is greater than the value of end, then we swap the values.

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

Inside for loop, for every value of count, we check if its not perfectly divisible by 2. If true, it’s a Odd number and we output that number to the console window.

Source Code: C Program To Find Odd Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int start, end, temp, count;

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

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

    printf("Odd numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count % 2 != 0)
            printf("%d\n", count);
    }

    return 0;
}

Output 1
Enter start and end value, to find odd numbers
40
60
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

Output 2
Enter start and end value, to find odd numbers
60
40
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

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 Prime Number or Not using For Loop

Lets write a C program to check whether user input number is prime number or not, using for loop.

Prime Number: is a natural number greater than 1, which has no positive divisors other than 1 and itself.

Related Read:
For Loop In C Programming Language
if else statement in C
break Statement In C Programming Language

In this video tutorial we’re illustrating 3 methods to find if the user entered number is prime number or not.

For loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2. So our c program starts checking for divisibility from number 2.

Video Tutorial: C Program To Find Prime Number or Not using For Loop


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

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

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count, prime = 1;

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

    for(count = 2; count < num; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
7
7 is prime number

Output 2:
Enter a number
10
10 is not prime number

Logic: Method 1

We ask the user to enter a positive number and store it in variable num. Using for loop we start dividing the user entered number from 2 to num-1 times. If any number from 2 to num-1 perfectly divide the user entered number, then it’s not a prime number. We assign value 0 to variable prime and break out of the loop and print the message to the user.

Method 2 Source Code: Prime Number or Not: Divide By 2

#include<stdio.h>

int main()
{
    int num, count, prime = 1, inum;

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

    inum = num / 2;

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
41
41 is prime number

Output 2:
Enter a number
15
15 is not prime number

Logic: Method 2

Please read the logic for method 1 above before proceeding.
In this method, we divide the user entered number by 2. This reduces the number of iterations of for loop.

If num = 41;
inum = num / 2;
inum = 41 / 2;
inum = 20;

So its enough if we iterate through the for loop 19(num/2) times to check if number 41 is perfectly divisible by any number from 2 to 20.

Method 3 Source Code: Prime Number or Not: square root Method

#include<stdio.h>
#include<math.h>

int main()
{
    int num, count, prime = 1, inum;

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

    inum = sqrt(num);

    for(count = 2; count <= inum; count++)
    {
        if(num % count == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("%d is a Prime Number\n", num);
    else
        printf("%d is a not Prime Number\n", num);

    return 0;
}

Output 1:
Enter a number
50
50 is not prime number

Output 2:
Enter a number
53
53 is prime number

Logic: Method 3

Please read the logic for method 1 above before proceeding.
In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of for loop even further.

If num = 41;
inum = sqrt(num);
inum = sqrt(41);
inum = 6;

So its enough if we iterate through the while loop 5( sqrt(num) ) times to check if number 41 is perfectly divisible by any number from 2 to 6.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if and else because we only have 1 line of code after if and else – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

You can also watch video for C Program To Find Prime Number or Not using While Loop

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