#include<stdio.h>
int main()
{
int a, b, c, *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &b;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
c = *ptr1 + *ptr2;
printf("Using *ptr1 + *ptr2 : %d + %d = %d\n", a, b, c);
c = *(&a) + *(&b);
printf("Using *(&a) + *(&b) : %d + %d = %d\n", a, b, c);
return 0;
}
Output: Enter 2 numbers 25 25 Using *ptr1 + *ptr2 : 25 + 25 = 50 Using (&a) + *(&b) : 25 + 25 = 50
Logic To Add Two Numbers using Pointers
Here we are storing the address of variable a in pointer variable ptr1 and address of variable b in pointer variable ptr2.
We know that any address preceded by * would fetch the value present at that address. So we use following code to get the addition of 2 numbers result using pointers.
c = *ptr1 + *ptr2;
Source Code: C Program To Add Two Numbers using Pointer and Function
#include<stdio.h>
void addition(int, int, int*);
int main()
{
int a, b, c;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
addition(a, b, &c);
printf("%d + %d = %d\n", a, b, c);
return 0;
}
void addition(int x, int y, int *z)
{
*z = x + y;
}
Output: Enter 2 numbers 10 40 10 + 40 = 50
Logic To Add Two Numbers using Pointers and Function
We ask the user to enter 2 numbers and store it inside address of variables a and b. Next we pass the values of a and b and address of variable c to a function called addition(). And then print the result in main method itself.
The motive is to change the value present at address of variable c. That way we can print the result directly inside main method itself, without our function addition() returning any value back to calling function(main()).
Inside addition() method, we add the values passed in first 2 arguments and then store the result back in the address location sent to addition() function as third argument. Function addition() doesn’t return any value, so it has void as its return type.
Lets write a C program to add first seven terms of the following series 1 / 1! + 2 / 2! + 3 / 3! + ….. + 7 / 7!
We’ve also written the C program to ask the user to enter the number of terms of the series that has to be added. You can find code to both of it below.
int main()
{
int num = 1, count;
float sum = 0.0, fact;
while(num <= 7)
{
fact = 1;
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
sum = sum + (num / fact);
num++;
}
printf("Sum of series is %f\n", sum);
return 0;
}
Output: Sum of series is 2.718056
In above code, while loop iterates from 1 to 7. For each iteration for loop calculates the factorial of the selected number – which is present in variable num. Outside for loop, we add the individual series elements. At the end of while loop execution, variable sum will have sum of 7 terms of the series.
Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!
printf("Sum of %d terms of series is %f\n", limit, sum);
return 0;
}
int main()
{
int num = 1, count, limit;
float sum = 0.0, fact;
printf("Enter the number of terms\n");
scanf("%d", &limit);
while(num <= limit)
{
fact = 1;
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
sum = sum + (num / fact);
num++;
}
printf("Sum of %d terms of series is %f\n", limit, sum);
return 0;
}
Output 1: Enter the number of terms 7 Sum of 7 terms of series is 2.718056
Output 2: Enter the number of terms 8 Sum of 8 terms of series is 2.718254
In above code, we ask the user to enter the number of terms in the series that needs to be added. While loop iterates until the user entered number of times. Inside for loop we calculate the factorial of the selected number(number is selected by while loop and it’ll be present in variable num). Outside the for loop we add the individual series element. At the end of execution of while loop we’ll have sum of the series until user entered number of terms in the series.
Lets write a C program to perform addition of 2 numbers without using plus symbol or the addition operator(+).
In this video tutorial we are using ~(tilde symbol) bitwise complement operator to perform the operation to get to the anticipated result.
Example: If user enters 2 numbers. a = 8 and b = 7. To add 8 and 7 we use result = a + b. And the result will be 15. But in this C program we are not using plus(+) operation, but still we must get the same result 15(for above input).
Logic To Add Two Numbers without using Plus Operator
If num = 7; result = ~num; will give 1s complement of 7, that is -8. result = -(~num); will give 8. result = -(~num)-1; will give 7.
Now using this logic, we ask the user to enter 2 numbers. If user enters a = 8 and b = 7. We use the below logic to perform addition, without using plus symbol:
printf("Addition of %d and %d is %d\n", a, b, add);
return 0;
}
#include < stdio.h >
int main()
{
int a, b, add;
printf("Enter 2 numbers for addition\n");
scanf("%d%d", &a, &b);
add = a-~b-1;
printf("Addition of %d and %d is %d\n", a, b, add);
return 0;
}
Output 1: Enter 2 numbers for addition 1 2 Addition of 1 and 2 is 3
Output 2: Enter 2 numbers for addition -1 2 Addition of -1 and 2 is 1
Output 3: Enter 2 numbers for addition -1 -2 Addition of -1 and -2 is -3
Output 4: Enter 2 numbers for addition -5 5 Addition of -5 and 5 is 0
Output 5: Enter 2 numbers for addition 5 -10 Addition of 5 and -10 is -5
Video Tutorial: C Program To Add Two Numbers without using Plus Operator
Note: These kind of tricky C programming questions can be asked in your viva or company interviews or competitive exams or even in your academic exams. So be prepared.
c += a; is equal to writing c = c + a; c -= a; is equal to writing c = c – a; c *= a; is equal to writing c = c * a; c /= a; is equal to writing c = c / a; c %= a; is equal to writing c = c % a;
In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).
We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program. Simple Calculator Application In C
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
#include < stdio.h >
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
switch(choice)
{
case 'a': printf("%d + %d = %d\n", a, b, (a+b));
break;
case 'b': printf("%d - %d = %d\n", a, b, (a-b));
break;
case 'c': printf("%d x %d = %d\n", a, b, (a*b));
break;
case 'd': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output Enter your choice a. Addition b. Subtraction c. Multiplication d. Division c Enter 2 integer numbers 5 10 5 x 10 = 50
In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case'+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case'-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case'*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case'/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
</stdio.h>
#include
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case '+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case '-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case '*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case '/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division 1 Enter 2 integer numbers. Format: a + b 50 + 60 50 + 60 = 110
Here we ask the user to enter values as well as the operation to be performed. Operands and operator.
Note 1: There is no need of curly braces inside case. Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed. Note 3: default case is optional. And the code inside it executes only when non of the cases match. Note 4: Here switch, case, break, default are all keywords / reserve words.