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 Check whether a Number is Strong Number or Not

Lets write a C program to check whether user entered number is strong number or not, using nested while loop.

Strong Number: Sum of factorial of a number’s individual digits should be equal to the number itself. Such a number is called Strong Number.

For Example: If user entered number is 145. We find factorial of individual digits of 145 and add it.
i.e., !1 + !4 + !5

(1) + (24) + (120) = 145

So the original number and the sum of factorials of individual digits are both 145. So number 145 is considered as a Strong Number.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
Nested While Loop: C Program

Important Topics
Calculate Sum of Digits: C Program
C Program To Find Factorial of a Number

Source Code: C program To Check whether a Number is Strong Number or Not

#include < stdio.h >

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

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

    temp = num;

    while(num)
    {
        rem = num % 10;

        count = 1;
        fact  = 1;
        while(count <= rem)
        {
            fact = fact * count;
            count++;
        }

        printf("Factorial of %d is %d\n", rem, fact);

        sum = sum + fact;

        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is a strong number\n", temp);
    }
    else
    {
        printf("%d is not a strong number\n", temp);
    }

    return 0;
}

Output 1
Enter a number
145
Factorial of 5 is 120
Factorial of 4 is 24
Factorial of 1 is 1
145 is a strong number

Output 2
Enter a number
140
Factorial of 0 is 1
Factorial of 4 is 24
Factorial of 1 is 1
140 is not a strong number

Video Tutorial: C Program To Find Strong Number


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

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

Logic To Check whether a Number is Strong Number or Not

If user entered number is 145. i.e., num = 145. Using outer while loop we keep fetching digits one by one by using below code.

rem = num % 10;
num = num / 10;

For each iteration the outer loop fetches individual digits of the number. The inner while loop calculates the factorial for that fetched individual digit. Next we keep adding the factorial of each individual digit.

Once the control exits outer while loop we check if the user entered number is equal to the value present in variable sum. If true, then the user entered number is a strong number, if not, its not a strong 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

C Program to Calculate the Sum of Natural Numbers From 1 to N

Lets write a C program to calculate sum of all natural numbers from 1 to N, using while loop.

Related Read:
while loop in C programming
C Program to Print Natural Numbers from 1 to N using While loop
C Program to Print Natural Numbers Between Two Numbers using While loop

Source Code: C Program to Calculate the Sum of Natural Numbers From 1 to N

 
#include < stdio.h >

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

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


    while(count <= num)
    {
        sum = sum + count;
        count++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output 1:
Enter a positive number
5
Sum = 15

Output 2:
Enter a positive number
10
Sum = 55

OR

 
#include < stdio.h >

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

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

    printf("Sum of natural numbers from 1 to %d is:\n", num);
    while(count <= num)
    {
        sum = sum + count;
        printf("%d  ", count);
        count++;

        if(count > num)
        {
            printf(" = %d\n", sum);
        }
        else
        {
            printf("+ ");
        }

    }
    return 0;
}

Output 1:
Enter a positive number
5
Sum of natural numbers from 1 to 5 is:
1 + 2 + 3 + 4 + 5 = 15

Output 2:
Enter a positive number
10
Sum of natural numbers from 1 to 10 is:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

C Program to Calculate the Sum of Natural Numbers From 1 to N


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

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


Logic To Calculate the Sum of Natural Numbers From 1 to N

We ask the user to enter a positive number and store it in variable num. We assign 1 to variable count and 0 to variable sum.

In while loop we check for the condition: count is less than or equal to num. While loop keeps executing until that condition is true. Inside while loop we increment the value of count by one and also add the value of count to the previous value of sum. We keep doing this until count is less than or equal to num. Once that condition is false, control exits the while loop and we display the value of sum as output.

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 Calculate the Sum of Natural Numbers Between Range

Lets write a C program to calculate sum of all natural numbers between the user entered range of numbers, using while loop.

We assume that user enters smaller number first and biggest number next.

Related Read:
while loop in C programming
C Program to Print Natural Numbers Between Two Numbers using While loop

Source Code: C Program to Calculate the Sum of Natural Numbers Between Range

 
#include < stdio.h >

int main()
{
    int min, max, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        min++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Output 1:
Enter 2 positive numbers
1
5
Sum = 15

Output 2:
Enter 2 positive numbers
10
15
Sum = 75

OR

 
#include < stdio.h >

int main()
{
    int min, max, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    printf("\n");

    while(min <= max)
    {
        sum = sum + min;
        printf("%d ", min);
        min++;

        if(min > max)
        {
            printf(" = %d\n", sum);
        }
        else
        {
            printf("+ ");
        }
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
1
5

1 + 2 + 3 + 4 + 5 = 15

Output 2:
Enter 2 positive numbers
10
15

10 + 11 + 12 + 13 + 14 + 15 = 75

Output 3:
Enter 2 positive numbers
25
30

25 + 26 + 27 + 28 + 29 + 30 = 165

C Program to Calculate the Sum of Natural Numbers Between Range


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

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


Logic To Calculate the Sum of Natural Numbers Between Range

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. We check if variable min is less than or equal to max. Until this condition is true, while loop keeps iterating. Inside while loop we increment the value of min by one for each iteration. We also add the value of min to the value of sum on each iteration. At the end, when min is greater than max, control exits while loop and we printout the value of sum – which has the sum of all the numbers between the range entered by the user.

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 Generic Root of a Number

Lets write a C program to calculate Generic Root of a user input number.

Generic Root: of a number is sum of all the digits of the number until we get a single-digit output.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program

For Example: If user input number is 12345, then we add all the individual digits of the number i.e., 1 + 2 + 3 + 4 + 5 = 15. We got 15. Now we add individual digits of number 15 i.e., 1 + 5 = 6. So Generic Root of number 12345 is 6.

Source Code: C Program to Find Generic Root of a Number

 
#include < stdio.h >

int main()
{
    int num, temp, rem, sum = 0;

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

    temp = num;

    while(temp > 0)
    {
        rem  = temp % 10;
        sum  = sum + rem;
        temp = temp / 10;

        if(temp == 0)
        {
            if(sum > 9)
            {
                temp = sum;
                sum  = 0;
            }
        }
    }

    printf("Generic Root of %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter a number
12345
Generic Root of 12345 is 6

Output 2:
Enter a number
123
Generic Root of 123 is 6

Output 3:
Enter a number
15937
Generic Root of 15937 is 7

Output 4:
Enter a number
222222
Generic Root of 222222 is 3

Output 5:
Enter a number
99999
Generic Root of 99999 is 9

C Program to Calculate Generic Root of a Number


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

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


Logic To Find Generic Root of a Number

If user entered number is 1234.
Num = 1234;
temp = 1234;
sum = 0;

Iteration 1
rem = temp % 10;
rem = 1234 % 10;
rem = 4;

sum = sum + rem
sum = 0 + 4;
sum = 4;

temp = temp / 10;
temp = 1234 / 10;
temp = 123;

Iteration 2
rem = temp % 10;
rem = 123 % 10;
rem = 3;

sum = sum + rem
sum = 4 + 3;
sum = 7;

temp = temp / 10;
temp = 123 / 10;
temp = 12;

Iteration 3
rem = temp % 10;
rem = 12 % 10;
rem = 2;

sum = sum + rem
sum = 7 + 2;
sum = 9;

temp = temp / 10;
temp = 12 / 10;
temp = 1;

Iteration 4
rem = temp % 10;
rem = 1 % 10;
rem = 1;

sum = sum + rem
sum = 9 + 1;
sum = 10;

temp = temp / 10;
temp = 1 / 10;
temp = 0;

Now inside while loop we keep checking for the condition temp = 0. Once this condition is true, we check if the value present in sum is greater than 9. i.e., we check if variable sum has double digit number. If that is true, then we copy the number present in variable sum to variable temp and we assign 0 to sum. Now the while loop continues until the variable sum has single digit number.

Once variable sum has single digit number in it, control exits the while loop and we out put the value of sum as Generic Root of the user entered 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