In: Computer Science
CODE IN C PLEASE
Ask for user to input 4 different numbers
Use pointers in calculations instead of variables to calculate the sum of those 4 numbers
Print the sum
Use a pointer to a pointer for print operation
Answer:
Program:
#include<stdio.h>
int main()
{
// variable declaration
int a,b,c,d,tot,*total,**sum,*p,*q,*r,*s;
// prompts the message
printf("Enter four different numbers: ");
// reads 4 integer values from the keyboard, and
stores in the variables a,b,c,d
scanf("%d%d%d%d",&a,&b,&c,&d);
// storing the addresses of variables a,b,c,d into
pointer variables p,q,r,s respectively
p=&a;
q=&b;
r=&c;
s=&d;
// adding the contents of a,b,c,d using pointers and
storing into the variable 'tot'
tot=(*p+*q+*r+*s);
// The address of 'tot' is stores into the pointer
variable 'total'
total=&tot;
// The address of the pointer variable 'total' is
again stored in pointer to poiter 'sum'
sum=&total;
// printing the resultant sum value using pointer to
pointer
printf("The sum of four numbers is=%d",**sum);
return 0;
}
Program Screenshot:
Output Screenshot: