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 Find Perfect Number using while loop

Lets write a C program to check if user entered number is a perfect number or not, using while loop.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming

Perfect Number: A number is called perfect number if sum of its divisors(except the number itself) is equal to the number.

For Example: If the user entered number is 6. The numbers which perfectly divide 6 are 1, 2, 3 and 6. Leave 6 and add all other numbers. i.e., 1 + 2 + 3 = 6. So the entered number and the sum are equal. So 6 is a perfect number.

Source Code: C Program to Find Perfect Number using while loop

#include<stdio.h>

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

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

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

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

    return 0;
}

Output 1
Enter a number
5
5 is not a perfect number

Output 2
Enter a number
6
6 is a perfect number

Output 3
Enter a number
25
25 is not a perfect number

Output 4
Enter a number
28
28 is a perfect number

Video Tutorial: C Program to Find Perfect Number using while loop


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

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

Logic To Find Perfect Number using while loop

We initialize variables count to 1 and sum to 0. Next we ask the user to enter a number. We iterate through while loop until value of count is less than the user entered number. Inside while loop we increment the value of variable count by one for each iteration. Inside while loop we also check for the condition – if user entered number modulo division value of count is equal to 0. If its true we add the value of count to the previous value of sum.

After control exits while loop we check if the value of sum and number entered by the user are same. If its same, then the user entered number is perfect number. If not, the number is not a perfect 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 Find Area of Right Angled Triangle

Lets write a C program to calculate area of a right angled Triangle, by asking the user to enter its width and height.

Related Read:
Find Area of a Triangle Using Its Sides: C Program
Find Area of a Triangle Using Its Base and Height: C Program

Formula To Find Area of Right Angled Triangle

Area = (width * Height) / 2.0;

OR

Area = (width * Height * 0.5);

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

C program To Find Area of Right Angled Triangle


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

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


Source Code: C program To Find Area of Right Angled Triangle

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    area = (h * w) / 2.0;

    printf("Area of a Right Angled Triangle is %f\n", area);

    return 0;
}

Output:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    if(w == 0 || h == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (h * w) / 2.0;
        printf("Area of a Right Angled Triangle is %f\n", area);
    }

    return 0;
}

Output 1:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Output 2:
Enter height and width of a right angled triangle
0
5
Invalid Input

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