0% found this document useful (0 votes)
8 views8 pages

Average Calculation in C++ Program

The document contains code snippets for 8 programs that perform basic calculations and operations: 1) calculating the average of numbers, 2) calculating simple interest, 3) swapping values of two variables using a third variable and without a third variable, 4) checking if a number is even or odd, 5) summing the digits of a number, 6) reversing the digits of a number. Each code snippet is followed by sample input/output demonstrating its functionality.

Uploaded by

Poonam Rana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Average Calculation in C++ Program

The document contains code snippets for 8 programs that perform basic calculations and operations: 1) calculating the average of numbers, 2) calculating simple interest, 3) swapping values of two variables using a third variable and without a third variable, 4) checking if a number is even or odd, 5) summing the digits of a number, 6) reversing the digits of a number. Each code snippet is followed by sample input/output demonstrating its functionality.

Uploaded by

Poonam Rana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] to find average of n numbers.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int i,a[30],n;
float avg,sum=0;
cout<<"\n Enter the number of elements whose average is to be found ";
cin>>n;
cout<<"\n Enter the numbers ";
for(i=0;i<n;i++)
{ cin>>a[i];
sum=sum+a[i];
}
avg=sum/n;
cout<<"\n Average = "<<avg;

OUTPUT :
Enter the number of elements whose average is to be found : 5
Enter the numbers 3 7 4 9 10
Average = 6.6

2. WAP to calculate simple interest .


#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float p,r,t,s;
cout<<"\n Enter principle amount ";
cin>>p;
cout<<"\n Enter rate of interest per annum ";
cin>>r;
cout<<"\n Enter time in years ";
cin>>t;
s=(p*r*t)/100;
cout<<"\n Simple Interest = "<<s;

OUTPUT :
Enter principle amount in Rs.: 1200
Enter rate of interest per annum: 5
Enter time in years: 3
Simple Interest = Rs. 180

3. WAP to swap the value of two variables .


(1) Using 3rd variable :
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float a,b,t;
cout<<"\n Enter two numbers: ";
cin>>a>>b;
cout<<"\n Value of a and b : "<<a<<" "<<b;
t=a;
a=b;
b=t;
cout<<"\n Swapped values of a and b : "<<a<<" "<<b;

OUTPUT:
Enter two numbers : 4 7
Value of a and b : 4 7
Swapped values of a and b : 7 4

(2) Without using 3rd variable :

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float a,b,t;
cout<<"\n Enter two numbers: ";
cin>>a>>b;
cout<<"\n Value of a and b : "<<a<<" "<<b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\n Swapped values of a and b : "<<a<<" "<<b;

OUTPUT:
Enter two numbers : 3 7
Value of a and b : 3 7
Swapped values of a and b : 7 3

5. WAP to check whether a no is even or odd.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a;
cout<<"\n Enter a number: ";
cin>>a;
if(a%2==0)
{
cout<<"\n Number is even " ;
}
else
{
cout<<"\n Entered number is odd " ;
}
}

OUTPUT:
Enter a number: 7
Entered number is odd

7. WAP to sum the digits of a number.


#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,p,sum=0;
cout<<"\n Enter a number: ";
cin>>n;
while(n!=0)
{
p=n%10 ;
sum=sum+p;
n=n/10;
}
cout<<" Sum of digits = "<<sum;
}
OUTPUT:
Enter a number: 1234
Sum of digits = 10

8. WAP to reverse the digits of a number.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,p,rev=0;
cout<<"\n Enter a number: ";
cin>>n;
while(n!=0)
{
p=n%10 ;
rev=rev*10+p;
n=n/10;
}
cout<<" Reverse = "<<rev;
}

OUTPUT:
Enter a number: 1234
Reverse = 4321

Common questions

Powered by AI

The critical steps in determining if a number is even or odd in a C++ program involve receiving a number as input and applying the modulus operator to check the remainder of the division of the number by 2. If the remainder is zero, the number is even; otherwise, it is odd. This method is efficient because it performs a single arithmetic operation and a simple comparison, minimizing computational overhead and ensuring fast execution .

Using a method to swap two values without a third variable is particularly beneficial in systems where memory usage is a constraint. This technique, utilizing arithmetic operations, eliminates the need for additional memory allocation by directly manipulating the values of the variables. It's highly advantageous in embedded systems or applications where resources are minimal. However, it requires careful handling as incorrect operations can lead to data overflow or loss, especially when dealing with integer overflows in poorly managed code .

A simple program calculates the simple interest by utilizing the formula SI = (P * R * T) / 100, where P is the principal amount, R is the rate of interest per annum, and T is the time period in years. The user must input these three values for the program to execute successfully and output the calculated simple interest .

To calculate the average of a sequence of numbers, sum all the numbers and then divide the sum by the number of elements. In a C++ program, this is implemented using an array to store the numbers, a loop to accumulate their sum, and then a division operation to find the average. The program prompts the user to enter the number of elements and the elements themselves, performs the sum inside a loop, and finally divides the sum by the total count to get the average .

Reversing the digits of a number can be required in cryptography, data encoding, or specific problem-solving tasks. In C++, reversing digits involves repeatedly extracting the last digit of the number and appending it to a new variable that builds the reversed number. This is done by taking the modulus of 10 of the number to get the last digit, multiplying the resulting reversed number by 10 to shift its digits, and adding the new digit to it, followed by dividing the original number by 10 to discard the last digit. This loop continues until the original number is reduced to zero .

Summing the digits of a number is useful in various scenarios such as validating numerical inputs, generating hash codes, or solving specific algorithmic problems like digit root calculations. In C++, this is implemented utilizing a loop where the program continuously extracts the last digit using modulus operation, adds it to a running total, and then removes the digit from the number using integer division, repeating the process until all digits are processed .

When swapping two variables using a third temporary variable in C++, the primary consideration is to ensure that the values are copied correctly without altering the original data. The temporary variable holds the value of the first variable, allowing the first variable to receive the value of the second one, and subsequently, the second variable receives the value stored in the temporary variable. This approach guarantees that neither of the original variable values are lost or overwritten during the swap process .

You might also like