The main task is to generate 3 rectangles using two points (top left and bottom right of each) and determinate the largest area among them.
Partially, I have already completed the task using this code:
#include <stdio.h>
#include <time.h>
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point botRight;
};
int Area (struct Rectangle r) {
int length, breadth;
length = r.botRight.x - r.topLeft.x;
breadth = r.topLeft.y - r.botRight.y;
return length * breadth;
}
int main() {
srand(time(NULL));
struct Rectangle r1, r2, r3;
r1.topLeft.x = -50 + rand() % 101;
r1.topLeft.y = -50 + rand() % 101;
r1.botRight.x = -50 + rand() % 101;
r1.botRight.y = -50 + rand() % 101;
while (r1.botRight.x <= r1.topLeft.x) {
r1.botRight.x = -50 + rand() % 101;
}
while (r1.topLeft.y <= r1.botRight.y) {
r1.topLeft.y = -50 + rand() % 101;
}
printf("\t----------RECTANGLE 1----------\n");
printf("\tTop left point is x = %d y = %d\n", r1.topLeft.x, r1.topLeft.y);
printf("\tBottom right point is x = %d y = %d\n", r1.botRight.x, r1.botRight.y);
printf("\tArea is %d\n", Area(r1));
r2.topLeft.x = -50 + rand() % 101;
r2.topLeft.y = -50 + rand() % 101;
r2.botRight.x = -50 + rand() % 101;
r2.botRight.y = -50 + rand() % 101;
while (r2.botRight.x <= r2.topLeft.x) {
r2.botRight.x = -50 + rand() % 101;
}
while (r2.topLeft.y <= r2.botRight.y) {
r2.topLeft.y = -50 + rand() % 101;
}
printf("\t----------RECTANGLE 2----------\n");
printf("\tTop left point is x = %d y = %d\n", r2.topLeft.x, r2.topLeft.y);
printf("\tBottom right point is x = %d y = %d\n", r2.botRight.x, r2.botRight.y);
printf("\tArea is %d\n", Area(r2));
r3.topLeft.x = -50 + rand() % 101;
r3.topLeft.y = -50 + rand() % 101;
r3.botRight.x = -50 + rand() % 101;
r3.botRight.y = -50 + rand() % 101;
while (r3.botRight.x <= r3.topLeft.x) {
r3.botRight.x = -50 + rand() % 101;
}
while (r3.topLeft.y <= r3.botRight.y) {
r3.topLeft.y = -50 + rand() % 101;
}
printf("\t----------RECTANGLE 3----------\n");
printf("\tTop left point is x = %d y = %d\n", r3.topLeft.x, r3.topLeft.y);
printf("\tBottom right point is x = %d y = %d\n", r3.botRight.x, r3.botRight.y);
printf("\tArea is %d\n", Area(r3));
printf("\t-------------------------------\n");
if (Area(r1) >= Area(r2) && Area(r1) >= Area(r3))
printf("\tRectangle 1 has a biggest area --> %d\n", Area(r1));
if (Area(r2) >= Area(r1) && Area(r2) >= Area(r3))
printf("\tRectangle 2 has a biggest area --> %d\n", Area(r2));
if (Area(r3) >= Area(r1) && Area(r3) >= Area(r2))
printf("\tRectangle 3 has a biggest area --> %d\n", Area(r3));
But I'm very confused about how to do this task if there are, for example, 50 rectangles?)
There is what i've already done:
I've created the function of generatig the points of each rectangle (genRec) in order to generate 50 rectangles.
#include <stdio.h>
#include <time.h>
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point botRight;
};
int Area(struct Rectangle r) {
int length, breadth;
length = r.botRight.x - r.topLeft.x;
breadth = r.topLeft.y - r.botRight.y;
return length * breadth;
}
int genRec();
int main() {
srand(time(NULL));
for (int i = 1; i < 51; i++) {
printf("\t----------RECTANGLE %d----------\n" , i);
genRec();
}
}
int genRec() {
struct Rectangle rec;
rec.topLeft.x = -50 + rand() % 101;
rec.topLeft.y = -50 + rand() % 101;
rec.botRight.x = -50 + rand() % 101;
rec.botRight.y = -50 + rand() % 101;
while (rec.botRight.x <= rec.topLeft.x) {
rec.botRight.x = -50 + rand() % 101;
}
while (rec.topLeft.y <= rec.botRight.y) {
rec.topLeft.y = -50 + rand() % 101;
}
printf("\tTop left point is x = %d y = %d\n", rec.topLeft.x, rec.topLeft.y);
printf("\tBottom right point is x = %d y = %d\n", rec.botRight.x, rec.botRight.y);
printf("\tArea is %d\n", Area(rec));
return 0;
}
But, i have no idea how to compare the areas of each rectangle easily, without eternal comparing using "if" because Area(rec) is an only one variable, but with differnt numbers.
I will be glad for any help, don't judge harshly, I'm just a begginer)
Your issue is that you can't just:
Generate the "lower" coordinate (in a given dimension, either X or Y).
then loop on the "upper" coordinate (in the given dimension) until it is larger.
If the "lower" is ever the maximum, the program will loop infinitely because the "upper" can never be greater than the "lower".
One solution is to generate a pair of numbers (e.g. lo and hi) and loop if lo >= hi. The key difference is the loop will regenerate the lo value on each iteration.
In other words, if the range is bad, reject both lower and upper numbers and try again.
A helper function to generate a pair can help.
Below is the refactored code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point botRight;
};
int
Area(const struct Rectangle *r)
{
int length, breadth;
length = r->botRight.x - r->topLeft.x;
if (length <= 0)
exit(1);
breadth = r->topLeft.y - r->botRight.y;
if (breadth <= 0)
exit(2);
return length * breadth;
}
int genRec(void);
int
main(void)
{
srand(time(NULL));
for (int i = 1; i < 51; i++) {
printf("\t----------RECTANGLE %d----------\n", i);
genRec();
}
return 0;
}
void
genPair(int *lo,int *hi)
{
while (1) {
*lo = -50 + rand() % 101;
*hi = -50 + rand() % 101;
if (*lo < *hi)
break;
}
}
int
genRec(void)
{
struct Rectangle rec;
genPair(&rec.topLeft.x,&rec.botRight.x);
genPair(&rec.botRight.y,&rec.topLeft.y);
printf("\tTop left point is x = %d y = %d\n",
rec.topLeft.x, rec.topLeft.y);
printf("\tBottom right point is x = %d y = %d\n",
rec.botRight.x, rec.botRight.y);
printf("\tArea is %d\n", Area(&rec));
return 0;
}
As to determining the maximum area of all the rectangles, you calculate the area with Area but just print it.
We can return this value to main. It can retain/remember the maximum area and its corresponding rectangle index.
One issue with the original code in this regard is that genRec uses a function scoped variable rec. We want to have this be a pointer argument so main can populate an array.
Here is the refactored code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Point {
int x;
int y;
};
struct Rectangle {
struct Point topLeft;
struct Point botRight;
int area;
};
int
Area(const struct Rectangle *r)
{
int length, breadth;
length = r->botRight.x - r->topLeft.x;
if (length <= 0)
exit(1);
breadth = r->topLeft.y - r->botRight.y;
if (breadth <= 0)
exit(2);
return length * breadth;
}
int genRec(struct Rectangle *rec);
int
main(void)
{
srand(time(NULL));
int maxarea = -1;
int maxidx = -1;
struct Rectangle rec[50];
for (int i = 0; i < sizeof(rec) / sizeof(rec[0]); i++) {
printf("\n");
printf("\t----------RECTANGLE %d----------\n", i + 1);
int area = genRec(&rec[i]);
if (area > maxarea) {
maxarea = area;
maxidx = i;
}
}
printf("\n");
printf("\tMaximum area is %d in rectangle %d\n",maxarea,maxidx + 1);
return 0;
}
void
genPair(int *lo,int *hi)
{
while (1) {
*lo = -50 + rand() % 101;
*hi = -50 + rand() % 101;
if (*lo < *hi)
break;
}
}
int
genRec(struct Rectangle *rec)
{
genPair(&rec->topLeft.x,&rec->botRight.x);
genPair(&rec->botRight.y,&rec->topLeft.y);
printf("\tTop left point is x = %d y = %d\n",
rec->topLeft.x, rec->topLeft.y);
printf("\tBottom right point is x = %d y = %d\n",
rec->botRight.x, rec->botRight.y);
int area = Area(rec);
printf("\tArea is %d\n", area);
return area;
}
Related
I am beginner I tried so many times but I couldn't solve this problem I will be very pleased if you help me...
the question is:
Let x be an integer, and R(x) is a function that returns the reverse of the x in terms of its digits.
For example , if x:1234 then R(x)=4321.
Letβs call a positive integer mirror-friendly if it satisfies the following condition: π₯ + π
(π₯) = π¦^2 π€βπππ π¦ ππ ππ πππ‘ππππ
Write a program that reads a positive integer as n from the user and prints out a line for each of the first n mirror-friendly integers as follows: x + R(x) = y^2
Example: If the user enters 5 as n, then the program should print out the following:
2 + 2 = 2^2
8 + 8 = 4^2
29 + 92 = 11^2
38 + 83 = 11^2
47 + 74 = 11^2
Here is the my code:
int
reverse(int num)
{
int reverse,
f,
i;
reverse = 0;
i = 0;
for (; i < num + i; i++) {
f = num % 10;
reverse = (reverse * 10) + f;
num /= 10;
}
return reverse;
}
int
sqrt(int n)
{
int i = 1;
int sqrt;
for (; i <= n; i++) {
sqrt = i * i;
}
return sqrt;
}
int
main()
{
int j = 1;
int main_num = 0;
for (; main_num <= 0;) {
printf("Please enter a positive integer: \n");
scanf_s("%d", &main_num);
}
int count = 0;
for (int i = 1; i <= main_num; i++) {
for (; j <= main_num; j++) {
if (j + reverse(j) == sqrt(?)) {
printf("%d + %d = %d\n", j, reverse(j), sqrt(?));
}
}
}
}
A few issues ...
sqrt does not compute the square root
reverse seems overly complicated
main_num (i.e. n from the problem statement) is the desired maximum count of matches and not the limit on x
Too many repeated calls to sqrt and reverse
No argument given to sqrt
The if in main to detect a match is incorrect.
sqrt conflicts with a standard function.
The variables you're using don't match the names used in the problem statement.
The printf didn't follow the expected output format.
Using a function scoped variable that is the same as the function is a bit confusing (to humans and the compiler).
Unfortunately, I've had to heavily refactor the code. I've changed all the variable names to match the names used in the problem statement for clarity:
#include <stdio.h>
#include <stdlib.h>
#ifdef DEBUG
#define dbgprt(_fmt...) printf(_fmt)
#else
#define dbgprt(_fmt...) do { } while (0)
#endif
int
reverse(int x)
{
int r = 0;
for (; x != 0; x /= 10) {
int f = x % 10;
r = (r * 10) + f;
}
return r;
}
int
isqrt(int x)
{
int y = 1;
while (1) {
int y2 = y * y;
if (y2 >= x)
break;
++y;
}
return y;
}
int
main(int argc,char **argv)
{
int n = -1;
--argc;
++argv;
if (argc > 0) {
n = atoi(*argv);
printf("Positive integer is %d\n",n);
}
while (n <= 0) {
printf("Please enter a positive integer:\n");
scanf("%d", &n);
}
int x = 1234;
dbgprt("x=%d r=%d\n",x,reverse(x));
int count = 0;
for (x = 1; count < n; ++x) {
dbgprt("\nx=%d count=%d\n",x,count);
// get reverse of number (i.e. R(x))
int r = reverse(x);
dbgprt("r=%d\n",r);
// get x + R(x)
int xr = x + r;
dbgprt("xr=%d\n",xr);
// get y
int y = isqrt(xr);
dbgprt("y=%d\n",y);
if (xr == (y * y)) {
printf("%d + %d = %d^2\n", x, r, y);
++count;
}
}
return 0;
}
Here is the program output:
Positive integer is 5
2 + 2 = 2^2
8 + 8 = 4^2
29 + 92 = 11^2
38 + 83 = 11^2
47 + 74 = 11^2
UPDATE:
The above isqrt uses a linear search. So, it's a bit slow.
Here is a version that uses a binary search:
// isqrt -- get sqrt (binary search)
int
isqrt(int x)
{
int ylo = 1;
int yhi = x;
int ymid = 0;
// binary search
while (ylo <= yhi) {
ymid = (ylo + yhi) / 2;
int y2 = ymid * ymid;
// exact match (i.e. x == y^2)
if (y2 == x)
break;
if (y2 > x)
yhi = ymid - 1;
else
ylo = ymid + 1;
}
return ymid;
}
UPDATE #2:
The above code doesn't scale too well for very large x values (i.e. large n values).
So, main should check for wraparound to a negative number for x.
And, a possibly safer equation for isqrt is:
ymid = ylo + ((yhi - ylo) / 2);
Here is an updated version:
#include <stdio.h>
#include <stdlib.h>
#ifdef DEBUG
#define dbgprt(_fmt...) printf(_fmt)
#else
#define dbgprt(_fmt...) do { } while (0)
#endif
// reverse -- reverse a number (e.g. 1234 --> 4321)
int
reverse(int x)
{
int r = 0;
for (; x != 0; x /= 10) {
int f = x % 10;
r = (r * 10) + f;
}
return r;
}
// isqrt -- get sqrt (linear search)
int
isqrt(int x)
{
int y = 1;
while (1) {
int y2 = y * y;
if (y2 >= x)
break;
++y;
}
return y;
}
// isqrt2 -- get sqrt (binary search)
int
isqrt2(int x)
{
int ylo = 1;
int yhi = x;
int ymid = 0;
// binary search
while (ylo <= yhi) {
#if 0
ymid = (ylo + yhi) / 2;
#else
ymid = ylo + ((yhi - ylo) / 2);
#endif
int y2 = ymid * ymid;
// exact match (i.e. x == y^2)
if (y2 == x)
break;
if (y2 > x)
yhi = ymid - 1;
else
ylo = ymid + 1;
}
return ymid;
}
int
main(int argc,char **argv)
{
int n = -1;
--argc;
++argv;
setlinebuf(stdout);
// take number from command line
if (argc > 0) {
n = atoi(*argv);
printf("Positive integer is %d\n",n);
}
// prompt user for expected/maximum count
while (n <= 0) {
printf("Please enter a positive integer:\n");
scanf("%d", &n);
}
int x = 1234;
dbgprt("x=%d r=%d\n",x,reverse(x));
int count = 0;
for (x = 1; (x > 0) && (count < n); ++x) {
dbgprt("\nx=%d count=%d\n",x,count);
// get reverse of number (i.e. R(x))
int r = reverse(x);
dbgprt("r=%d\n",r);
// get x + R(x)
int xr = x + r;
dbgprt("xr=%d\n",xr);
// get y
#ifdef ISQRTSLOW
int y = isqrt(xr);
#else
int y = isqrt2(xr);
#endif
dbgprt("y=%d\n",y);
if (xr == (y * y)) {
printf("%d + %d = %d^2\n", x, r, y);
++count;
}
}
return 0;
}
In the above code, I've used cpp conditionals to denote old vs. new code:
#if 0
// old code
#else
// new code
#endif
#if 1
// new code
#endif
Note: this can be cleaned up by running the file through unifdef -k
for(;i<num+i;i++)
is equal to
for(; 0<num;i++)
or for(; num;i++) if we are working with positive values only.
or even to while(num)
So, we don't need variable i in reverse function.
We don't need cycle at all in sqrt function. Just return n * n; is ok. But it is not sqrt then
The last cycle is too strange. At least variable j is not initialized.
145 = sum of 1! + 4! + 5!. I need to write a program in C, that finds the 5 digit numbers that have this property.
I have written the code successfully for the 3 digits. I used the same code for 5 digits, but it cant find any number.
I would like to help me with my solution, in order for me to see where am I wrong.
#include <stdio.h>
int factorial(int n);
main() {
int pin[5];
int q = 1;
int w = 0;
int e = 0;
int r = 0;
int t = 0;
int result = 0;
int sum = 0;
for (q = 1; q <= 9; q++) {
for (w = 0; w <= 9; w++) {
for (e = 0; e <= 9; e++) {
for (r = 0; r <= 9; r++) {
for (t = 0; t <= 9; t++) {
pin[0] = q;
pin[1] = w;
pin[2] = e;
pin[3] = r;
pin[4] = t;
int factq = factorial(q);
int factw = factorial(w);
int facte = factorial(e);
int factr = factorial(r);
int factt = factorial(t);
sum = factq + factw + facte + factr + factt;
result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;
if (sum == result)
printf("ok");
}
}
}
}
}
}
int factorial(int n) {
int y;
if (n == 1) {
y = 1;
} else if (n == 0)
y = 0;
else {
y = n * factorial(n - 1);
return y;
}
}
Your factorial function doesn't return a value in all cases:
int factorial (int n) {
int y;
if (n==1) {
y = 1;
}
else
if (n==0)
y = 0;
else {
y = n * factorial(n-1);
return y;
}
}
It only returns a value when it makes a recursive call. The base cases don't return anything. Failing to return a value from a function and then attempting to use that value invokes undefined behavior.
Move the return statement to the bottom of the function so it gets called in all cases. Also the value of 0! is 1, not 0.
int factorial (int n) {
int y;
if (n<=1)
y = 1;
else
y = n * factorial(n-1);
return y;
}
Also, when you find the target value you probably want to print it:
printf("ok: %d\n", result);
dbush's answer is accurate in pointing out why your code didn't work. This is an alternative solution to reduce the amount of calculation done by your program by not re-calculating the factorial of each numeral every step of the way. The way your program currently works, it winds up being around 500,000 calls to the factorial function from your nested loop, and then in turn recursively calls the function on average 4ish times for each call from the nested loop, so that's around 2 million calls to factorial. The more digits you tack on, the faster that number grows and more expensive it gets. To avoid all these recalculations, you can create a Look-up table that stores the factorial of the numerals [0-9] and just looks them up as needed.
You can calculate these values ahead of time and initialize your LUT with these values, but if hypothetically you wanted them to be calculated by the program because this is a programming assignment where you can't cut out such a step, it is still pretty trivial to populate the LUT.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
void populate_lut(uint32_t *lut);
int main(void) {
// lut is an array holding the factorials of numerals 0-9
uint32_t lut[10];
populate_lut(lut);
for (uint8_t q = 1; q <= 9; q++) {
for (uint8_t w = 0; w <= 9; w++) {
for (uint8_t e = 0; e <= 9; e++) {
for (uint8_t r = 0; r <= 9; r++) {
for (uint8_t t = 0; t <= 9; t++) {
// now instead of calculating these factorials, just look them up in the look-up table
uint32_t sum = lut[q] + lut[w] + lut[e] + lut[r] + lut[t];
uint32_t result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;
if (sum == result) {
printf("Solution: %" PRIu32 "\n", result);
}
}
}
}
}
}
}
// populate your lookup table with the factorials of digits 0-9
void populate_lut(uint32_t *lut) {
lut[0] = 1;
lut[1] = 1;
for(uint8_t i = 2; i < 10; ++i) {
lut[i] = lut[i-1] * i;
}
}
So, I tried to implement the Middle Square PRNG method, to generate the first 100 numbers. It works well until a certain point, when I get as a result negative numbers.
I used the time library to change the values on my temp array, so that it won't get stuck on the same sequence, where the number ends with two zeros.
My code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
time_t now;
struct tm *tm;
unsigned long int prng(int seed)
{
int num = seed * seed;
int t[10], inc = 0;
//Reverse number in an array
while(num != 0)
{
t[inc] = num%10;
num /= 10;
inc++;
}
int min = inc/4;
int max = inc / 2 + min;
int temp[10];
//Assign the middle square part to another table
for(int i = min; i <= max; i++)
{
temp[i-min] = t[i];
}
for(int i=0; i < max-min; i++)
{
//Check if there is a "0" "0" sequence, if TRUE - replace with current time (seconds)
if(temp[i] == 0 && temp[i+1] == 0)
{
now = time(0);
tm = localtime(&now);
temp[i] = tm->tm_sec/10;
temp[i + 1] = tm->tm_sec%10;
}
}
//Transform the squared array into an integer
unsigned long int k = 0;
for (int i = 0; i <= max-min; i++)
k = 10 * k + temp[i];
return k;
}
int main()
{
unsigned long int n = 123; //seed
printf("%d ", n);
for(int i = 0; i<100; i++)
{
n = prng(n);
printf("\n%d ", n);
}
return 0;
}
The results that I get:
123
215
226
701
419
6557
24992
7064
7099
85930
-696950
8997
6490
10212
94824
36561
760763
-724206
30238
66334
22325
65048
-94273
...
I am trying to solve this tutorial practice question that doesn't have an answer that I can check my code against. The goal is to write a program to display numbers whose digits are 2 greater than the corresponding digits of the entered number. So if the number input is 5656 then the output number should be 7878. I have figured out how to separate each number and add them, but I can't seem to get them to print in a four-digit sequence.
#include <stdio.h>
int main ()
{
int n, one, two, three, four, final;
scanf("%d", &n);
one = (n / 1000);
n = (n % 1000) + 2;
two = (n / 100) + 2;
n = (n % 100) + 2;
three = (n / 10) + 2;
n = (n % 10) + 2;
four = (n / 1) + 2;
n = (n % 1) + 2;
final = (one * 1000) + (two * 100) + (three * 10) + four;
printf("%d", final);
return 0;
}
#include <stdio.h>
int main()
{
int n,a[4], final;
scanf("%d", &n);
for(int i=3;i>=0;i--)
{
a[i]=n%10+2;
n/=10;
}
final = (a[0] * 1000) + (a[1] * 100) + (a[2] * 10) + a[3];
printf("%d", final);
return 0;
}
Below function works with N number of digits.
Idea is to extract each digit from the input number and add its decimal position.
#include <stdio.h>
int power(int x, int y)
{
int res = 1;
for (;y>0;y--)
{
res *=x;
}
return res;
}
int main ()
{
int n;
scanf("%d", &n);
int sum = 0;
int i=0;
while(n>0)
{
sum += ((n%10) +2)*power(10,i);
i++;
n /=10;
}
printf("%d", sum);
return 0;
}
Another idea:
char str[10]; // enough to contain an int as string + 1
char *s = str+sizeof(str); // points to last char + 1
int n;
scanf("%d", &n);
*--s = 0; // terminate the string
while(n) {
*--s = (((n % 10)+2)%10) + '0'; // write a char from the end
n /= 10;
}
printf("%s\n", s);
int a = 5696;
// output is 7818
// ---------Java-----------
// --------solution--------
int first = a/1000+2;
int b = a%1000;
int second = b/100+2;
int c = b%100;
int d = c/10+2;
int third = d/10;
int e = c%10;
int fourth = e+2;
String result = Integer.toString(first)+Integer.toString(second)+Integer.toString(third)+Integer.toString(fourth);
System.out.println(result);
I want to split the a given number (6 digits) after incrementing it by 1 into 2 numbers (3 digits for each one) then sum first 3 digits and last 3 digits and check if sum matches, do the process again, finally return the number that has the first and last 3 digits sum is equal.
My code splits the number , but the last 3 digits are printed reversed somehow (that's not the matter as I want the sum of them only)
but the problem comes when I try to sum every 3 digits.
int onceInATram(int x) {
// Complete this function
int n = 0;
int y = 0;
int len = 0;
int digit = 0;
int t1 = n;
int t2 = y;
int reminder1 = 0;
int reminder2 = 0;
int sum1 = 0;
int sum2 = 0;
len = (int) floor(log10(abs(x))) + 1;
do {
n = x + 1; // to add 1 to the number
while ((floor(log10(abs(n)) + 1) > len / 2)) { // split it by half
digit = n % 10;
n = n / 10;
y = (y * 10) + digit;
}
int l = 3;
while (l--) {
reminder1 = t1 % 10;
sum1 = sum1 + reminder1;
t1 = t1 / 10;
reminder2 = t2 % 10;
sum2 = sum2 + reminder2;
t2 = t2 / 10;
}
} while (sum1 != sum2);
//return(printf("%d\n%d\n", n, y)); // for debugging
return printf("%d%d\n", n, y); // '' ''
//return printf("%d\n", sum1); // '' ''
}
int main() {
int x;
scanf("%i", &x);
int result_size;
char* result = (char *) onceInATram(x);
printf("%s\n", result);
return 0;
}
and I used function but seems that nothing work!
my input:
555555
my output: 555655 > same as 555556 (incrementing by 1 but reverse last 3 digits).
expected output: 555564 (as the sum of first 3 digits == last 3 digits).
I re-wrote it to try to be simpler and more straightforward.
I came up with this:
IDEOne Link:
#include <float.h>
#include <math.h>
#include <stdlib.h>
int onceInATram(int n) {
int y = 0;
int x = 0;
int t1 = n;
int t2 = y;
int reminder1 = 0;
int reminder2 = 0;
int sum1 = 0;
int sum2 = 0;
do {
n = n + 1; // to add 1 to the number
y = n % 1000; // This is the first 3 numbers
x = n / 1000; // This is the last 3 numbers
printf("%d is now split into %d and %d\n", n, x, y);
t1 = x;
t2 = y;
sum1 = 0;
sum2 = 0;
for(int l=0; l<3; ++l) {
reminder1 = t1 % 10;
sum1 = sum1 + reminder1;
t1 = t1 / 10;
reminder2 = t2 % 10;
sum2 = sum2 + reminder2;
t2 = t2 / 10;
}
} while (sum1 != sum2);
return 1000*x+y;
}
int main() {
int x;
scanf("%d", &x);
int result = onceInATram(x);
printf("The Final Answer is %d\n", result);
return 0;
}
Example Input / Output:
123456
The Final Answer is 123501
because 1 + 2 + 3 == 6 == 5 + 0 + 1