- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this section we will see how to print the sum of two numbers without using any type of operator into our program.

This problem is tricky. To solve this problem we are using minimum width field of printf() statement. For an example if we want to put x number of spaces before “Hello” using printf() we can write this. Here printf() takes the width and then the character that will be printed. In this case we are writing blank space.

#include<stdio.h> main() { int x = 10; printf("%*cHello", x, ' '); }

Hello

Now let us see how this functionality can help us to get the result of sum in our code. We take x and y as input to get result of x + y. So using this procedure we will create x number of spaces followed by y number of spaces. Then we take the returned value of the printf() as our result. We know that the printf() returns the length of that string.

#include<stdio.h> int add(int x, int y) { int len; len = printf("%*c%*c", x, ' ', y, ' '); return len; } main() { int x = 10, y = 20; int res = add(x, y); printf("\nThe result is: %d", res); }

The result is: 30

- Related Questions & Answers
- Find the Sum of two Binary Numbers without using a method in C#?
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- C program to find sum and difference of two numbers
- 8085 program to sum of two 8 bit numbers without carry
- How to find the Sum of two Binary Numbers using C#?
- C++ program to Find Sum of Natural Numbers using Recursion
- Program to find remainder without using modulo or % operator in C++
- Add two numbers using ++ operator in C++.
- Java program to swap two numbers using XOR operator
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- How to sum two integers without using arithmetic operators in C/C++ Program?
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Maximum of four numbers without using conditional or bitwise operator in C++
- Program to find the maximum difference between the index of any two different numbers in C++
- C program to print number series without using any loop

Advertisements