pages

Tuesday, 4 May 2021

c-program to calculate sum and average of five number by using pointer in c-programming

 program to receives 5 integers and return the sum, average of these numbers by using pointers in c-programming.

In this c-program , we calculate to Find the Sum and Average of a number by Using the Pointer by taking input from the user .

#include<stdio.h>
#include<conio.h>
void main()
{
         float a[5],ave=0,sum=0;
         int i;
         float *pa,*pave=0,*psum=0;
         pa=a;
         pave=&ave,psum=&sum;
         printf("enter the number:-");
         for(i=0;i<5;i++){
                  scanf("%f",(pa+i));
                  *psum=*psum+*(pa+i);
         }
         printf("\nthe sum of the number is %.2f",*psum);
         *pave=*psum/5;
         printf("\nthe average of the number is %.2f",*pave);
         getch();
}

output:-



Another way to solve this problem are follow :-


#include<stdio.h>
#include<conio.h>
void input(int *p,int  size);
void display(int *p,int size);
void main()
{
         int a[5];
         input(a,5);
         display(a,5);
         getch();
}
void input(int *p,int  size)
{
         int i;
         printf("enter %d number",size);
         for(i=0;i<size;i++)
                  scanf("%d",(p+i));
}
void display(int *p,int size)
{
         int i,sum=0;
         float ave=0;
         printf("\nthe number are as follow:-");
         for(i=0;i<size;i++){
                  printf("%d ",*(p+i));
                  ave=ave+*(p+i);
                  sum=sum+*(p+i);
         }
         printf("\n the sum of the number is %d",sum);
         ave=ave/5.00;
         printf("\n \naverage of the number is %.2f",ave);
}

Output :- 




















No comments:

Post a Comment