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

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

Selection Sort of N numbers: C

Selection sort is a sorting technique which is inefficient on large list of elements. Its simple and easy to understand.

In selection sort, first we select one element and compare it with the rest of the elements. If the compared element is smaller than the selected element, then those numbers are sorted. This approach is continued till the last element in the array index.

The number of iterations is 1 less than the array size.
i.e., if the user enters array size as 5, then there will be 4 iterations.

Below is an example:

Lets assume, user has entered the array size as 5.
and entered 13, 0, -2, 8, 3 as array elements.


First Iteration:

selection-sort-first-iteration



In first iteration, a[0] is selected. and is compared with a[1], a[2], a[3] and a[4]. and is swapped with the least value in the array.


Second Iteration:

selection-sort-second-iteration



In the second iteration, we leave off a[0] and select a[1] and compare it with the rest of the elements in the array i.e., a[2], a[3] and a[4]. and swap the least value with a[1]

Third Iteration:

selection-sort-third-iteration



In third iteration, we select a[2] and compare it with a[3], a[4] and swap the least value to a[2]


Forth Iteration:

selection-sort-forth-iteration



In forth iteration, we select a[3] and compare it with a[4] and swap the least value.

At the end of 4 iterations all array elements are sorted.

Video Tutorial: Selection Sort of N numbers: C


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

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



Since the array index starts from 0, and we need not compare the last element in the array with any other elements, we iterate the outer loop from 0 to N-2.
We iterate the inner loop from i+1 till N-1.

Source Code: Selection Sort of N numbers: C

#include<stdio.h>
#include<conio.h>
 
void main()
{
int a[20], i, j, N, temp;
clrscr();
 
printf("Enter the size of the array\n");
scanf("%d", &N);
 
printf("Enter %d elements\n", N);
for(i=0; i<n ; i++)
 scanf("%d", &a[i]);
 
for( i=0; i<=N-2; i++ )
 for( j=i+1; j<=N-1; j++ )
  if(a[i] > a[j])
  {
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
  }
 
         printf("\nSorted Array:\n");
  for(i=0; i < n ; i++)
   printf("\n%d", a[i]);
 
   getch();
 
}

Output:
Enter the size of the array
5
Enter 5 elements
13
0
-2
8
3
Sorted Array:
-2
0
3
8
13