Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a small simple school project for c programming. I am supposed to write a number and get the reverse of it and I couldn't understand how this code works. So, I need someone to explain to me what happens in the while loop, and why we use these operations, and whats %10?
Thanks!
/* Reverse number using while loop */
#include<stdio.h>
int main()
{
int num, reverse=0;
scanf("%d",&num);
while(num > 0)
{
reverse= reverse*10;
reverse = reverse + (num%10);
num = num/10;
}
printf("%d",reverse);
return 0;
}
'While loop' repeats the number of entered numbers.
'num%10' means the remainder of num divided by 10. This is the process to know the numbers at the end.
For example, if you enter '123' then while loop will repeat 3 times.
The first step, reverse -> 3, num -> 12
Second, reverse -> 30 -> 32, num -> 1
Third, reverse -> 320 -> 321, num -> 0
Therefore you can get the reverse number!
In your code while loop is the place where the digits of the entered number get reversed and stored as another number - reverse. In order to do that the last digits of the original number need to be accessed first. The % modulo operator returns the remainder of the division. Since you are dividing the original number by 10 you get the last digit of it, which is the remainder. We need to store that digit as the first of the reversed number and albo somehow get to the next digits of the original number. To store consequtive digits you multiply currently existing reversed number by 10 and add the next digit. That way you add next digit to the right of reverse, eventually, after adding all digits, getting the complete reversed number. Getting to the next digits of the original number uses the property of the division / operator which leaves the quotient of the division. When dividing by 10 it "cuts off" the last digit of the given number. That also explains the condition in the while loop: when your number becomes 0 it means you have gone through all of its digits.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am doing a program in C in which I have to read the value of the button in my TM-1638 and sent it to the 7-segment display. I got through the reading part now I am having problems to show the value in the display.
I have converted the value to BCD with this code:
const uint8_t dec[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,0x07,0x7f,0x6f};
if(readbyte > 0)
{
num <<= 4;
num |= readbyte % 10;
readbyte /= 10;
}
senddatabyte(0x0a, dec[num]);
senddatabyte(0x0c, dec[num]);
senddatabyte(0x0e, dec[num &0x0f]);
in which senddatabyte sends the value to the selected adress of the 7 segment display.
It works good when it have to display one digit number but it doesnt work when it displays tens and hundreds.
For larger numbers, you have to use a loop. Loop while the number is larger than or equal to 10. Take the number modulus 10 to get the least significant digit, which you can print on the display. Then divide the number by 10 and loop again. And then finally when the loop is done and what remains of the number is a digit smaller than 10, print that digit too.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a relatively simple program that asks for user input between a range and then checks it using Do - while loop.
int n;
do
{
print ("hello, world\n");
n = getvalue() //just making this syntax up.It takes value from user
}
while (n<0 && n>99);
print ("%d\n",n);
I want to prompt the user to input another value if he/she enters either a negative number or a triple digit number (or higher). But the condition within while is not being validated.
What the output looks like:
No matter what number I enter, it get printed. i.e. -2 is printed as -2, 101 as 101 and 50 as 50. Ideally, -2 should prompt user to enter a number again, so should 101 and only 50 should print out.
I think the problem is your rather cryptic condition of n < 0 && n > 99, which can't reasonably be satisfied.
You need to look more closely at the logic of your loop. If you want to prompt the user again if they enter a number less than zero or greater than 99, you need to use the logical or operator ||.
while (n < 0 || n > 99);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
C Programming
Ask user to enter a random number between 1 and 100. Then ask how many numbers he wants to display that precedes first number he enters.
Let’s say if user enter 9 and wants 3 numbers that precedes 9, your program should display this:
• 6 7 8 9
Have no idea need some help.
For asking the user a question, you can use something like printf or puts.
To request numbers from the user, scanf is probably the best approach for the level you're working at.
By way of example, here's a complete program that asks the user for a number then gives them the next number:
#include <stdio.h>
int main (void) {
int num;
printf ("Enter a number: ");
if (scanf ("%d", &num) != 1) {
puts ("That wasn't a valid number");
return 1;
}
printf ("The next number is %d\n", num + 1);
return 0;
}
Analysing that code, and what it does when it runs, should be enough to get you started.
For your specific project, the following pseudo-code should help:
print "Enter the ending number: "
input endnum
print "Enter the count of preceding numbers: "
input count
num = endnum - count
do:
print num
num = num + 1
while num <= endnum
That's the algorithm you can use, I won't provide it as C code since you'll become a better coder if you do that yourself. In any case, those pseudo-code lines all pretty much have a one-to-one mapping with C statements so it should be relatively easy to get something going.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
sscanf() is great for the problem i'm facing, but it's doing something unexpected.
tmp is a line read in from a file, i need to parse out parts of the line into separate pieces of data. curritem is a struct with int dollar, int cent, cat[], int stock, and int history[].
The odd issue i'm having is that when %d picks up a double zero (00), it appropriately inserts integer zero into destination, but upon confronting a single zero (0), the integer is not added appropriately to the destination; for example, while collecting data pertaining to history[], all is well, adding 1, 4, 8, 2, 13, etc. to the array, but a single zero cuts everything off there, as if NULL terminated at that point, which i presume is related.
The code:
sscanf(tmp, "%d.%d%s %d %d %d", &curritem.dollar, &curritem.cent,
curritem.cat, &curritem.stock, &curritem.history[0],
&curritem.history[1]);
I have left out some indices of curritem.history[], as it clutters the necessary information.
Example of what i should get, if all integers were added correctly:
curritem.history[0...11] = 5 2 6 1 0 11 9 0 15 0 7 10
What actually results as of right now:
curritem.history[0...11] = 5 2 6 1
Something dies when sscanf() sees a single '0' via %d and adds that to &struct.intarray[n]
Any ideas?
Try not to recommend an entirely different solution to my general program function, sscanf() is working wonders in every other aspect.
Thank you for your time!
EDIT:
A snippet of exact input:
WL162343Fleece-Lined Double L Jeans 49.95PANTS 3 8 1 0 1 0 7 1 5 2 10 2 6
All information is acquired successfully until after the category- in this case, 'PANTS'.
With this example, my int array called history should hold the values
3 8 1 0 1 0 7 1 5 2 10 2 6
But upon printing every item in the array:
int n = 0;
for (n = 0; curritem.history[n]; n++) {
printf("%i ", curritem.history[n]);
}
The following output results:
3 8 1
EDIT:
The 'for' loop used for printing is incapable of distinguishing between an integer '0' and a null-termination of the array. Annoying.
What do you think that curritem.history[n] will return in the for's condition when it contains zero?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to make a program that will divide a number by 2 only if that number is divisible by 2. I made it so that if the result of the number divided by 2 is a float, then divide it by two, like this:
int p;
printf("Percentage please: ");
scanf("%d", &p);
while (((p/2) != // a floating point value)
{
p = p/2;
}
But the problem is that I'm not sure if there is a function in the C standard libraries like isint() or iswholenumber(). Is there any function or any way I could implement something similar?
Any help would be appreciated.
You are looking for the modulo operation, that returns the rest of the division, so:
if( n % 2 == 1) // the number is not divisible by 2
if( n % 2 == 0) // divisible by 2
When you divide two ints the result is always an int (edit: truncated):
1/2 --> 0
2/2 --> 1
3/2 --> 1
So the logic p/2 is not a float does not make sense. Instead, as others have suggested, you want to use the modulo operator which returns the remainder of the division:
if( n % 2 ) // not divisible by 2
{
}
else // divisible by 2
{
}
Note: Since all integers that do not evaluate to 0 are equivalent to true you do not need to check n % 2 != 0.
you could ask the user for a string, then use int.TryParse
int x;
if (int.TryParse(inputString, out x))
{
// input is an integer.
}