uday.bidkar
uday.bidkar
Branch Unspecified
19 Mar 2007

Find sum of two numbers

#include<stdio.h>
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

Branch Unspecified
12 years ago
hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
scanf("%u",&i);
scanf("%u",&i+1);
printf("%d",(*(&(i)))+(*(&(i)+1)));
}
uday.bidkar

uday.bidkar

Branch Unspecified
12 years ago
sahana
hi,
i have used the consecutive memory location of i to store the 2 data.this works.
#include<stdio.h>
#include<conio.h>
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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<stdio.h>
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

Branch Unspecified
12 years ago
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<stdio.h>
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
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

Branch Unspecified
12 years ago
hi i have the programme of sum of 2 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter any 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",&c);
}
12 years ago
Satyajit
hi i have the programme of sum of 2 numbers
#include<stdio.h>
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

Branch Unspecified
12 years ago
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 -

Only logged in users can reply.