me and friend wrote this small prog but it is still not good enugh (it
is really long)
could you plz take a look and give me some editions ?
The prog need to take a number then test :
find the largest digit.
is the number simatric
show the reverse number
it is released under FAL licence (F*@! all licences)
all right belong to Debian .. :-)
------------------------------------------------------------------------
#include
#include
char get_max_digit(int num);
int get_summ_digits(int num);
char is_symmetric(int num);
int reverse_num(int num);
void menu()
{
printf("\n1. Summ of digits.\n");
printf("2. Max digit.\n");
printf("3. Reverse number.\n");
printf("4. Is symmetric.\n");
printf("5. New number.\n");
printf("6. Exit.\n");
}
int main(int argc, char **argv)
{
char w = 1;
int num = 0;
while (w)
{
menu();
while(isspace(w = getchar()));
switch(w)
{
case '1':
printf("Summ of digits is: %i\n", get_summ_digits(num));
break;
case '2':
printf("Max. digit is: %i\n", get_max_digit(num));
break;
case '3':
printf("Reversed number is: %d\n", reverse_num(num));
break;
case '4':
printf("Is symmetric: %d\n", is_symmetric(num));
break;
case '5':
printf("Enter new number: ");
scanf("%i", &num);
break;
case '6':
w = 0;
break;
}
}
return 0;
}
char get_max_digit(int num)
{
char res = num % 10;
char dig;
while (num /= 10)
{
dig = num % 10;
if (dig > res)
res = dig;
}
return res;
}
int get_summ_digits(int num)
{
int res = num % 10;
while (num /= 10)
res += num % 10;
return res;
}
int reverse_num(int num)
{
int res = 0;
int multiplier = 10;
while (num / multiplier)
multiplier *= 10;
multiplier /= 10;
while(num / 10)
{
res += num % 10 * multiplier;
multiplier /= 10;
num /= 10;
}
res += num;
return res;
}
char is_symmetric(int num)
{
char dig1, dig2, res = 0;
int multiplier1, multiplier2;
multiplier1 = 1;
multiplier2 = 10;
while (num / multiplier2)
multiplier2 *= 10;
multiplier2 /= 10;
while(
(((num / multiplier1) % 10) == ((num / multiplier2) % 10)) &&
(multiplier1 <= multiplier2)
)
{
multiplier1 *= 10;
multiplier2 /= 10;
}
return (multiplier1 <= multiplier2) ? 0 : 1;
}
--
Bookmark/Search this post with:
How to make a prog more efieiant (in C)
Is this from a recent job interview, with whiteboard programming questions?
Anyway, get_max_digit() and get_sum_digits() seem perfectly reasonable. For reverse_num() and is_symmetric(), about the only other approach I can think of is converting the numbers to strings first, do your manipulations, then convert back to integers. It would be fewer lines of code probably, but not necessarily more efficient.
-- Kevin
How to make a prog more efieiant (in C)
Sorry if i mislead some one im just trieng to make my progy work better. (It's an HW assignment)
btw do you mean :res_3
simatric ?
char res_2,res_3;
res_2=(char)num;
res_3=res_2;
then
loop to rearnge the res_2
and (res_2==res_3)?0:1; ?
On 12/1/06, Kevin Ross <kross@statuspro.tv> wrote: