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

(Basic) Find Sum Using Dynamic Memory Allocation: C++

This video tutorial illustrates basics of Dynamic memory allocation in C++. It shows the use of new and delete operator for allocating and deallocating the memory dynamically.

Find the sum of entered elements using dynamic memory allocation in c++.

In cpp, dynamic memory management can be done using the operators new and delete.
Operator new is used to allocate the memory during execution time or run time, the dynamically allocated memory can be freed / released using the operator delete.
Syntax:

< data_type >  < pointer_variable > = new < data_type >[size];

Full Source code

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
#include< iostream .h>
#include< conio .h>
 
void main()
{
  int sum=0, N;
  clrscr();
 
  cout< <"Enter array size\n";
  cin>>N;
 
  int *a = new int[N];
  cout< <"\nEnter "<<N<<" integer numbers"<<endl;
  for(int i=0; i<N; i++)
   cin>>a[i];
 
   cout< <"Input array is.."<<endl;
   for(i=0; i<N; i++)
   {
    cout<<a[i]<<endl;
    sum = sum + a[i]; // sum += a[i];
   }
   cout<<"Total Sum: "<<sum;
 
   delete(a);
   getch();
}

We need not include any extra header file to perform dynamic memory allocation or de-allocation.

int *ptr = new int[N];

here it is mandatory to take pointer variable for dynamic memory allocation.

for de-allocation we use delete operator: delete(ptr);

NOTE:
Student *p = new Student[3];

where Student is a user defined data type, maybe structure or class.

Student *p = new Student; // This is for only 1 student.

Video Tutorial:(Basic) Find Sum Using Dynamic Memory Allocation


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

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



Output:
Enter array size
5
Enter 5 integer numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Total Sum: 15

Array Basics in C++ : Find Sum

Video tutorial to show the basic use of arrays: Initialization, Declaration, Getting values from the users, finding sum of all the array elements etc.

Array is a collection of homogeneous data items.

Full Source Code

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
#include<iostream .h>
#include<conio .h>
 
void main()
{
  //  int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
 
  int a[5], sum = 0;
  clrscr();
 
  cout< <"Enter 5 numbers\n";
  for(int i=0; i&lt;5; i++)
   cin>>a[i];
 
  cout< <"Input array is..\n";
  for(i=0; i&lt;5; i++)
  {
   cout<<a[i]<<endl;
   sum = sum + a[i];  // sum += a[i];
  }
  cout<<"Sum of array elements: "<<sum;
 
  getch();
 
}

In this program we take input from the user and display the user entered numbers and find the sum of all the array elements and display it as well.

Elements are stored from the index number 0. i.e., if the array size is 5, the values will be stored in a[0], a[1], a[2], a[3], a[4];

Ex:
int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
float a[5] = { 1.1, 2.0, 3.3, 1.3, 5.6 };

Video Tutorial: Array Basics in C++ : Find Sum


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

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



Output:
Enter 5 numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Sum of array elements: 15