Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Help me in a simple Program.

  1. #1
    Join Date
    Mar 2010
    Location
    Satara, Mh, India.
    Beans
    62
    Distro
    Ubuntu 10.04 Lucid Lynx

    Help me in a simple Program.

    please check out the attachment,
    I've written an program for finding two variables but it doesn't works

    Also I've inserted an picture of the terminal showing the output for this program.

    Friends.. I'm waiting for your replies ....
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2010
    Location
    Kolkata
    Beans
    275
    Distro
    Ubuntu 14.10 Utopic Unicorn

    Re: Help me in a simple Program.

    I think you should start a and b with 1, because anything multiplying with 0 returns 0. So the condition is getting true each time.

    BTW remember initializing those variables with 1 will never make your condition true, so you won't get any output.

  3. #3
    Join Date
    Aug 2007
    Location
    I live in the universe.
    Beans
    195
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Help me in a simple Program.

    As I understand you want to find a,b for:

    (a+b)^2 = a^2 + b^2

    This is only possible if a=0 ( any value of b) or b=0 ( any value of a )

    Am I missing anything?

  4. #4
    Join Date
    Oct 2010
    Beans
    13
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help me in a simple Program.

    Actually, a*a + 2*a*b + b*b = (a+b) * (a+b)

    in ur case, the condition is satified only when b == 0 . hence the output.

    change the formula and things will be fine.

    Darshan

  5. #5
    Join Date
    Sep 2007
    Location
    Netherlands
    Beans
    454
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Help me in a simple Program.

    Quote Originally Posted by shankhs View Post
    As I understand you want to find a,b for:

    (a+b)^2 = a^2 + b^2

    This is only possible if a=0 ( any value of b) or b=0 ( any value of a )

    Am I missing anything?
    Yep you are: 2*a*b
    Never upgrade your working system to the newest release without thorough testing on the actual hardware you will be running it on.
    Never perform a system update in a live session
    Installed Ubuntu Touch 1.0 on my Nexus 7. And loving it!

  6. #6
    Join Date
    Mar 2010
    Location
    Satara, Mh, India.
    Beans
    62
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help me in a simple Program.

    @krishnandu.sarkar you are true,
    but Friends, somebody told me that, there are really two numbers, and those are non-zero.
    so i thought, to make a program to search it.

    look at the output carefully, you will get noticed that, only one variable is getting incremented while both should get incremented..
    like..

    for a=0, b should increment from 0 to 999,
    again, for a=1, b from 0-999, and so on upto a becomes 999.

    please help.


  7. #7
    Join Date
    Aug 2007
    Location
    I live in the universe.
    Beans
    195
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Help me in a simple Program.

    (a+b)^2 = a^2+b^2
    =>a^2+b^2+2ab=a^2+b^2
    =>2ab=0

    So either a=0 or b=0

  8. #8
    Join Date
    Mar 2010
    Location
    Satara, Mh, India.
    Beans
    62
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help me in a simple Program.

    @ shankhs,

    (a+b)^2 = a^2+b^2

    is not possible,
    actually, the correct formula is :



    (a+b)^2 = a^2+b^2 + 2*a*b

  9. #9
    Join Date
    Apr 2010
    Location
    Kolkata
    Beans
    275
    Distro
    Ubuntu 14.10 Utopic Unicorn

    Re: Help me in a simple Program.

    Quote Originally Posted by rshinde70 View Post
    @krishnandu.sarkar you are true,
    but Friends, somebody told me that, there are really two numbers, and those are non-zero.
    so i thought, to make a program to search it.

    look at the output carefully, you will get noticed that, only one variable is getting incremented while both should get incremented..
    like..

    for a=0, b should increment from 0 to 999,
    again, for a=1, b from 0-999, and so on upto a becomes 999.

    please help.


    Well...how would we know what formula or what do you want to implement. I just corrected the logic.

  10. #10
    Join Date
    Apr 2010
    Location
    Kolkata
    Beans
    275
    Distro
    Ubuntu 14.10 Utopic Unicorn

    Re: Help me in a simple Program.

    Do you want this thing??

    #include<stdio.h>
    main()
    {


    // A PROGRAM TO FIND a AND b IF (a*a)+(2*a*b)+(b*b) = (a*a)+(b*b)

    int i;
    int a=0, b=0;
    int x, y;

    for(i=0;i<1000;i++)
    {

    x=(a*a)+(2*a*b)+(b*b);

    y=(a*a)+(b*b);

    if(x==y)
    {
    printf("\na=%d\tb=%d",a,b);
    a++;
    b++;
    }

    }

    if(a==999 && b==999)
    printf("\nElements not found!");


    }

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •