answersLogoWhite

0

#include<iostream>

int main

{

int x=40, y=2;

std::cout<<"The sum of "<<x<<" and "<<y<<" is "<<x+y<<std::endl;

}

User Avatar

Wiki User

11y ago

Still curious? Ask our experts.

Chat with our AI personalities

ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
More answers

The following example shows one possible solution that emulates the half-adder and full-adder logic gates utilised by the CPU's arithmetic circuitry. There are more efficient ways to manipulate the individual bits to form the total, but the code is deliberately simplified for clarity whilst avoiding all usage of any arithmetic operators (only bitwise operators are used throughout). Note that even the while loop in main uses the sum() function rather than using the prefix increment operator (++) to increment the counter.

Code

#include

#include

int half_adder(int a, int b, int& c)

{

c=a&b; // return the carry bit (AND)

return(a^b); // return the sum bit (XOR)

}

int full_adder(int a, int b, int& c)

{

// The full-adder combines two half-adders such that the

// sum of the first becomes a in the second, while the

// given carry bit becomes b in the second. d and e are

// the carry bits returned from each half-adder

int d=0, e=0;

int s = half_adder( half_adder( a, b, d ), c, e );

c = d|e; // return the carry bit (OR)

return( s ); // return the sum bit

}

int sum(int lhs, int rhs)

{

// some variables...

int total=0, carry=0, pos=1;

int lbit, rbit, sum, tmp;

// repeat while there are bits to process, including

// the carry bit from the previous iteration (OR)

while(lhs|rhs|carry)

{

// mask the least-significant bits from both operands (AND)

lbit=lhs&1;

rbit=rhs&1;

// determine the sum of the two bits and the previous carry bit

sum = full_adder(lbit,rbit,carry);

// determine the correct position of the sum bit using bitshifts

tmp=pos;

while(tmp>>=1)

sum<<=1;

// update the total (OR)

total|=sum;

// bit shift the operands for next iteration

lhs>>=1;

rhs>>=1;

// bit shift the position for next iteration

pos<<=1;

}

return(total);

}

int main()

{

srand((unsigned)time(NULL));

int x,y;

int counter=0;

do

{

x=rand();

y=rand();

int z=sum(x,y);

std::cout<

}while((counter=sum(counter,1))<10);

}

Example output

3583+13622=17205

9318+2059=11377

27249+30832=58081

22356+26289=48645

8301+14324=22625

28315+12451=40766

24452+16165=40617

14819+8566=23385

13642+25686=39328

30981+28975=59956

User Avatar

Wiki User

11y ago
User Avatar

There is no algorithm. To add two numbers just put a + between them.

int a = 1 + 2;

User Avatar

Wiki User

16y ago
User Avatar

int a = 3;

int b = 7;

int c;

c = a + b;

User Avatar

Wiki User

14y ago
User Avatar

Here is an example:

c = a + b;

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you add two numbers in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp