Hello I have code to find out the first two digits of a number and save them to a variable in C and the code only works for even length numbers. I need something that will work for both even and odd length numbers. The number I need for first2 is the first two numbers.
long long int input = 6789466321
first2 = input;
while(first2 >= 100)
{
first2 = first2 / 100;
}
Divide by 10 at a time instead of 100 so that you are only removing one digit each iteration.
A function to return the first 2 digits of a long given an input that is a positive number and at least 2 digits (e.g. 10 or above).
int getFirst2Digits(long input)
{
long local = input;
while (local >= 100)
{
local /= 10;
}
return local;
}
Related
I am trying to find each digit of a number entered by the user.
The length of the number is up to the user.
So far I have:
managed to limit what the user enters by using long long card, meaning they cannot enter more than about 18 or 19 characters and only numbers are accepted.
managed to get the first and last digit of that number, as well as the count of the number.
Now I wanted to:
specifically ask for no more than 16 digits max - > but it keeps saying undeclared identifier and am not sure where I go wrong
be able to identify each digit, i.e.:
53689
1st digit:5
2nd digit:3
3rd digit:6
etc.
I learned about modulus but I can't figure out how to tell it to stop at the 2nd last/3rd last digit.
I feel it's something really simple but how? especially if I don't know if the user enters 5 numbers or 15, otherwise I could say (repeat modulus x-1 times) or something like that.
Just for now, my code in case you see anything for #1.
Before I tried to limit the entry-conditions, it worked fine showing me card, card length and 1st,last digit.
#include <cs50.h>
#include <stdio.h>
//finding first 2 numbers and last number and checking what card type
int main(void)
{
int count = 0;
do
{
long long card = get_long_long("Enter your card number: ");
}
while (card ! = 0)
{
card = card / 10;
count++;
}
printf("Card number: %llu\n", card);
printf("Number of digits: %d \n", count);
firstdigit = card;
while (firstdigit >= 20)
{
firstdigit = firstdigit / 10;
}
printf("First digit = %d \n", firstdigit);
lastdigit = card % 10;
printf("Last digit= %d \n", lastdigit);
return 0;
}
info: Yes it is from CS50 credit, I can't understand the full exercise yet so I decided to take it apart and only learn how to write a program that analyses the number for first 2 digits and tells me what card type it is. To do so I'm trying to learn how to count every number on any given number length for practice, for me to actually understand this slowly, so please don't share a full answer to the credit problem.
I also haven't learned arrays or more complex solutions yet either.
As others have advised, "break the problem down into smaller pieces".
Here's an example of that approach. Instead of dealing with a massive number all at once, this 'segments' the number into the typical 4 sections embossed into a credit card. This might give you a start toward a solution...
int main() {
int count = 0;
long long card = get_long_long("Enter your card number: ");
long long cpy = card;
int blk4 = cpy % 10000; // 4 rightmost digits as int
cpy = cpy / 10000; // shift right
int blk3 = cpy % 10000; // next 4 digits as int
cpy = cpy / 10000; // shift right
int blk2 = cpy % 10000; // next 4 digits as int
cpy = cpy / 10000; // shift right
int blk1 = cpy % 10000; // leftmost 3-4 digits as int
printf( "Card number: %04d %04d %04d %04d\n", blk1, blk2, blk3, blk4 );
return 0;
}
Having shown that, please be advised that a credit card "number" is not a "number" in the usual sense. It is a string made-up only of digits. Trying to solve this CV50 problem with "integers" is going to lead to tears. It's time to learn about arrays of characters (even if every character is an ASCII digit)...
I am doing this problem:
"We have a huge decimal number N. Write a program to determine the followings:
The number of digits in N.
Is N an even number?
The number of zeros in it.
Is N a multiple of 11? Note that we can determine if N is a multiple of 11 by checking the difference between the sum of the odd positioned digits and the sum of the even positioned digits. For example, 82375 is not a multiple of 11 because the sum of the even positioned digits is 2 + 7 = 9, and the sum of the odd positioned digits is 8 + 3 + 5 = 16, and the difference between 9 and 16 is 7, which is not a multiple of 11.
We will give you the number one digit per line. For example, if you get digits ‘1’, ‘2’, ‘3’, ’4’, ‘0’ in order, then the number is 12340. The number will not start with 0.
Input Format
The input has several lines. Each line has a digit. EOF indicates the end of input.
Output Format
Output the four answers above line by line. If the number is even output a 1; otherwise a 0. If the number is a multiple of 11 output a 1; otherwise output a 0.
Subtask
10 points: you can store the decimal number in an integer without overflow
10 points: the number of digits is no more than 32768, so you can store digits in an array
80 points: you will get MLE if you use array"
my code is:
#include <stdio.h>
#include <stdbool.h>
int digit(long n);
int is_even(int n);
int count_zeros(long n);
int is_multiple(long n);
int main() {
int digits = 0;
long x;
scanf("%ld", &x);
digit(x);
int even = is_even(x);
printf("%d\n", even);
printf("%ld\n",count_zeros(x));
printf("%ld\n", is_multiple(x));
}
int digit(long n)
{
int digits = 0;
while (n > 0) {
n /= 10;
digits++;
}
printf("%ld\n", digits);
}
int is_even(int n)
{
if (n % 2 == 0)
return true;
else
return false;
}
int count_zeros(long n)
{
int count = 0;
while (n > 0) {
n /= 10;
if (n %10 == 0)
count++;
}
return count;
}
int is_multiple(long n)
{
if (n % 11 == 0) {
return true;
}
else
return false;
}
Basically i dont know how to meet the problem's requirement, so I made a simpler version of the problem. Any clue on how to do this?
If you comment on this, please be nice, I am a beginner and people was rude in the past,if you have nothing important to say, do not be mean/do not comment.
Well, the first problem with your current version is it only reads one integer. However problem states that each digit is on a separate line. The first approach may be to just replace that scanf with a loop and keeping multiplying by 10 and accumulating until end of file. Then the rest of the program would work fine.
A more advanced approach will be to use an array to store the digits. An integer can hold a very limited number of digits whereas you are only bounded with the size of available memory using array.
So in the reading loop rather than storing digits in an integer, you can store digits in an array (which could be fixed size because an upper limit is given). But for the rest of the program you should change the calculation to use digits in the array instead of the regular integer arithmetic.
For a homework program we are asked to check if credit card numbers are valid.
While I am fairly sure about how I want to solve this problem I am stuck with iterating over a creditcard number and getting every second digit.
I already know that I need to use modulo and division and I do get the second to last number, but I am lost at how to iterate over the rest of the numbers.
In the code is the code I used to check that I did indeed get the second to last number, and after that some code I tried to use to iterate over the complete number. Hope somebody could show me in the right direction!
long number = 378282246310005;
int digit;
int cc_number;
cc_number = number % 10;
digit = cc_number / 10;
printf("%i\n", digit);
while (number > 0)
{
digit += number % 10;
number /= 10;
printf("%li", number);
}
printf("\n");
Before your while loop you reversed the last digit and the remaining number.
number % 10 is the last digit, number / 10 is the remaining number.
Adding to digit doesn't make sense if the variable is intended to contain a single digit. (You may need to add the digits for a validity check.)
Example code to extract all digits and their positions:
#include <stdio.h>
int main(void)
{
long number = 378282246310005;
int digit;
int position = 0;
printf("%li\n", number);
while (number > 0)
{
digit = number % 10;
number /= 10;
printf("(%i) %i ", position, digit);
position++;
}
printf("\n");
return 0;
}
This prints
378282246310005
(0) 5 (1) 0 (2) 0 (3) 0 (4) 1 (5) 3 (6) 6 (7) 4 (8) 2 (9) 2 (10) 8 (11) 2 (12) 8 (13) 7 (14) 3
To select every other digit you could check position % 2 or toggle a variable between 0 and 1 in every loop cycle.
Edit:
Explanation as requested in ron_g's comment: "Why's it printing out the digits in reverse?"
The loop separates the last digit from the remaining part of the number, so it prints the last digit first. Then it repeats the same with the remaining part as long as the remaining number is not 0.
The answer keeps the original implementation from the question which is based on storing the number as a long int. In this case it is easier to start with the last digit. When the number would be stored as a string it would be easier to start with the first digit.
/* number is initially the whole number or the remaining part */
while (number > 0)
{
/* remainder of division by 10 is the last digit */
digit = number % 10;
/* division by 10 is the remaining part after cutting off the last digit */
number /= 10;
printf("(%i) %i ", position, digit);
position++;
}
I am writing a program in which I am asking a user for long long int. After the user has provided me with a number, I want to add every other digit in that number, starting from the second digit. Now my question is, how can I select every other digit in long long int? (C language)
Given a integral type number n, n % 100 / 10 will extract the second to last digit. This expression is a touchstone for your knowledge of operator precedence and associativity.
You'll need to use n % 10 and n / 10 to extract and subsequently remove the last digit if the number of digits in the number is even (search around on this site for adequate algorithms to count the number of digits in a number).
n / 100 will remove the final two digits.
Put the above into a loop, and you're done.
You can do :-
#include<stdio.h>
int main()
{
long real_Number,num,arr[100],count=0;
scanf("%ld",&real_Number);
do{
if(real_Number>99)
{
num=real_Number%100;
num=num/10;
arr[count]=num;
real_Number=real_Number/100;
++count;
}
if(real_Number<100)
{
arr[count]=real_Number/10;
break;
}
}while(real_Number>=10);
}
Note : The Digits stored in array are backward for example 123456 results in
arr[0]=5, arr[1]=3 ,arr[2]=1;
To print:-
for(count; count>=0 ;count--)
{
printf("%ld",arr[count]);
}
Ok everyone so i have a 9 digit number in input (123456789) and i need to break it down to first middle and last three numbers (123,456,789).
I know that i can use modulo to get the last three digits.
first_group_of_three = input number % 1000;
But i have no idea how to get the other two groups.
Any help would be appreciated.
UPDATE !
Thanks to milevyo answer i managed to figure it out.Now for the code freaks out there,i am a beginner programmer and i would like to ask what is a more efficient way to write this code(below),maybe using less variables or some other technique.I didn't transform it to char because we are not allowed to since we still "havent learned it"
scanf("%d" , &input_number);
last_group_of_three = input_number % 1000;
temp = (input_number-last_group_of_three)/1000;
middle_group_of_three = temp % 1000;
first_group_of_three = (temp-middle_group_of_three)/1000;
first_group_of_three = input number % 1000;
shift=(input number-first_group_of_three)/1000
second_group_of_three = shift % 1000;
and so on
Simplification: #Barmar
scanf("%d", &input_number);
last_group_of_three = input_number % 1000;
input_number /= 1000;
middle_group_of_three = input_number % 1000;
input_number /= 1000;
// % 1000 only if original input exceeded 9 digits
first_group_of_three = input_number % 1000;
Note: many compilers will optimize the % 1000 and / 1000 into a single operation providing the two results: remainder & quotient.
(OP has not mention what code should do with negative numbers.)
You can input the numbers as string in a char table and then the first 3 numbers will stay the 4th char of table will be the char ',' and the 5th,6th,7th char will be the next 3 numbers etc. So you will finally create the string "123,456,789", then print.