CrazyEngineers
  • Find sum of two numbers

    uday.bidkar

    Member

    Updated: Oct 22, 2024
    Views: 1.1K
    #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
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sahana

    MemberMar 10, 2007

    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)));
    }
    Are you sure? This action cannot be undone.
    Cancel
  • uday.bidkar

    MemberMar 11, 2007

    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
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberMar 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 !!!
    😒
    Are you sure? This action cannot be undone.
    Cancel
  • uday.bidkar

    MemberMar 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<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.
    */

    }
    Are you sure? This action cannot be undone.
    Cancel
  • DEP

    MemberMar 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<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.
    Are you sure? This action cannot be undone.
    Cancel
  • uday.bidkar

    MemberMar 14, 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
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberMar 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Satyajit

    MemberMar 16, 2007

    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);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorMar 16, 2007

    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-
    Are you sure? This action cannot be undone.
    Cancel
  • uday.bidkar

    MemberMar 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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register