0
1.5kviews
write a program in C++ to calculate sum of 10 float number using pointer
1
45views

## C++ Program to Calculate the Sum of 10 Float Numbers using Pointers

• The below C++ program creates the following things:

• 10 variables named "n1 to n10" to store 10 float numbers.

• 10 Pointer variables named "ptr1 to ptr10" to store the address of these 10 float numbers.

• One float variable named "sum" to store the sum of these 10 float numbers.

• Then take the 10 float numbers as input from the user and store them into the variables "n1 to n10" respectively.

• Then allocate the addresses of variable "n1 to n10" to pointer variables "ptr1 to ptr10" respectively.

• Finally, perform the addition of 10 float numbers using the pointer variables and Sum of the 10 Float Numbers is displayed.

C++ Program -

#include <iostream>

using namespace std;

int main()
{
float n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;    // Variables to store 10 float numbers
float *ptr1,*ptr2, *ptr3, *ptr4, *ptr5, *ptr6, *ptr7, *ptr8, *ptr9, *ptr10;
//  Pointer variables to store the address of these 10 float numbers
float sum;    // A variable to store the sum of these 10 float numbers

cout << "Please Enter 10 Float Numbers: "<< endl;
// Ask the user to input 10 float numbers to store into the  variables
cin>>n1>>n2>>n3>>n4>>n5>>n6>>n7>>n8>>n9>>n10;
// Get the 10 float numbers from the user

ptr1=&n1;    // Allocate the address of variable n1 to ptr1
ptr2=&n2;    // Allocate the address of variable n2 to ptr2
ptr3=&n3;    // Allocate the address of variable n3 to ptr3
ptr4=&n4;    // Allocate the address of variable n4 to ptr4
ptr5=&n5;    // Allocate the address of variable n5 to ptr5
ptr6=&n6;    // Allocate the address of variable n6 to ptr6
ptr7=&n7;    // Allocate the address of variable n7 to ptr7
ptr8=&n8;    // Allocate the address of variable n8 to ptr8
ptr9=&n9;    // Allocate the address of variable n9 to ptr9
ptr10=&n10;    // Allocate the address of variable n10 to ptr10

sum=*ptr1+*ptr2+*ptr3+*ptr4+*ptr5+*ptr6+*ptr7+*ptr8+*ptr9+*ptr10;
// Addition of 10 float numbers using the pointer variables

cout<<"Sum of the 10 Float Numbers : "<<sum;
// Show the result of addition of 10 float numbers
return 0;
}


Output -