-
Problem I Can't solve...
I'm just now learning to program using C at school. I've been working on a problem from my book for 3 days now and cannot find out how to make it work. If anyone can please help me out with this or post the code for it I would be forever grateful.
The problem:
Write a program that prompts a user for an integer value in the range 0 to 32,767 and then prints the individual digits of the numbers on a line with three spaces between the digits. The first line is to start with the leftmost digit and print all five digits; the second line is to start with the second digit from the left and print four digits, and so forth. For example, if the user enters 1234, then your program should print
0 1 2 3 4
1 2 3 4
2 3 4
3 4
4
______________
after working on it for some time I have only made it to here, which (if you try to run it) obviously doesn't work correctly.
Code:
// Import input/output library
#include <stdio.h>
// Main block
int main (void)
{
// Declare int to store character in.
int num[5];
// Print a prompt
printf("Please enter a number between or equal to 0 and 32767: ");
// Grab user input
scanf("%d",&num);
// Check if value outside of range
if ((num < 0) || (num > 32767))
{
printf("Outside of Range\n");
}
// Value is inside of range.
else
{
printf("%d %d %d %d %d\n", num[4], num[3], num[2], num[1], num[0]);
}
system("pause");
return 0;
}
-
Well, you really use wrongly the array in C.
num[5] is an array in which you can store 5 integers. Not 5 digits of an integer...
So you ll have to separate each digit from the integer.
E.g. to get the last digit you can do
Code:
int number;
scanf("%d", &number);
num[0] = num % 10;
in this way you get what remains of the division of the integer entered with 10. Thus, the last digit. You store that at num[0]. The digit entered is supposed to be stored at "number".
Figure out how to get all the digits.
Also, what if somebody enters a 3 digit number? You have to figure out a way to handle this. Well, that's not hard once you find out how to separate the digit, since you can also count them with the same code.
So find a way to separate the digits of an integer and we will work from there
-
I actually just figured it out. My book had the % 10 thing, but I still have no idea how to check the rest of the digits. I only know that % 10 is the far right digit, so if you could elaborate on that a bit it would be great.
Here's what I did, long and repetetive, but it works
Code:
// Import input/output library
#include <stdio.h>
// Main block
int main (void)
{
// Declare ints
int num, num0, num1, num2, num3, num4;
int digit4, digit3, digit2, digit1, digit0;
// Print a prompt
printf("Please enter a number between or equal to 0 and 32767: ");
// Get user input
scanf("%d",&num);
// Check if value outside of range
if ((num < 0) || (num > 32767))
{
printf("Outside of Range\n");
}
// Value is inside of range
else
{
// line 1 below
num4 = num / 10000;
digit4 = num4 % 10;
printf("%d ", digit4);
num3 = num / 1000;
digit3 = num3 % 10;
printf("%d ", digit3);
num2 = num / 100;
digit2 = num2 % 10;
printf("%d ", digit2);
num1 = num / 10;
digit1 = num1 % 10;
printf("%d ", digit1);
num0 = num / 1;
digit0 = num0 % 10;
printf("%d ", digit0);
// line 2 below
num3 = num / 1000;
digit3 = num3 % 10;
printf("\n%d ", digit3);
num2 = num / 100;
digit2 = num2 % 10;
printf("%d ", digit2);
num1 = num / 10;
digit1 = num1 % 10;
printf("%d ", digit1);
num0 = num / 1;
digit0 = num0 % 10;
printf("%d ", digit0);
// line 3 below
num2 = num / 100;
digit2 = num2 % 10;
printf("\n%d ", digit2);
num1 = num / 10;
digit1 = num1 % 10;
printf("%d ", digit1);
num0 = num / 1;
digit0 = num0 % 10;
printf("%d ", digit0);
// line 4 below
num1 = num / 10;
digit1 = num1 % 10;
printf("\n%d ", digit1);
num0 = num / 1;
digit0 = num0 % 10;
printf("%d ", digit0);
// line 5 below
num0 = num / 1;
digit0 = num0 % 10;
printf("\n%d \n", digit0);
}
system("pause");
return 0;
}
-
What % does is get the remainder of a division.
So 3 % 2 == 1, for example.
The repetitive code can be generalized into a loop, if you know how to do that.
The rest is just basic math (not much to do with C).