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

Replies

  • sahana
    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)));
    }
  • uday.bidkar
    uday.bidkar
    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
    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
    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
    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
    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
    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
    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
    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
    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

You are reading an archived discussion.

Related Posts

Hi frnds i am rohan i am doing mech engg i am in need of animation of francis turbine! can any 1 help me????????????????? plz if anybody having it or...
Hello CEans! ๐Ÿ˜€ My question would be better understood by the CEans who work in the IT industry, or the ones who get a 'good' amount of important emails ๐Ÿ˜...
CEans, I found an interesting puzzle on the Internet here - Puzzle#110 - https://www.archimedes-lab.org/page10b.html The puzzle is as follows - Find the "Professor Gibbus' Angle" below - Difficulty Level: (2/5)...
CEans who are Guitarists - ๐Ÿ˜Ž Can anyone of you get me the staff for this song on YouTube? - Pachelbel's Canon https://youtube.com/watch?v=6uJXC5WqbeU&mode=related&search= -The Big K-
get free is codes download here..