Find First and Second Biggest in N Numbers, without using Arrays: C Program

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

Related Read:
while loop in C programming
Find Biggest of N Numbers, without using Arrays: C Program

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

 
#include<stdio.h>

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

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

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

    while(limit > 0)
    {
        scanf("%d", &num);

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

        limit--;
    }

    printf("First Big is %d\n", fbig);
    printf("Second Big is %d\n", sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
First Big is 5
Second Big is 4

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
First Big is 5
Second Big is 4

Output 3:
Enter the limit
5
Enter 5 numbers
1
4
3
5
2
First Big is 5
Second Big is 4

Output 4:
Enter the limit
8
Enter 8 numbers
1
5
9
3
7
4
6
8
First Big is 9
Second Big is 8

Find First and Second Biggest in N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=gs4YT-Qcw6k]

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


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 while loop until limit value is 0. For each iteration of while loop we ask the user to enter a 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. If the new number entered by the user is not greater than fbig, then we check if its greater than sbig. If true, we transfer the value of num to sbig.

Once the value of limit is 0, control exits the while loop, and inside fbig we have first 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

Find Biggest of N Numbers, without using Arrays: C Program

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

Related Read:
while loop in C programming

Logic To Find Biggest Number, 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 while loop 5 times i.e., until value of limit is 0. Inside the while 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 copy assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user. Simultaneously we keep decrementing the value of variable limit by one for each iteration of while loop. Once limit is 0, control exits the while loop and we print the value present inside the variable big.

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

 
#include<stdio.h>

int main()
{
    int limit, num, big = 0;

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

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

    scanf("%d", &num);
    big = num;
    limit = limit - 1;

    while(limit > 0)
    {
        scanf("%d", &num);
        if(num > big)
        {
            big = num;
        }
        limit--;
    }

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

    return 0;
}

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

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

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

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

Find Biggest of N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=Cx4-egvgSQs]

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

Calculate Sum and Average of N Numbers without using Arrays: C Program

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

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

Logic

Here 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 while loop we ask the user to input 5 integer values, and we add those values and keep storing the resulting number inside variable Sum. And simultaneously we keep decrementing the value of variable limit by 1 for each iteration of while loop. Once the value of variable limit is equal to 0 the control exits the while loop. Immediately outside while loop we calculate the average by using the formula:
average = sum / (float)limit;

Important Note: We need to type cast the data type of variable limit to float orelse it’ll give wrong results for certain inputs. Because any number divided by a integer number will return integer part of the result and discard the numbers after decimal point.

We’ve missed this in the video. So please fix the source code while practicing.

Source Code: Calculate Sum and Average of N Numbers without using Arrays: C Program

 
#include<stdio.h>

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

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

    temp = limit;

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

    while(limit)
    {
        scanf("%d", &num);
        sum = sum + num;
        limit--;
    }
    avg = sum / (float)temp;

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

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers:
1
2
3
4
5
Sum = 15
Average = 3.000000

Output 2:
Enter the limit
10
Enter 10 numbers:
1
5
9
7
5
3
10
45
50
60
Sum = 195
Average = 19.500000

Calculate Sum and Average of N Numbers without using Arrays: C Program


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

YouTube Link: https://www.youtube.com/watch?v=z5D1i9fhV6M [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 Print Multiplication Table Using While Loop

Lets write a C program to ask the user to input an integer value and then output multiplication table(up to 10) on to the console window.

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

Simple Mathematical Trick — logical thinking

Source Code: C Program To Print Multiplication Table Using While Loop

 
#include < stdio.h >

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

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

    printf("\nMultiplication table for %d is:\n\n", num);
    while(count <= 10)
    {
        printf("%d x %d = %d\n", num, count, (num*count));
        count++;
    }

    return 0;
}

Output 1:
Enter a number
2

Multiplication table for 2 is:

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Output 2:
Enter a number
9

Multiplication table for 9 is:

9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90

Output 3:
Enter a number
14

Multiplication table for 14 is:

14 x 1 = 14
14 x 2 = 28
14 x 3 = 42
14 x 4 = 56
14 x 5 = 70
14 x 6 = 84
14 x 7 = 98
14 x 8 = 112
14 x 9 = 126
14 x 10 = 140

Output 4:
Enter a number
10

Multiplication table for 10 is:

10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

C Program To Print Multiplication Table Using While Loop


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

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


Logic: Multiplication Table

We take a variable count and initialize it to 1 and keep increment the value of count by 1, until its value is 11. Once its value is 11 we stop iterating the while loop. This way we can calculate and out put multiplication table for 10 numbers.

Inside the while loop we multiply the user entered number and the value of count, and then display the result on each iteration.

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 Compute Smallest Number of Notes That Will Combine To Give Rs N

Consider a currency system in which there are notes of seven denominations, namely, Re 1, Rs 2, Rs 5, Rs 10, Rs 50 and Rs 100. If a sum of Rs N is entered through the keyboard, write a C program to compute the smallest number of notes that will combine to give Rs N.

Related Read:
Basic Arithmetic Operations In C

Logic To Get Minimum Number of Notes To Represent The Amount

First we divide the user entered amount with the biggest denomination i.e., Rs 100. Next, if its divisible, then we subtract the number of Rs 100 currency notes we get by dividing the amount by 100. Next we check for the next biggest denomination Rs 50 and so on ..Rs 10, Rs 5, Rs 2 and Rs 1.

Source Code: C Program To Compute Smallest Number of Notes That Will Combine To Give Rs N

 
#include < stdio.h >

int main()
{
    int amount, temp;

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

    temp   = amount / 100;
    amount = amount - (temp*100);

    printf("%d   x 100 = %d\n", temp, (temp*100));

    temp   = amount / 50;
    amount = amount - (temp*50);

    printf("%d   x 50  = %d\n", temp, (temp*50));

    temp   = amount / 10;
    amount = amount - (temp*10);

    printf("%d   x 10  = %d\n", temp, (temp*10));

    temp   = amount / 5;
    amount = amount - (temp*5);

    printf("%d   x 5   = %d\n", temp, (temp*5));

    temp   = amount / 2;
    amount = amount - (temp*2);

    printf("%d   x 2   = %d\n", temp, (temp*2));

    temp   = amount / 1;
    amount = amount - (temp*1);

    printf("%d   x 1   = %d\n", temp, (temp*1));

    return 0;
}

Output 1:
Enter amount
123
1 x 100 = 100
0 x 50 = 0
2 x 10 = 20
0 x 5 = 0
1 x 2 = 2
1 x 1 = 1

Output 2:
Enter amount
2000
20 x 100 = 2000
0 x 50 = 0
0 x 10 = 0
0 x 5 = 0
0 x 2 = 0
0 x 1 = 0

Output 3:
Enter amount
123456
1234 x 100 = 123400
1 x 50 = 50
0 x 10 = 0
1 x 5 = 5
0 x 2 = 0
1 x 1 = 1

C Program To Compute Smallest Number of Notes That Will Combine To Give Rs N


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

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