C Program To Find Perfect Number using For loop

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

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language
C Program to Find Factors of a Number using For Loop
C Program to Find Perfect Number using while loop

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 28. The numbers which perfectly divide 28 are 1, 2, 4, 7, 14, 28. Leave 28 and add all other numbers. i.e., 1 + 2 + 4 + 7 + 14 = 28. So the entered number and the sum are equal. So 28 is a perfect number.

Video Tutorial: C Program To Check Perfect Number or Not, using For loop


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

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

Source Code: C Program To Find Perfect Number using For loop

#include<stdio.h>

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

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

    printf("Factors of %d are(except the number itself):\n", num);
    for(count = 1; count < num; count++)
    {
        if(num % count == 0)
        {
            printf("%d\n", count);
            sum = sum + 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
6
Factors of 6 are(except the number itself):
1
2
3

6 is a perfect number

Output 2
Enter a number
8
Factors of 8 are(except the number itself):
1
2
4

8 is not a perfect number

Output 3
Enter a number
14
Factors of 14 are(except the number itself):
1
2
7

14 is not a perfect number

Output 4
Enter a number
28
Factors of 28 are(except the number itself):
1
2
4
7
14

28 is a perfect number

Logic To Check Perfect Number or Not using For loop

We ask the user to enter a number and store it inside address of integer variable num. Next we find factors of that user entered number i.e., all the numbers which perfectly divide the user entered number. In that factors list, we exclude the user entered number itself.

Now we add all the factors(except the user entered number itself). If the sum and the user entered number are equal then its a perfect number.

We initialize for loop counter variable count to 1 and iterate the for loop until count is less than num. For each iteration of for loop we increment the value of count by 1.

Inside for loop we check if num % count == 0. If true, we add value of count to previous value of variable sum.

After control exits for 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 Factors of a Number using For Loop

Write a C program to display Factors of user entered number, using for loop. All the numbers which perfectly divide a given number are called as Factors of that number.

For Example, if user enters integer number 50. All the numbers which perfectly divide the number 50 are called Factors of number 50.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
C Program to Find Factors of a Number

Video Tutorial: C Program to Find Factors of a Number using For Loop


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

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


Logic To Find Factors of a Number using For Loop

We ask the user to enter a integer number. Next we iterate through the for loop until count is less than or equal to the user entered number. Example, if user entered number is 50, then we iterate through the loop for 50 times. Each time we check if the user entered number is perfectly divisible by the value of count. Initial value of count is 1 and after each iteration of the loop count value increments by 1. Whenever a number perfectly divides the user entered number, we display it as factor of the user entered number.

Source Code: C Program to Find Factors of a Number using For Loop

#include<stdio.h>

int main()
{
    int num, count;

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

    printf("Factors of %d are\n", num);
    for(count = 1; count <= num; count++)
    {
        if(num % count == 0)
            printf("%d\n", count);
    }

    return 0;
}

Output 1:
Enter a number to find factors
60
Factors of 60 are
1
2
3
4
5
6
10
12
15
20
30
60

Output 2:
Enter a number to find factors
25
Factors of 25 are
1
5
25

Output 3:
Enter a number to find factors
40
Factors of 40 are
1
2
4
5
8
10
20
40

Output 4:
Enter a number to find factors
75
Factors of 75 are
1
3
5
15
25
75

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 First and Second Biggest in N Numbers, without using Arrays, using For Loop

Write a C program to find first and second biggest numbers in a list of N numbers entered by the user, without using arrays and without sorting the list and by using for loop.

Related Read:
For Loop In C Programming Language
Find First and Second Biggest in N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop


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

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


Source Code: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, fbig = 0, sbig = 0;

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

    printf("Enter %d positive numbers\n", limit);

    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }

        if(num > sbig && num < fbig)
        {
            sbig = num;
        }
    }

    printf("First big = %d\nSecond Big = %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 positive numbers
1
9
5
3
8
First big = 9
Second Big = 8

Output 2:
Enter the limit
8
Enter 8 positive numbers
1
5
9
3
7
8
6
10
First big = 10
Second Big = 9

Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the for loop until loop counter variable count is less than or equal to limit. For each iteration of for loop we ask the user to enter a positive integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig.

Next we check if the user entered number is greater than sbig and less than fbig, if true, we assign the value of num to sbig.

Once control exits for loop, inside fbig we will have biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers 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 Biggest of N Numbers, without using Arrays, using For Loop

Write a C program to find biggest of N numbers without using Arrays and using for loop.

Related Read:
For Loop In C Programming Language
Find Biggest of N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop


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

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


Logic To Find Biggest Number of N numbers, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the for loop 5 times i.e., until value of count is less than or equal to limit.

We check if its the first iteration of for loop. If true, we assign the first user entered number to variable big.

Inside the for loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user.

Source Code: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, big;

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

    printf("Enter %d numbers\n", limit);
    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > big || count == 1)
        {
            big = num;
        }
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
9
3
7
4
Biggest number is 9

Output 2:
Enter the limit
6
Enter 6 numbers
-2
-5
-6
-9
-1
-4
Biggest number is -1

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 Sum and Average of N Numbers without using Arrays, using For Loop

Write a C program to calculate Sum and Average of N numbers without using Arrays and using for loop.

Related Read:
Basic Arithmetic Operations In C
For Loop In C Programming Language
Calculate Sum and Average of N Numbers without using Arrays: C Program

Video Tutorial: C Program To Calculate Sum and Average of N Numbers without using Arrays, using For Loop


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

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


Logic To Calculate Sum and Average of N Numbers without using Arrays

We ask the user to input the limit. Based on that limit value we ask the user to enter the integer numbers. For example, if the user enters value of limit as 5, then we ask the user to enter 5 integer numbers.

If limit is 5, then inside for loop we ask the user to input 5 integer values, and we add those values to the previous value present in variable sum. Simultaneously we keep incrementing the value of variable count by 1 for each iteration of for loop. Once the value of variable count is greater than the value of limit, then the control exits for loop. Immediately outside for loop we calculate the average by using the formula:

average = sum / limit;

Source Code: C Program To Calculate Sum and Average of N Numbers without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int num, count, sum = 0;
    float avg = 0.0, limit;

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

    printf("Enter %f numbers\n", limit);
    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);
        sum = sum + num;
    }
    avg = sum / limit;

    printf("Sum = %d\nAverage = %0.2f\n", sum, avg);

    return 0;
}

Output 1:
Enter the limit
6
Enter 6 numbers
1
5
9
3
5
7
Sum = 30
Average = 5.00

Output 2:
Enter the limit
14
Enter 14 numbers
5
6
3
2
1
4
9
7
8
0
-5
-6
0
-10
Sum = 24
Average = 1.71

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