You are to write a C program that inputs a value N and outpu

You are to write a C program that inputs a value, N, and outputs a list of all numbers less than N with a property similar to that of 1729. Namely, each number in the list should be expressible as the sum of two cubes in two different ways. Your program should output the two different ways. For example, if N

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

#include <stdio.h>

int main()
{
int N;
printf(\"Enter N: \");
scanf(\"%d\",&N); //read number N

int a=0,b=0,c=0,d=0; //initialize 4 variables to 0, to store the numbers of which cubic sums = N

for(int i=1; i<N; i++){ //outer loop

for(int j=i; j<N; j++){ //inner loop starts from i, in order to avoid inverse repetation

if( (i*i*i) + (j*j*j) == N){ //checking whether the cubic sums of i & j are = N
  
if(a!=0 && b!=0){ //if a & b are already populated with values, then we have 2 pairs to print one line of output
c = i; //save the third and fourth numbers to c and d
d = j;
  
printf(\"%d = %d^3 + %d^3 = %d^3 + %d^3\ \",N,a,b,c,d); //print output
  
a=0; b=0; c=0; d=0; //if one line is printed, then resets all 4 variables to 0, to start again the process to print next pairs

}

if(a==0 && b==0){ //if a & b are 0, then this i & j are the first pairs we got, so save it to a & b
a = i;
b = j;
}
  
  
}
}
}
  
  
return 0;
}

----------------------------------

OUTPUT:

Enter N: 1729                                                                                                                                                                                                 

1729 = 1^3 + 12^3 = 9^3 + 10^3

Enter N: 4104                                                                                                                                                                                                 

4104 = 2^3 + 16^3 = 9^3 + 15^3

 You are to write a C program that inputs a value, N, and outputs a list of all numbers less than N with a property similar to that of 1729. Namely, each number

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site