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

C Program To Find Sum of All Odd Numbers From 1 To N, using While loop

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

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

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

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program

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


[youtube https://www.youtube.com/watch?v=y4Sy9xo-pFU]

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

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

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

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

#include<stdio.h>

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

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

    while(count <= num)
    {
        if(count%2 != 0)
        {
            sum = sum + count;
        }
        count++;
    }

    printf("Sum of ODD integer number is %d\n", sum);

    return 0;
}

Output 1:
Enter a integer number
5
Sum of ODD integer number is 9

Output 2:
Enter a integer number
20
Sum of ODD integer number is 100

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 Find Prime Numbers From 2 To N, using While Loop

Lets write a C program to find and print / display all the prime numbers from 2 to N. Here N is the user entered number / limit.

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

Video Tutorial: C Program To Find Prime Numbers From 2 To N, using While Loop


[youtube https://www.youtube.com/watch?v=iYHo-9rClAs]

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

While loop Logic

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

Source Code: C Program To Find Prime Numbers From 2 To N, using Nested While Loop

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

int main()
{
    int start = 2, num, count, flag, inum;

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

    printf("\n\nPrime numbers from 2 to %d are:\n", num);

    while(start <= num)
    {
        inum = sqrt(start);
        count= 2;
        flag = 1;

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

        if(flag) printf("%d, ", start);

        start++;
    }

    printf("\n\n");

    return 0;
}

Output 1:
Enter the limit
50

Prime numbers from 2 to 50 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter the limit
75

Prime numbers from 2 to 75 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,

Output 3:
Enter the limit
100

Prime numbers from 2 to 100 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Output 4:
Enter the limit
500

Prime numbers from 2 to 500 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,

Output 5:
Enter the limit
1000

Prime numbers from 2 to 1000 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,

Logic To Find Prime Numbers From 2 To N, 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 user entered number is divisible by any number other than 1 and itself.

We start checking for prime number from number 2 till the user entered number. Variable start is assigned an initial value of 2. For each iteration of outer while loop value of start increments by 1. Inside the inner while loop we check if the value present in variable start is prime number or not. If its prime number then we print that number on to the console window.

Outer while loop selects the number and stores it in variable start on each iteration. Inner while loop checks if the selected number(present in variable start) is prime number or not. You can know the complete logic to check whether a selected number is prime or not: C Program To Find Prime Number or Not using While Loop

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

Lets write a C program to check whether user input number is prime number or not, using 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:
while loop in C programming
if else statement in C

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

While 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 Check Whether a Given Number is Prime Number or Not, using While Loop


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

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

Method 1 Source Code: Prime Number or Not

#include<stdio.h>

int main()
{
    int num, count = 2, flag = 1;

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

    while(count < num)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 accept the number from the user. In while loop condition we check if the user entered number is greater than the number present in variable count. We are not checking for greater than or equal to, because all the positive natural numbers are divisible by itself, so we skip checking divisibility of user entered number by itself.

Inside while loop we keep incrementing the value of count by one for each iteration and we check if num%count == 0. If that condition is true, then we store 0 in variable flag and break out of the loop. That indicates there is yet another number(other than 1 and the number itself) which perfectly divides the user entered number.

Outside the while loop we check if the value of flag is 1 or 0. If its 1, then the user entered number is prime number orelse its not a prime number.

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

#include<stdio.h>

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

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

    inum = num / 2;

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 while loop.

If num = 50;
inum = num / 2;
inum = 50 / 2;
inum = 25;

So its enough if we iterate through the while loop 25(num/2) times to check if number 50 is divisible by any number from 2 to 25.

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

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

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

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

    inum = sqrt(num);

    while(count <= inum)
    {
        if(num%count == 0)
        {
            flag = 0;
            break;
        }
        count++;
    }

    if(flag) printf("%d is prime number\n", num);
    else     printf("%d is 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 while loop.

If num = 50;
inum = sqrt(num);
inum = sqrt(50);
inum = 7;

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

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 For 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