So the question is following:
Given three integers as input determine if the values were entered in ascending order (from smallest to largest) and whether those values represent a Pythagorean Triplet.Click Here

Examples
*Enter the three values to test (a b c): 3 4 5
a < b < c Status: 1
Pythagorean Triplet Status: 1

Enter the three values to test (a b c): 4 3 5
a < b < c Status: 0
Pythagorean Triplet Status: 0

Enter the three values to test (a b c): 3 5 6
a < b < c Status: 1
Pythagorean Triplet Status: 0*

Here is my attempt on the question:

/*
 * Description: Program is aimed towards to take three inputs from user.
 * We have to check that the numbers are in ascending order.
 * We are not allowed to use any conditional statement only, basic math
 * operations like (/,%,+,-,etc), but no logical operators or ternary operator,etc.
 * If the numbers are in ascending order then output is 1, or else 0.
 * If the number is in ascending order, then we check if it is pythogorean triplet and if it
 * then output is 1 or else 0.
 * Condition: If the number are not ascending order, then by default its pythogorean triplet answer
 * should also be 0.
 */
#include <stdio.h>
#include <math.h>
int main()
{
  int a,b,c,x,sum,e,f,y;
  /*
   * a = number 1
   * b = number 2
   * c = number 3
   * e = holds the value obtained after modding a,b
   * f = holds the value obtained after modding c,b
   * y = holds the value obtained after modding e,f
   */

  printf("Enter the numbers:");
  scanf("%d %d %d",&a,&b,&c);

  sum = ((a*a)+(b*b));
  e = a%b;
  f = c%b;
  y = e%f;

  x = sum/(c*c);

  printf("Value is to check a<b<c: %d\n",x);
  printf("Value is to check it is Pythogorean Triplet: %d\n",y);

  //printf("Value is: %d\n",(36/34));
  return 0;
}

Code was working fine but gave wrong outputs when I tested with cases like following:
4 3 5
4 5 3

Recommended Answers

All 4 Replies

To determine if a<b<c why not just use the normal simple if statement

if( (a < b) && (b < c) )
{
   // true
}

then to determine if they are Pythagorean Triple:

if( (a*a) + (b*b) == (c*c) )
{
   // true
}

But I cannot use if,else or even the less than or greater than.

In lines 30-32 I think your trying to find out if a<b<c. if( a%b > 0 && b%c > 0) then a<b<c. So line 31 is backwards, should be b%c not c%b.

line 34: That is doing integer division which means no fractions. 3/4 == 0 not 0.75. Convert either the numerator or denominator to float

x = (float)sum/(c*c);

And x should be declared as float, not int.

First of all, your output is reversed. a<b<c check should be y, while pythagorean check should be x.

Anyway, if the output is supposed to be 0 or 1 only, then your code is still lacking. Assuming input of positive and nonequal three integers, using e=a%b, if a is greater than b, then the result of a%b would always be less than a. On the other hand, if b is greater than a, then the result of a%b would always be a. What can we do then, to make the output 0 and 1 respectively?

Follow that with f=b%c, if a is greater than b, or if b is greater than c, then one or both of e and f is 0. If one of them is 0, how can we make sure that y would be 0?

As for checking for pythagorean sets, you can do it with integers as well. If sum is less than c2 (c-squared, for simplicity), sum/c2 is 0. What if sum is greater than c2? Well, the reciprocal, c2/sum gives 0. If sum=c2, both give 1. Get the idea?

Lastly, what if a<b<c is false, while pythagorean check is true? Well, the second condition depends on the first condition anyway. So make use of y (which is equal to 0 in this case) to do something about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.