Need help modifying the below program so that the user can enter more than one n
ID: 3621912 • Letter: N
Question
Need help modifying the below program so that the user can enter more than one number to be tested for repeated digits. The program should terminate when the user enters a number that's less than or equal to 0./* Checks number for repeated digtis*/
#include <stdbool.h> /*C99 only*/
#include <stdio.h>
int main (void)
{
bool digit_seen [10]
int digit;
long n;
printf ("Enter a number: ");
scanf ("%1d", &n);
while (n > 0) {
digit = % 10;
if (digit_seen [digit])
break;
digit_seen [digit] = true;
n / = 10;
}
if (n > 0)
printf ("Repeated digit ");
else
printf ("No repeated digit ");
return 0;
}
Need help modifying this in C-Language (can't be C# or C++)
Explanation / Answer
please rate - thanks
using a boolean would make this much more complicated, since a number that repeats >2 times you only want to appear once.
/* Checks number for repeated digtis*/
#include <stdbool.h> /*C99 only*/
#include <stdio.h>
#include <conio.h>
int main (void)
{
int digit_seen [10];
int digit;
long n;
printf ("Enter a number(<=0 to terminate): ");
scanf ("%ld", &n);
while(n>0)
{for(digit=0;digit<10;digit++)
digit_seen[digit]=0;
while (n > 0)
{
digit = n% 10;
digit_seen[digit]++;
n /= 10;
}
printf("repeated digit(s): ");
for(digit=0;digit<10;digit++)
if(digit_seen[digit]>1)
printf ("%d ",digit);
printf (" Enter a number(<=0 to terminate): ");
scanf ("%ld", &n);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.