Anyway, there is a problem I was hitting on since couple of days and til now there is no answer.
let me show it to you:
/* Checks number for repeated digits */
#include<stdio.h>
#define TRUE 1;
#define FALSE 0;
typedef int Bool;
main()
{
Bool digit_seen[10]={0};
int digit;
long int n;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0)
{
digit=n%10;
if (digit_seen[digit])
break;
digit_seen[digit] = TRUE;
n /= 10;
}
if (n > 0)
printf("Repeated digit\n\n");
else
printf("No repeated digit\n\n");
return 0;
}
#define TRUE 1;
#define FALSE 0;
typedef int Bool;
main()
{
Bool digit_seen[10]={0};
int digit;
long int n;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0)
{
digit=n%10;
if (digit_seen[digit])
break;
digit_seen[digit] = TRUE;
n /= 10;
}
if (n > 0)
printf("Repeated digit\n\n");
else
printf("No repeated digit\n\n");
return 0;
}
To be honest I spent a long period to understand that code. I didn't start solving the problem until I got the idea about it =D
anyway, I want to share the question with you guys, I think it's fun for all of us to think and try to come with a solution or an idea about how the answer code will be.
Here's the exercise:
Modify the repdigit.c program (The above program) so that it shows which digits (if any) were repeated:
Enter a number: 93977
Repeated digit(s): 7 9
didn't figure it out yet but I'm still trying. Here's what I went through so far:
#include<stdio.h>
#define TRUE 1;
#define FALSE 0;
typedef int Bool;
main()
{
Bool digit_seen[10]={0};
Bool repeated_digit[10];
int digit;
long int n;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0)
{
digit=n%10;
if (digit_seen[digit])
{repeated_digit[digit]=digit;
}
digit_seen[digit] = TRUE;
n /= 10;
}
if (repeated_digit[digit])
{printf("Repeated digit\n\n");
printf("%d", repeated_digit[digit]);}
else
{printf("No repeated digit\n\n");
printf("%d", n);}
return 0;
}
it's a wrong answer and I've been changing it from the beginning to now =D
enjoy it =)
peace~
No comments:
Post a Comment