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

C Program To Check Whether a Character is Vowel or Consonant

Lets write a C program to check whether user entered character is a vowel or a consonant.

Related Read:
if else statement in C
Logical Operators In C

Note: Lowercase English alphabets a, e, i, o, u and uppercase English alphabets A, E, I, O, U are called Vowels. All other alphabets are called Consonants.

We assume that the user enters only alphabets as input for this program.

Source Code: C Program To Check Whether a Character is Vowel or Consonant

#include < stdio.h >

int main()
{
    char ch;

    printf("Enter a character\n");
    scanf("%c", &ch);

    if( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
    {
        printf("%c is vowel\n", ch);
    }
    else
    {
        printf("%c is consonant\n", ch);
    }

    return 0;
}

Output 1:
Enter a character
A
A is vowel

Output 2:
Enter a character
B
B is consonant

Output 3:
Enter a character
e
e is vowel

Output 4:
Enter a character
f
f is consonant

Logic To Check Whether a Character is Vowel or Consonant

We ask the user to enter an alphabet and store it inside character variable ch. Using if else construct we check if the user entered alphabet is vowel or consonant. Inside if condition, we check if alphabet present in variable ch is equal to a or e or i or o or u or A or E or I or O or U. If any of these conditions are true, then it’ll return true and block inside if executes orelse else block gets executed.

In the background, c program checks the ASCII value present in variable ch with the ASCII values of a, e, i, o, u, A, E, I, O, U.

Video Tutorial: C Program To Check Whether a Character is Vowel or Consonant


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

YouTube Link: https://www.youtube.com/watch?v=lJSmUKw3cMM [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