as in the code given below i want to multiply 1 by 2 then delete it and so on but i don't how to do that i made the first steps to delete the last number by using %10 then show it with the /10 i hope someone can help

#include <cs50.h>
#include <stdio.h>

int main(void)
    long number;
        number = get_long("Number: ");

    int length = 0;
    while (number > length)
        number = number / 10;
    if (length != 13 && length != 15 && length != 16)
    int last;
    long temp = number;
        last = temp % 10; //4003600000000014
        temp = temp / 10; //400360000000001      
    while(number > 0);
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Dec 15, 2023 at 1:27

1 Answer 1


Number of problems... this list will likely grow.

  1. You didn't initialize number when you declared it. Get in the habit of always initializing your variables. Here a good value to use is -1 because that's not a valid value.

  2. Don't need a do... while loop anymore... you can just have a while. But more importantly the condition of your while loop is wrong. 'number' an 'int' is not going to be equal to '-' initially. You didn't define it originally it will be equal to whatever the user entered. And if they happen to enter the ascii number that represents -, they are going to get sent back around. Better idea would just be while ( number < 0 ) given you initialize number to -1 like I suggested in step 1.

  3. In your next while loop to determine the length of number you destroy number in the process (you keep dividing until nothing is left..) and it is not the original value anymore. So when you then do long temp = number this will be an assignment to '1' as a result of your repeated divisions. You need to use a temporary variable here so that you dont destroy number.

  4. What does the algorithm want you to do? Does it differ based on if you have an even number length or an odd length which digits get multiplied by two or not? Or does starting from the back render this moot?

If you can't figure out what you need to do I'd strongly urge you to solve cash first instead of credit.

The way to extract the '1' is to continue to the loop, which will modulo by 10 and give you last = 1 and then it will divide by 10 and delete the 1. If you want to multiply it by two, then you would in this case multiply last by 2. How you want to accomplish this in the loop and know when to multiply by 2 and when not to and how you keep track of the numbers you have processed are all up to you and a key part of solving this problem and learning that you are supposed to demonstrate mastery of in order to solve this problem so I can not tell you anymore right now. I am not saying any of this to be mean or rude, I'm saying this to be helpful: Remember your goal isn't to solve the problem it's to learn how to solve the problem. When you can't even clearly articulate what you are trying to do to carry out the algorithm it makes it clear that you either don't understand the problem/algorithm or you don't understand the fundamentals you were supposed to have learned from the week 1 materials. Your problem isn't really about getting the '1' and multiplying it by '2', your problem is you don't know seem to know how to use variables to help you keep values you need to store work along the way to building a solution, and you don't seem to have the notion that you'll need to use a conditional statement in the loop based on which digit you are working on as to how you'll treat it.

Strongly suggest you write out in pseudo code what you would do first and then figure out the C syntax for it in this case. Break problems into subproblems, when you solve all the subproblems the problem will be solved.

Good luck

  • thank u i made some changes in my code but it fails in 4 test and i think there is logic mistake in first do while loop which i use it to multiply by 2 there is my code bpa.st/YXGQ and that's my check50 submit.cs50.io/check50/90821ad2e40df1897e43f9b28094be25bc41bbef Dec 16, 2023 at 10:56
  • open up another question entirely. but note that I'm not going to address it if you dont post the code and if you dont take the suggestions i offered here and don't run style50 on your code first. I can't remotely follow your code with the crazy indentation you have going on. but there are MULTIPLE errors in how you are carrying out the checksum function. You are multiplying every digit by 2 you arent supposed to do that. and if its greater than 9 you need to add 1, subtract 10 and add whats left (for the numbers you are actually supposed to be multiplying by 2)
    – UpAndAdam
    Dec 16, 2023 at 19:59
  • go over by hand what the algorithm is supposed to do. follow the Luhn's algorithm written summary in cs50.harvard.edu/x/2023/psets/1/credit and then trace that with your code. is your code doing what it says? (it's not)
    – UpAndAdam
    Dec 16, 2023 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .