View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12764 Members
Join this group to post and comment.
uday.bidkar
uday.bidkar • Mar 8, 2007

Find sum of two numbers

#include
int main()
{
int i;
printf("Enter 2 numbers\n");

/*
Write here few lines of code so that user gives 2 numbers as
input(on console) and display the addition of those 2 numbers.. and
the condition is no more variable(s) except for 'i' declared in first line
should be used throughout the program.
*/

return 0;
}

Regards,
Uday
sahana
sahana • Mar 11, 2007
hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include
#include
void main()
{
int i;
scanf("%u",&i);
scanf("%u",&i+1);
printf("%d",(*(&(i)))+(*(&(i)+1)));
}
uday.bidkar
uday.bidkar • Mar 12, 2007
sahana
hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include
#include
void main()
{
int i;
scanf("%u",&i);
scanf("%u",&i+1);
printf("%d",(*(&(i)))+(*(&(i)+1)));
}
The memory pointed to &i+1 is not valid memory that you can use for operations as you have not allocated the same neither asked compiler to allocate it for you on stack. Hence the program may crash because of invalid memory operations. So solution not acceptable!

-Uday
DEP
DEP • Mar 12, 2007
uday.bidkar
The memory pointed to &i+1 is not valid memory that you can use for operations as you have not allocated the same neither asked compiler to allocate it for you on stack. Hence the program may crash because of invalid memory operations. So solution not acceptable!

-Uday
hi uday,
i have tried a lot to solve this problem and believe me whether you accept it or not pointers is the only way to solve it.

i guess i quite agree with the solution provided by sahana and am unable to figure out any other solution.

so please post any other solution if you have it !!!
😒
uday.bidkar
uday.bidkar • Mar 14, 2007
DEP
hi uday,
i have tried a lot to solve this problem and believe me whether you accept it or not pointers is the only way to solve it.

i guess i quite agree with the solution provided by sahana and am unable to figure out any other solution.

so please post any other solution if you have it !!!
😒
There is a way to solve this. But before i post the answer for this, I would like to relax the condition and wait for few more days.
And the relaxed question is
#include
int main()
{
/*
Write here few lines of code so that user gives 2 numbers as
input(on console) and display the addition of those 2 numbers.. and the
condition is only one integer variable should be declared and used
throughout the program. You are now free to declare the variable as you want.
*/

}
DEP
DEP • Mar 14, 2007
uday.bidkar
There is a way to solve this. But before i post the answer for this, I would like to relax the condition and wait for few more days.
And the relaxed question is
#include
int main()
{
int *a;
cin>>*a;
cin>>*(a+1);
cout<<*a+*(a+1);
}
well uday i am sorry but this is the only solution that i have worked out.and i guess this is the c++ equivalent code of what sahanna had posted.
please please please tell the answer. am dying to know it.
uday.bidkar
uday.bidkar • Mar 15, 2007
DEP
please please please tell the answer. am dying to know it.
Here is answer to the problem when variable is not
already declared.

int main()
{
static int i = 0;
if( !i )
{
i = -1 ;
printf( "The Sum is %d\n", main() + main() ) ;
return 0 ;
}
printf( "Enter a number\n" );
scanf( "%d",&i );
if( !i )
{
i = -1 ;
return 0 ;
}
else
{
return i ;
}
}

Regards,
Uday
sahana
sahana • Mar 16, 2007
wow.that was really smart.
i accept my mistake.well actually i actually got the output ,as i was lucky that the unallocated memory was not a part of anyother program, and didnt overwrite any data of another program.
Satyajit
Satyajit • Mar 17, 2007
hi i have the programme of sum of 2 numbers
#include
void main()
{
int a,b,c;
printf("enter any 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",&c);
}
Kaustubh Katdare
Kaustubh Katdare • Mar 17, 2007
Satyajit
hi i have the programme of sum of 2 numbers
#include
void main()
{
int a,b,c;
printf("enter any 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",&c);
}
Satyajit, mind reading the first post in this thread? There is a special condition to be followed.

-The Big K-
uday.bidkar
uday.bidkar • Mar 19, 2007
Time to post the answer and here it is...

int main()
{
int i ;
printf("Enter two numbers\n");
printf("Sum is %d\n",(scanf("%d",&i)?i:0) + (scanf("%d",&i)?i:0));
return 0 ;
}

Regards,
Uday

Share this content on your social channels -