# Introduction

Discussion in 'Meet and Greet' started by lp1938, Jan 21, 2011.

1. ### lp1938New Member

Joined:
Jan 21, 2011
Messages:
5
0
Trophy Points:
0
Occupation:
Retired
Location:
Chiang Mai Thailand and Melbourne Australia
Hi folks. I'm a retired guy who loves to program in C. Yes I've done a bit of C++ especially the Borland C++ Builder a few years ago. But C just seems to do it better. I'm starting a few projects that I'm finding a bit challenging so I'll ask about the specifics when they arise.

Bob

2. ### NewsBotNew Member

Joined:
Dec 2, 2008
Messages:
1,267
2
Trophy Points:
0
Hi Bob and welcome to the forum

3. ### Rakesh Kumar PahwaNew Member

Joined:
Jan 13, 2011
Messages:
23
0
Trophy Points:
0
Hi Bob... welcome

4. ### lkm1103New Member

Joined:
Mar 7, 2011
Messages:
4
0
Trophy Points:
0
Occupation:
student
Location:
in south africa
Hi Bob, i'm Lonel ,i was wandering if you can help me to solve this problem of mine that i have wich is:

I need to write a program that accepts a single integer as a command-line argument, the input must be validate and then the process described below must be perform until it converges.
The process is as follows: Take any 3-digit number and arrange its digits in descending order and in ascending order and subtract the larger number from the smaller number. Repeat with the result until the resulting number stops changing (converges).

And I would like to know how many iterations it took to converge and what the converged value is

i'll geatly appreciat your assistance because i really wanna get this !!!

5. ### lp1938New Member

Joined:
Jan 21, 2011
Messages:
5
0
Trophy Points:
0
Occupation:
Retired
Location:
Chiang Mai Thailand and Melbourne Australia
Code:
```/* 3digits.c - Take any 3-digit number and arrange its digits in
* descending order and in ascending order and subtract the larger
* number from the smaller number. Repeat with the result until the
* resulting number stops changing (converges).
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *emesg="There must be one 3 digit argument provided\n";

int main(int argc, char **argv) {
char fwd[4], rev[4];
int i, count, sum, oldsum, nfwd, nrev;
// validate input, must have 1 arg and it must be 3 long
if (argc != 2 || strlen(argv[1]) != 3) {
fputs(emesg, stderr);
return 1;
} // if()
for(i = 0; i < 3; i++) {
fwd[i] = argv[1][i];
rev[2 - i] = argv[1][i];
} // for(i..
fwd[3] = rev[3] = '\0';
printf("%s %s\n", fwd, rev);
count = 0;
nfwd = atoi(fwd);
nrev = atoi(rev);
oldsum = nfwd;
sum = nrev;
while (oldsum != sum) {
oldsum = sum;
if (nfwd > nrev)
sum = nfwd - nrev;
else
sum = nrev - nfwd;
printf("%d %d %d\n", nfwd, nrev, sum);
sprintf(fwd, "%d", sum);
for (i = 0; i < 3; i++) {
rev[2 - i] = fwd[i];
} // for(i..
nfwd = atoi(fwd);
nrev = atoi(rev);
count++;
} // while()
printf("Iterations: %d\n", count);
return 0;
} // main()
```
I've left some print statements in.

Joined:
Mar 7, 2011
Messages:
4