C Program To Print Floyd’s Triangle using For Loop

Lets write C program to print Floyd’s Triangle, using nested for loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
C Program To Print Floyd’s Triangle

Video Tutorial: C Program To Print Floyd’s Triangle using For Loop


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

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

Logic To Print Floyd’s Triangle using For Loop

We ask the user to input the number of rows for Floyd’s Triangle, we store it inside variable num.

Outer For loop
In our C program, row number and the total numbers to be printed for that row is present inside variable row. So the outer for loop selects the row number and the number of elements to be printed in that row.

We initialize the row to 1 and iterate through the outer for loop until row is less than or equal to user entered number. For each iteration of the outer for loop we increment the value of row by 1. That way selecting the next row for each iteration.

Inner For loop
Inner for loop prints natural numbers in each selected row. Variable col is assigned to 1 for each iteration of outer for loop, so that the numbers gets printed from the first position in any selected row.

Inner for loop prints the natural number from the first position till the selected row number of times.

For Example, if user enters num = 5, the following Triangle will be printed:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Important Note:
Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.

Source Code: C Program To Print Floyd’s Triangle using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, count = 1;

    printf("Enter number of rows for Floyd's Triangle\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            printf("%d  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter number of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter number of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

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 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