How can I display individual digits of an input number? - c

If I input this number: 1234, I want the output to begin with 1 2 3 4 not 4 3 2 1. How do I do this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers, count=0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
while(numbers>0)
{
num = numbers%10;
numbers = numbers/10;
printf("%d", num);
}
printf("The total number of digits is: %d\n", num);
return 0;
}

One of the solutions is to use recursion.
#include <stdio.h>
#include <stdlib.h>
// prints reverse order and returns count of digits
int printrev(int numbers)
{
if (numbers <= 0)
return 1;
int num = numbers % 10;
int count = printrev(numbers / 10);
printf("%d", num);
return count + 1;
}
int main()
{
int numbers, count = 0, num;
printf("\nEnter numbers: ");
scanf("%d", &numbers);
count = printrev(numbers);
printf("\nThe total number of digits is: %d\n", count);
return 0;
}

This topic was already answered in this stackoverflow question. Also, an easier way is to create an array where you can save the digits inside of while loop and then print it backwards. But in this case it would need to declare an array a priori.

How about to display character by character in string order?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers = 0;
printf("Enter numbers: ");
scanf("%d", &numbers);
char szNumber[16] = "";
snprintf(szNumber, sizeof(szNumber), "%d", numbers);
int i = 0;
while (szNumber[i] != '\0')
{
printf("%c ", szNumber[i]);
++i;
}
return 0;
}

Related

Letting user try inputting again after entering incorrect option

So I have some code that generates a set of random numbers. Afterwards, it asks the user if they would like to produce another set of random numbers. The problem I'm having is that I want the user to only input y for yes or n for no, and if they don't input those to options, tell them that is an invalid input and to try again. I cannot figure out how to do this. Here is the code:
// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size:\n");
scanf("%d", &lower);
printf("\nMaximum number size:\n");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated:\n");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
scanf(" %c",&c);
printf("\n");
if (c=='n'){
exit(0);
}
do {
printf("Please enter valid input!\n");
scanf(" %c", &c);
if (c=='n'){
exit(0);
}
} while (c!='y'||c!='n');
} while(c=='y');
return 0;
}
Any help would be appreciated.
This answers your question, but on a side note, I usually like putting the conditional in the first question so if the user enters zero then the app will quit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size: ");
scanf("%d", &lower);
printf("\nMaximum number size: ");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated: ");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
do {
printf("\t\t==> ");
scanf(" %c", &c);
c &= 0x5f; // convert to uppercase
if (c=='N') exit(0);
} while (c != 'Y');
} while (1);
return 0;
}

C Program : Display a Partially filled Array

I need some help displaying a float array that's partially filled. Not sure what I'm doing wrong, but here's what I have.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define SIZE 20
int main()
{
float score[SIZE];
float GPA, sum, avg;
int count, ctr, num_of_entries;
sum = 0;
for (count = 0; count < SIZE; count++)
{
printf("Enter a GPA. -1 to stop the data entry: ");
scanf("%f", &GPA);
if (GPA == -1)
break;
score[count] = GPA;
}
printf("Number of GPAs entered = %d", count);
num_of_entries = count;
printf("\n\nContent of the Array:\n=========================\n");
for (ctr = 0; ctr <= num_of_entries; ctr++);
{
printf("%.2f \n", score[ctr]);
}
_getch();
return 0;
}
I'm trying to display the entered GPA values as float variables with 2 decimal places. The printed result is a very large negative number. Any help would be greatly appreciated.

How to make a variable equal to a string of other variables in C

This is the full code, the dig variables are user inputs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int dig1=0;
int dig2=0;
int dig3=0;
num=dig1,dig2,dig3;
fflush(stdin);
printf("Please enter the first digit of your three digit number:");
scanf("%d", &dig1);
fflush(stdin);
printf("Please enter the second digit of your three digit number:");
scanf("%d", &dig2);
fflush(stdin);
printf("Please enter the third digit of your three digit number:");
scanf("%d", &dig3);
if (num==(dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3))
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
How could I make the variable "num" equal to all of the inputs for "dig1", "dig2", and "dig3". As in if dig 1 was 2 and dig 2 was 4 and dig 3 was 6, num would be 246. Please help!
Your problem can be solved this way:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int dig[3];
int i,j = 100;
int result = 0;
for(i = 0;i<3;i++){
fflush(stdin);
printf("Please enter the %d digit of your three digit number:",i+1);
scanf("%d", &dig[i]);
}
for(i = 0;i<3;i++){
result += pow(dig[i],3);
num += dig[i] * j;
j/=10;
printf("%d * %d = %d\n",dig[i],j,num);
}
if (num == result)
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
You could multiply these numbers according to their significant value:
int num = dig1 + dig2 * 10 + dig3 * 100;

Using arrays in c to find repeated digits in an number (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;
}

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

Resources