
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C Program to find sum of two numbers without using any operator
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.
Example Code
#include<stdio.h> main() { int x = 10; printf("%*cHello", x, ' '); }
Output
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.
Example Code
#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); }
Output
The result is: 30
- Related Articles
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- Find the Sum of two Binary Numbers without using a method in C#?
- Find XOR of two number without using XOR operator in C++
- Program to find remainder without using modulo or % operator in C++
- C program to find sum and difference of two numbers
- 8085 program to sum of two 8 bit numbers without carry
- Division without using ‘/’ operator in C++ Program
- C++ program to Find Sum of Natural Numbers using Recursion
- Java program to swap two numbers using XOR operator
- How to find the Sum of two Binary Numbers using C#?
- How to sum two integers without using arithmetic operators in C/C++ Program?
- Largest of two distinct numbers without using any conditional statements or operators
- Add two numbers using ++ operator in C++.
- Maximum of four numbers without using conditional or bitwise operator in C++
- C++ Program to Find the Product of Two Numbers Using Recursion
