Skip to main content
Add two numbers in c without using operator

How to add two numbers without using the plus operator in c

#include<stdio.h>
int main(){
   
    int a,b;
    int sum;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);
    //sum = a - (-b);
    sum = a - ~b -1;
    printf("Sum of two integers: %d",sum);
    return 0;
}



Sample output:

Enter any two integers: 5 10

Sum of two integers: 15

Algorithm:


In c ~ is 1's complement operator. This is equivalent to:  
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b

Comments

Popular posts from this blog

FACTORIAL OF A NUMBER USING RMI IN JAVA

CLIENT PROGRAM: import java.io.*; import java.rmi.*; public class client { public static void main(String args[])throws Exception { try { String s="rmi://"+args[0]+"/abc"; serverint f=(serverint)Naming.lookup(s); DataInputStream m=new DataInputStream(System.in); int n1=Integer.parseInt(m.readLine()); System.out.println("the factorial is"+f.fact(n1)); } catch(Exception e) { System.out.println(e); } } } INTERFACE PROGRAM: import java.rmi.*; public interface serverint extends Remote { int fact(int n)throws Exception; } IMPLEMENTATION PROGRAM: import java.rmi.*; import java.rmi.server.*; public class serverimpl extends UnicastRemoteObject implements serverint { public serverimpl()throws Exception { } public int fact(int n) { int i,c=1; for(i=1;i<=n;i++) { c=i*c; } ret...

CREATING RESPONSIVE LAYOUT

HTML CODE: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>::Responsive Layout::</title> <link rel="stylesheet" href="css/style.css"/> </head> <body class="body"> <header class="mainheader"/> <img src="img/logo.png"  alt="logo"/> <nav>   <ul>     <li class="active"><a  href="#">Home</a></li>     <li><a href="#">About</a></li>     <li><a href="#">Portfoliow</a></li>     <li><a href="#">Contact</a></li>   </ul> </nav> </header> <div class="maincontent">   <div class="content">     ...

simple question with answers

Five friends have their gardens next to one another, where they grow three kinds of crops: fruits (apple, pear, nut, cherry), vegetables (carrot, parsley, gourd, onion) and flowers (aster, rose, tulip, lily).   1. They grow 12 different varieties. 2. Everybody grows exactly 4 different varieties 3. Each variety is at least in one garden. 4. Only one variety is in 4 gardens. 5. Only in one garden are all 3 kinds of crops. 6. Only in one garden are all 4 varieties of one kind of crops. 7. Pear is only in the two border gardens. 8. Paul's garden is in the middle with no lily. 9. Aster grower doesn't grow vegetables. 10. Rose growers don't grow parsley. 11. Nuts grower has also gourd and parsley. 12. In the first garden are apples and cherries. 13. Only in two gardens are cherries. 14. Sam has onions and cherries. 15. Luke grows exactly two kinds of fruit. 16. Tulip is only in two gardens. 17. Apple is in a single garden. 18. Only in one garden next to Zick's is parsley....