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 Find Surface Area and Volume of a Cube

Lets write a C program to calculate Surface area, Volume and Lateral Surface area of a Cube.

Related Read:
Basic Arithmetic Operations In C

Formula To Calculate Surface Area, Volume and Lateral Surface area of a Cube

Note: If we know the length of any side of the cube, we can calculate the surface area and lateral surface area by using formula shown below.

Surface Area of a Cube
surface_area = 6 * (l * l);

Lateral Surface Area of a Cube
lateral_surface_area = 4 * (l * l);

Volume of a Cube
volume= l * l * l;

where l is value of length of any side of a cube.

The amount of space present inside the cube(3 dimensional cube) is called the volume of that cube.

Source Code: C Program to Find Surface Area and Volume of a Cube

 
#include < stdio.h >

int main()
{
    float l, SA, volume, LSA;

    printf("Enter length of any side of a cube\n");
    scanf("%f", &l);

    SA     = 6 * (l * l);
    volume = l * l * l;
    LSA    = 4 * (l * l);

    printf("Surface Area of the cube = %f\n", SA);
    printf("Volume of the cube = %f\n", volume);
    printf("Lateral Surface Area of the cube = %f\n", LSA);

    return 0;
}

Output 1:
Enter length of any side of a cube
3
Surface Area of the cube = 54.000000
Volume of the cube = 27.000000
Lateral Surface Area of the cube = 36.000000

Output 2:
Enter length of any side of a cube
5
Surface Area of the cube = 150.000000
Volume of the cube = 125.000000
Lateral Surface Area of the cube = 100.000000

C Program to Find Surface Area and Volume of a Cube


[youtube https://www.youtube.com/watch?v=rqAH-E_dR0w]

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