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

Calculate Distance in Nautical Miles: C Program

Write a C program to receive values of latitude(L1, L2) and longitude(G1, G2), in degrees, of two places on the earth and output the distance (D) between them in nautical miles.

Formula For Distance in Nautical Miles

D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );

where lat1, lat2 are latitudes. lon1, lon2 are longitude.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Logic To Find Distance In Nautical Miles

User input the values of latitude(lat1, lat2) and longitude(lon1, lon2 ), in degrees. We convert it to radians. Next we use the formula below:
D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 – lon1) );
and out put the result on to the console.

Source Code: Calculate Distance in Nautical Miles: C Program

 
#include < stdio.h >
#include < math.h >

int main()
{
    float lat1, lat2, lon1, lon2, D;
    const float PI = 3.141592;

    printf("Enter latitude(L1, L2)\n");
    scanf("%f%f", &lat1, &lat2);

    printf("Enter longitude(G1, G2)\n");
    scanf("%f%f", &lon1, &lon2);
  
    /* Convert Degrees To Radian */    lat1 = lat1 * ( PI / 180.0 );
    lat2 = lat2 * ( PI / 180.0 );
    lon1 = lon1 * ( PI / 180.0 );
    lon2 = lon2 * ( PI / 180.0 );

    D = 3963 * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1) );

    printf("Distance in nautical miles is %f\n", D);

    return 0;
}

Output:
Enter latitude(L1, L2)
52.3
43.2
Enter longitude(G1, G2)
12.3
15.6
Distance in nautical miles is 647.682434

Calculate Distance in Nautical Miles: C Program


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

YouTube Link: https://www.youtube.com/watch?v=d88pWBloIOE [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 Convert Polar To Cartesian Co-ordinates

Write a C program to receive Polar co-ordinates(r, θ) and convert them into Cartesian co-ordinates(x, y). Ask the user to enter theta(θ) value in degrees.

Related Read:
C Program To Convert Cartesian To Polar Co-ordinates

Formula To Convert Polar To Cartesian Co-ordinates

x = r * cos(θ);

y = r * sin(θ);

where x and y are Cartesian co-ordinates. r and θ are polar co-ordinates.

Formula To Convert Degree To Radian

radian = degree * (PI/180.0);
where PI is 3.141592

Source Code: Convert Polar To Cartesian Co-ordinates: C Program

 
#include < stdio.h >
#include < math.h >

int main()
{
    float x, y, r, theta;
    const float PI = 3.141592;

    printf("Enter Polar Co-ordinates(r, theta)\n");
    scanf("%f%f", &r, &theta);

    /* Convert angle from Degree To Radian */    theta = theta * (PI / 180.0); 

    x = r * cos(theta);
    y = r * sin(theta);

    printf("Cartesian Co-ordinates (x, y) = (%f, %f)\n", x, y);

    return 0;
}

Output:
Enter Polar Co-ordinates(r, theta)
5.01
53.131
Cartesian Co-ordinates (x, y) = (3.005938, 4.008047)

Convert Polar To Cartesian Co-ordinates: C Program


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

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