w3resource logo


C Programming Exercises

C Exercises: Add two numbers

Secondary Nav

C Pointer : Exercise-4 with Solution

Write a program in C to add two numbers using pointers.
Test Data :
Input the first number : 5
Input the second number : 6
Expected Output :

The sum of the entered numbers is : 11  

C Code:

#include <stdio.h>
int main()
{
   int fno, sno, *ptr, *qtr, sum;
   
   printf("\n\n Pointer : Add two numbers :\n"); 
   printf("--------------------------------\n");   
 
   printf(" Input the first number : ");
   scanf("%d", &fno);
   printf(" Input the second  number : ");
   scanf("%d", &sno);   
 
   ptr = &fno;
   qtr = &sno;
 
   sum = *ptr + *qtr;
 
   printf(" The sum of the entered numbers is : %d\n\n",sum);
 
   return 0;
}

Flowchart :

Flowchart: Add two numbers

C Practice online:


#include <stdio.h>
int main()
{
   int fno, sno, *ptr, *qtr, sum;
   
   printf("\n\n Pointer : Add two numbers :\n"); 
   printf("--------------------------------\n");   
 
   printf(" Input the first number : ");
   scanf("%d", &fno);
   printf(" Input the second  number : ");
   scanf("%d", &sno);   
 
   ptr = &fno;
   qtr = &sno;
 
   sum = *ptr + *qtr;
 
   printf(" The sum of the entered numbers is : %d\n\n",sum);
 
   return 0;
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables