Using arrays in c to find repeated digits in an number (C) - c

I need to make a program that checks to see if an entered value has any repeated digits. The user is asked to enter numbers until the entered value is 0. If there are any repeated digits, it displays "repeated digits" and then asks the user to enter another value. If there are no repeated digits, it displays "no repeated digits" and asks the user to enter another number. So far, this is what i have. It terminates the program when 0 is entered, but it always displays "no repeated digits" even if there are some.
#include <stdbool.h>
#include <stdio.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit;
long int n = 0;
printf("Enter a number: ");
scanf("%ld", &n);
while(n >= 0){
if(n==0)
break;
while (n > 0){
digit = n % 10;
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
n /= 10;
}
if (n > 0)
printf("Repeated digit: %d\n", digit);
else
printf("No repeated digit\n");
scanf("%ld", &n);
}
return 0;
}

A couple of things:
1: A bool only has two states: true and false. If you trying to build a frequency counter of each digit seen, for the presence of a digit more than once, then you should use a data type that can count to at least two, like a char or short or int, or your own enum.
2: This code:
if (digit_seen[digit]){
digit_seen[digit] = true;
break;
}
Is never going to be evaluated as true since you initialized digit_seen to be false at the start of your main function. What you should be doing is something like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
int digit_seen[10] = {0};
int entry;
int i, flag = 0;
printf("Enter a number: ");
scanf("%ld", &entry);
while(entry > 0)
{
int digit = (entry%10);
digit_seen[digit]++;
if(digit_seen[digit]>=2)
{
printf("Repeated digit: %d\n", digit);
}
entry /= 10;
}
for(i = 0; i < 10; i++)
{
if(digit_seen[i]>1) flag=1;
}
if(!flag)
{
printf("No repeated digits\n");
}
return 0;
}

#include <stdio.h>
int main() {
int seen [10] ={0}; // we set every element for a number is just 0
int N,rem;
printf("Enter the number:");
scanf("%d", &N);
while(N>0){
rem = N%10;
seen[rem]+=1;
N = N/10;
}
int i;
for(i=0;i<10;i++){ // checking the number seen counts
if(seen[i]==0){
continue;
}
printf("%d seen %d times\n",i,seen[i]); // just returned the given numbers informations
}
return 0;
}

Related

School exercice in c about prime numbers

Make a program that asks the user for an integer and says if the
number is prime or not. A number greater than 1 is prime if only
is divisible by 1 and by itself. Then, it will tell us what the prime number is.
example:
Enter a number: 8
8 is not first. The first one immediately superior to 8 is 11.
Enter a number: 5
5 is first. The first one immediately above 5 is 7.
I can only solve first part.
Here is my code:
#include <stdio.h>
int main() {
int num, i;
do {
printf("Enter a numer: ");
scanf("%d", & num);
}
while (num < 1);
for (i = 2; i < num; i++) {
if (num % i == 0)
printf("Its prime");
}
if (num % 1 == 0 && num % num == 0)
printf("Not prime");
return 0;
}
Try this logic. Not tested
#include <stdio.h>
int main()
{
int num, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
int isPrime=IsPrime(num)
if(isPrime==0){
numNext=num+1;
int nextPrimeNum=checkNextPrime(numNext);
}
}
int IsPrime(int num){
for(i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num%i == 0)
{
flag = 1;
break;
}
}
if (num == 1)
{
flag=1;//neither prime nor composite
}
return flag;
}
int checkNextPrime(int numNext){
int isNextPrime=IsPrime(numNext)
if(isNextPrime==0){
printf("This is the required output :"numNext);
return numNext;
}
else{
numNext=numNext+1;
checkNextPrime(int numNext)
}
}

Program C counting total number of odd digits

I'm writing a C program that counts the number of odd digits from user input.
Eg.
Please enter the number: 12345
countOddDigits(): 3
int countOddDigits(int num);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int num)
{
int result = 0, n;
while(num != 0){
n = num % 10;
if(n % 2 != 0){
result++;
}
n /= 10;
}
return result;
}
The code is not working.
Can someone tell me where does it go wrong?
There were a few mistakes in your code. Here is a working version of your code:
#include <stdio.h>
int countOddDigits(int n);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int n)
{
int result = 0;
while(n != 0){
if(n % 2 != 0)
result++;
n /= 10;
}
return result;
}
You are mixing n and num together - there is no need for two variables.
n%=10 is just causing mistakes - you need to check the last digit if(n%2!=0) and then move to the next one n/=10, that's all.
Looping variable is not correct. Your outer loop is
while (num !=0)
but the num variable is never decremented; the final statement decrements the n variable. My guess is you want to initialize
int n = num;
while (n != 0 )
{ ...
n/= 10;
}

finding a repeating integer in an array

Hi managed to make my program checked if i have repeated 8's. I also want to print out how many repeated 8's i have stored in the array.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit=0;
long n;
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
if (digit_seen[digit])
{
break;
}
digit_seen[digit] = true;
n/=10;
}
if (n>0 && digit ==8)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
If I understand your problem, you want to know the number of occurences a 8 is in your number. Like 88 has 2 8s. If that's the case, I don't see why you use a boolean array. First, you need a counter. Second, you need to know if digit is 8 for every digit and increment this counter if that's an 8. Here's an example :
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int digit=0;
long n;
int counter = 0;
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
if(digit == 8)
{
counter++;
}
n/=10;
}
if (counter > 0)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
In this example, counter would have the number of occurrences of 8s in your number. Just display it in the printf and it's done.
EDIT : here's a solution using an array:
#include <stdio.h>
int main(void)
{
int digit = 0;
long n;
int arrayNumber[10] = {0};
printf("Enter number: ");
scanf("%ld", &n);
while (n>0)
{
digit = n % 10;
arrayNumber[digit]++;
n /= 10;
}
if (arrayNumber[8] > 0)
{
printf("Repeated 8's");
}
else
{
printf("No 8's found");
}
return 0;
}
This way, you would know occurrence of every number from 0 to 9 in your integer. I'd also point out that you need to define what is a repeated number. If it's when there's at least 2 occurrence, you need to change arrayNumber[8] > 0 by arrayNumber[8] > 1

count digit until you get number of digits you want

I'm new to coding. I'm now learning C programing. I'm having a problem with counting digits. My objective is to only accept a fixed number of digits in the input. For example I want user to input only 7 digits number, so when they input anything else than a 7 digits number, the program should ask them to input again until it gets 7 digits number. Here is my attempt:
int n, count = 0;
printf("Please enter number:");
scanf("%d", &n);
printf("\n");
while (n != 0)
{
n /= 10;
count++;
}
printf("%d", count);
if (count != 7)
{
printf("You can use only 7 digits numbers");
}
You can try this one:
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000]; //Enter the maximum number of digits you expect as array size
printf("Please enter number: ");
scanf("%s", s);
while(strlen(s)!=7)
{
printf("%d\n", strlen(s));
printf("You can use only 7 digits numbers\n");
scanf("%s", s);
}
return 0;
}
After taking the input, you can easily convert it to int if you need to.
You can try this by creating a small function to check the number of digits in the number entered for you:
#include <stdio.h>
int count_digit(int n){
int count = 0;
if(n == 0) return 1;
while(n != 0)
{
n /= 10;
++count;
}
return count;
}
int main(void) {
int num;
printf("Please enter number:");
scanf("%d", &num);
while (count_digit(num) != 7){
printf("Please enter only 7digits number:\n ");
scanf("%d", &num);
}
printf("The 7digit number you entered is: %d",num);
return 0;
}

C largest number count

This is problem from textbook. I need to make users be able to enter integers and see the largest and count how many times the largest number appears. I got everything working except for the counting. I have been trying to it figure out for last week.
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
}
numOld = num;
if (numOld == big){
count++;
}else
count--;
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}
There is no need of count--, and you'll have to reset your count value each time you get new value for big.
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
count = 0;
}
numOld = num;
if (numOld == big){
count++;
}
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}
The function could be defined the following way
void bigEof()
{
int num;
int big;
int count = 0;
printf("Please enter an integer: ");
while ( scanf( "%d", &num ) == 1 )
{
if ( count == 0 || big < num )
{
big = num;
count = 1;
}
else if ( big == num )
{
++count;
}
printf( "Please enter next Integer <EOF> to stop: " );
}
if ( count != 0 )
{
printf( "The current biggest number is %d and is repeated %d times\n", big, count );
}
else
{
puts( "You did not enter numbers." );
}
}

Resources