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

C Program To Find Prime Numbers Between Two Intervals, using While Loop

Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested while loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested While Loop: C Program
C Program To Find Prime Number or Not using While Loop
C Program To Find Prime Numbers From 2 To N, using While Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using While Loop


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

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

Inner While loop Logic

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

While loop Logic

Outer while loop selects a number for each iteration and stores inside variable start. Inner while loop checks if the selected value(present in variable start) is prime or not. If its a prime number then the variable prime will have value of 1 or else it’ll have value 0 inside it(after completion of inner while loop iteration). If the value present in variable prime is 1, then we print the value present in variable start on to the console window.

Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop

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

int main()
{
    int start, end, count, prime, inum;

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

    printf("\n\nPrime Numbers from %d to %d are:\n", start, end);
    while(start <= end)
    {
        inum  = sqrt(start);
        count = 2;
        prime = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                prime = 0;
                break;
            }
            count++;
        }

        if(prime) printf("%d, ", start);
        start++;
    }
    printf("\n\n");

    return 0;
}

Output 1:
Enter start and end value
10
50

Prime Numbers from 10 to 50 are:
11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter start and end value
50
100

Prime Numbers from 50 to 100 are:
53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Output 3:
Enter start and end value
100
200

Prime Numbers from 100 to 200 are:
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,

Output 4:
Enter start and end value
25
50

Prime Numbers from 25 to 50 are:
29, 31, 37, 41, 43, 47,

Output 5:
Enter start and end value
75
150

Prime Numbers from 75 to 150 are:
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Logic To Find Prime Number, using While Loop

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 inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – 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.

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 Print Lowercase Alphabet(a-z) using While loop

In this video tutorial we show you how to write C program to print all the lower case alphabets(a-z) using simple while loop.

Related Read:
while loop in C programming
C Program To Print Uppercase Alphabet(A-Z) using While loop

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Note:
ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.
and so on till z ..
ASCII value of z is 122.

Source Code: C Program To Print Lowercase Alphabet(a-z) using While loop

#include < stdio.h >

int main()
{
    char ch = 'a';

    printf("Lowercase English Alphabets:\n");
    while(ch <= 'z')
    {
        printf("%c ", ch);
        ch++;
    }
    printf("\n");

    return 0;
}

Output:
Lowercase English Alphabets:
a b c d e f g h i j k l m n o p q r s t u v w x y z

Logic To Print Lowercase Alphabet(a-z) using While loop

Each alphabet has it’s own(unique) ASCII value. We initialize the character variable ch to a. Alphabet a has a ASCII value of 97. 97 gets stored in ch. We iterate through the while loop until value of ch is less than or equal to z(i.e., upto ASCII value of z, which is 122). We print character value(%c) of the variable ch, which has the character and not the ASCII value. We keep incrementing the value of ch by 1 for each iteration of while loop.

Video Tutorial: C Program To Print Lowercase Alphabet(a-z) using While loop


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

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

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 Print Uppercase Alphabet(A-Z) using While loop

In this video tutorial we show you how to write C program to print all the upper case alphabets(A-Z) using simple while loop.

Related Read:
while loop in C programming

Note: In C programming language, every character variable holds an ASCII value rather than the character itself. You can check ASCII value of all the characters here: C Program To Print All ASCII Characters and Code

Note:
ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.
and so on till Z ..
ASCII value of Z is 90.

Source Code: C Program To Print Uppercase Alphabet(A-Z) using While loop

#include < stdio.h >

int main()
{
    char ch = 'A';

    printf("Uppercase English Alphabets:\n");
    while(ch <= 'Z')
    {
        printf("%c ", ch);
        ch++;
    }
    printf("\n\n");

    return 0;
}

Output:
Uppercase English Alphabets:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Logic To Print Uppercase Alphabet(A-Z) using While loop

Each alphabet has it’s own(unique) ASCII value. So we initialize the character variable to A, and then using while loop we iterate until our character variable is equal to Z. C program checks with the characters underlying ASCII values. Inside while loop we increment the value of character variable by one for each iteration and also print the character present in it.

Video Tutorial: C Program To Print Uppercase Alphabet(A-Z) using While loop


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

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

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