Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

So i have to make a program which finds abc, three digit number which is true to a2+b2+c2=a+b+c.(Dont know what was unclear on this in my first post...)I made a code for this but i dont know if its good because it gives me only 4 numbers in range of 100-1000.Can any1 check it pls if its correct.There is only 4 answers??Or i made smtg wrong and thats why i gives me only 4.Thx in advance


C#
static int a;
static int b;
static int c;
static int abc;
static int sum;
static int sum2;




public static void main(String [] args){

    for(abc=100;abc<1000;abc++){

        a=abc/100;
        b=abc%100/10;
        c=abc%10;
        sum=a+b+c;

        sum2 = (int) (Math.pow(a, 2)+ Math.pow(b, 2)+Math.pow(c, 2));
    if(sum==sum2){
        System.out.println(" "+abc);

    }



    }
Posted
Updated 14-Mar-15 10:01am
v2
Comments
Maciej Los 14-Mar-15 17:03pm    
Are you sure your code is correct? It looks like you missed one bracket: }

Your answer is correct, this makes sense since if any digit was greater than 1 the condition must be false:

C#
public static void ABC()
    {
        int abc=0,a2b2c2=0,num=0;
        for(int a=0; a<10; a++)
        {
            for(int b=0; b<10; b++)
            {
                for(int c=0; c<10; c++)
                {
                    abc=a+b+c;
                    a2b2c2=a*a+b*b+c*c;
                    num=100*a+10*b+c;
                    if(abc == a2b2c2 && num>=100)
                        System.out.println(num);
                }
            }
        }
    }
 
Share this answer
 
Comments
Member 11524654 14-Mar-15 16:55pm    
So there are only 4 three digited numbers in range of 100-1000 which is true for that?
I do not have a lot experience with Java, so i tested your code (with small changes) in C#:
C#
for(int abc=100;abc<1000;abc++)
{
    int a=abc/100;
    int b=abc%100/10;
    int c=abc%10;
    int sum=a+b+c;
    int sum2 = (int)(System.Math.Pow(a,2) + System.Math.Pow(b,2) + System.Math.Pow(c,2));
    if(sum==sum2)
    {
        Console.WriteLine("abc={0} a={1} b={2} c={3} sum={4} sum2={5}", abc, a, b, c, sum, sum2);
    }
}



and using Linq query too:
C#
var qry = Enumerable.Range(100, 900).
            Select(z=> new{a = (int)z/100, b = (int)z%100/10, c = (int)z%10}).
            Select(n=> new{a = n.a, b = n.b, c = n.c, sum = n.a+n.b+n.c, sum2 = (int)(Math.Pow(n.a,2) + Math.Pow(n.b,2) + Math.Pow(n.c,2))}).
            Where(x=>x.sum==x.sum2).
            Select(f=>f);


Result is the same:
a b c sum sum2
1 0 0 1 1
1 0 1 2 2
1 1 0 2 2
1 1 1 3 3
 
Share this answer
 
Comments
Member 11524654 14-Mar-15 17:34pm    
Ty for ur answers
Maciej Los 14-Mar-15 17:35pm    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900