I need to make a program that can take numbers of up to 100 digits as input. No standard int datatype will be able to do that! I've never come across such a bizarre situation.
I don't get it at all. How am I supposed to solve this?
The question I'm working on is this:
A whole number will be given, and you have to make a program that will
determine whether it's an even or odd number.
Input Specification
In the first line, there will be an integer T denoting the number of
testcases. In the following T lines, a non-negative integer will be
given. The number can have a maximum of 100 digits.
Output Specification
For every whole number given, you will have to print whether it's odd
or even as output.
Can anyone guide me on how to solve the problem (if it is even possible to do so)?
The program will take a number as input and determine whether it's odd or even.
Read the input in a string (char [101]) and analyze only last digit to check whether number is odd or even. Rest of the digits are irrelevant for this task.
There is no standard numeric type guaranteed to hold that many digits. You need to store the value in a different way, e.g., as a string or other array. If you need to perform arithmetic on these numbers, you need to implement those operations for the types you use, or use some kind of arbitrary precision library.
(Tip: You also don't necessarily need the entire number for certain operations, e.g., you can tell whether it is even or odd by looking only at the last digit…)
The exercise is to determine whether a whole number of up to 100 digits is odd or even.
This does not require you to perform arbitrary arithmetic on the number, so if you need to handle numbers larger than the largest integer type on your system, you can treat them as a string of digits.
Whether it is even or odd only depends on the last digit.
To all those who took the time and effort to answer this question,
Thanks for the answers. And thanks for showing the way. I greatly appreciate the help!
The solution to the problem which I have coded is -
#include <stdio.h>
#include <string.h>
int main()
{
int T, i, j;
scanf("%d", &T);
for (i=1; i<=T; i++)
{
char N[101];
scanf("%s", N);
int k = strlen(N);
int p = N[k-1] - 48; //char to int conversion
if (p % 2 == 1)
{
printf("odd\n");
}
else
{
printf("even\n");
}
}
return 0;
}
Related
I am following the following function to calculate factorials of big numbers link, and I would like to understand a bit more why some things are happening...
#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];
int main(){
int num;
int i;
printf("Enter any integer number : ");
scanf("%d",&num);
fact[0]=1;
factorialof(num);
printf("Factorial is : ");
for(i=length;i>=0;i--){
printf("%d",fact[i]);
}
return 0;
}
void factorialof(int num){
int i;
for(i=2;i<=num;i++){
multiply(i);
}
}
void multiply(int num){
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++){
arr[i]=fact[i];
}
for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;
//printf("%d ",r);
}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}
My questions are:
What is the real meaning of the MAX constant? What does it mean if it's bigger or smaller?
I have found out that if I have a MAX = 10000 (as in the example), I can calculate up to 3250! If I try with 3251! I get a 'Abort trap: 6' message. Why is that number? Where does it come from?
Which would be the difference if I compile this code for a 32-bit machine with the flag -m32? Would it run he same as in 64-bit?
Thanks!
As Scott Hunter points out, MAX is the maximum number of elements in the fact and arr arrays, which means it's the maximum number of digits that can occur in the result before the program runs out of space.
Note that the code only uses MAX in its array declarations. Nowhere does it use MAX to determine whether or not it's trying to read from or write to memory beyond the end of those arrays. This is a Bad Thing™. Your "Abort trap: 6" error is almost certainly occurring because trying to compute 3251! is doing exactly that: using a too-large index with arr and fact.
To see the number of digits required for a given factorial, you can increase MAX (say, to 20,000) and replace the existing printf calls in main with something like this:
printf("Factorial requires %d digits.\n", length + 1);
Note that I use length + 1 because length isn't the number of digits by itself: rather, it's the index of the array position in fact that contains the most-significant digit of the result. If I try to compute 3251!, the output is:
Factorial requires 10008 digits.
This is eight digits more than you have available in fact with the default MAX value of 10,000. Once the program logic goes beyond the allocated space in the array, its behavior is undefined. You happen to be seeing the error "Abort trap: 6."
Interestingly, here's the output when I try to compute 3250!:
Factorial requires 10005 digits.
That's still too many for the program to behave reliably when MAX is set to 10,000, so the fact that your program calculates 3250! successfully might be surprising, but that's the nature of undefined behavior: maybe your program will produce the correct result, maybe it will crash, maybe it will become self-aware and launch its missiles against the targets in Russia (because it knows that the Russian counterattack will eliminate its enemies over here). Coding like this is not a good idea. If your program requires more space than it has available in order to complete the calculation, it should stop and display an appropriate error message rather than trying to continue what it's doing.
MAX is the number of elements in fact and arr; trying to access an element with an index >= MAX would be bad.
Error messages are often specific to the environment you are using, which you have provided no details for.
They are not the same, but the differences (for example, the size of pointers) should not affect this code in any discernible way.
Lets say we got two inputs. One being 123 and one being 321. Now, these two should return True.
Another eg. 543 with 345.
This is how far I've gotten:
int a,i=0;
printf("condition value");
scanf("%d",&i);
printf("comparison value");
scanf("%d",&a);
a=a%10;
i=a/10;
if(a==i){
printf("\nTrue");
}
Has anyone got any ideas on how to solve this?
If you want to know whether one string matches the reverse of another string, just compare character-by-character. Even if it's guaranteed that all the characters are digits, it's easier to solve the problem in the string domain.
Even if there's some number-theory trickery that would give you a closed-form solution for fixed-size integers, parsing strings into int in the first place will be slower than just a character-compare loop.
Often you can make your code simpler by taking advantage of limitations on the input, but it looks like this isn't one of those cases.
Given OP wants to check integers based on this comment
Simply reverse the digits of one of the numbers
Remove the least-significant digit from x, one at a time. Use that value to build up the reverse of x. Note that the range of the "reverse of x" is wider than the range of unsigned.
Use unsigned to avoid sign issues.
unsigned long long unsigned_rev(unsigned x) {
unsigned long long rev = 0;
while (x) {
rev = rev*10 + x%10;
x /= 10;
}
return rev;
}
I have just started competitive programming in SPOJ.I'm confused from sometime why i'm getting runtime error in ideone.The question is:
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133
Output:
818
2222
My program:
#include <stdio.h>
int main(void)
{
int t,i,reverse,same;
scanf("%d",&t); //t is no. of test cases
int num[t]; //num[t] is array of t elements
for(i=0;i<t;i++)
scanf("%d",&num[i]);
i=0; //since i will be equal to t therefore i is assigned to 0.
while(t--)
{
if(num[i]<=1000000)
{
while(num[i]++)
{
reverse=0;
same=num[i];
while(same>0)
{
reverse=reverse*10;
reverse=reverse+same%10;
same=same/10;
}
if(reverse==num[i])
printf("%d",reverse);
printf("\n");
if(reverse==num[i])
break;
}
}
i++;
}
return 0;
}
I don't know where i'm wrong.I'm sorry i'm asking this question may this question is asked by someone before.I tried to find the result but could not get the answer.Thankyou in advance and sorry for my bad english.
The question doesn't say that the number will be less than 1000000. It says that the number has less than 1 million digits. A number with a million digits looks like this
591875018734106743196734198673419067843196874398674319687431986743918674319867431986743198674319876341987643198764319876341987643198764319876431987643198763419876431987643198764319876139876...
You can't use scanf to read a number that has a million digits, and you can't store that number in an int.
The most likely reason for your error to occur is some memory fault. Keep in mind that online judges/compilers limit your available memory and if you try to allocate/use more memory than available, you get a runtime error. This also happens on your machine, but usually you have a lot more memory available for your program than in the case of online judges.
In your case, you could reduce the memory usage of your program by changing the data type of the num array from int to something like short or even char.
Hello guys I have found this question on some website
Description
Spade is a very good detective but he is not so good at math, this time his friend Archer has come to him with a very interesting math problem. Given two numbers 1 <= N <= 10^9 and 1 <= M <= 100, how many positive numbers with length N have the sum of its digits divisible by M. Archer is very obsessive and does not want numbers with leading zeroes to count. Spade has hired you to solve this problem, now his reputation is in your hands.
Input specification
Input contains a single line with two numbers N and M separated by a single space.
Output specification
Output a single line with the answer to the problem modulo 1000007.
Sample input
2 2
Sample output
45
My Code
Though I am getting the expected output it is not accepting my answer. Can any one please point out the error in the code.
#include <stdio.h>
int main(void)
{
int n,m,i,firstnum=1,count=0,lastnum=0,j=0,no=0;
scanf("%d",&n);
if (n>=1) {
scanf("%d",&m);
if (m>=1 && m<=100)
{
for (i=1;i<n;++i) {
firstnum*=10;
++count;
}
for (i=0;i<=count;++i)
lastnum=lastnum*10+9;
if (firstnum%m==0)
++no;
for (i=++firstnum;i<=lastnum;++i) {
j=i;
int sum=0,r;
do {
r=j%10;
sum+=r;
j=j/10;
} while(j);
if (sum%m==0)
++no;
}
}
}
printf("%d",no-1);
return 0;
}
You're trying to solve this problem by brute force, which won't work for the sizes involved. A number of length 10^9 can be up to 10^(10^9), which is a huge number that won't fit in an int or even a long long int. Even if it did, trying to enumerate all numbers of this length one by one would take billions of years.
You need to come up with an approach that doesn't look at the numbers one by one. Just like you can calculate that there are 33 numbers between 1 and 100 that are divisible by 3 without looking at them all, you need to come up with such an approach here. But here it will be harder because you will need to do it without actually calculating the value of 10^n.
Your approach is not actually correct.
You have declared "firstnum" variable as int, i.e. it cannot hold value greater than 2^(32-1) (on most of the on line judges). The max value of n in 10^9, hence you are trying to put 10^(10^9) in worst case.
I Hope you have got my point. I you want the approach you can comment below my answer. I don't want to spoil the question for you. :)
This small C script checks if a number is a prime... Unfortunately it doesn't fully work. I am aware of the inefficiency of the script (e.g. sqrt optimization), these are not the problem.
#include <stdio.h>
int main() {
int n, m;
printf("Enter an integer, that will be checked:\n"); // Set 'n' from commandline
scanf("%d", &n); // Set 'n' from commandline
//n = 5; // To specify 'n' inside code.
for (m = n-1; m >= 1; m--) {
if (m == 1) {
printf("The entered integer IS a prime.\n");
break;
}
if (n % m == 0) {
printf("The entered integer IS NOT a prime.\n");
break;
}
}
return 0;
}
I tested the programm with a lot of numbers and it worked... Then I tried a bigger number (1231231231231236) which is clearly not a prime...
BUT: the program told me it was!?
What am I missing...?
The number "1231231231231236" is too big to fit in an "int" data type. Add a printf statement to show what number your program thinks you gave it, and if that's prime, your program works fine; else, you might have a problem that merits checking. Adding support for integers of arbitary size requires considerable extra effort.
The reason you are having this problem is that intrinsic data types like int have a fixed size - probably 32 bits, or 4 bytes, for int. Given that, variables of type int can only represent 2^32 unique values - about 4 billion. Even if you were using unsigned int (you're not), the int type couldn't be used to store numbers bigger than around 4 billion. Your number is several orders of magnitude larger than that and, as such, when you try to put your input into the int variable, something happens, but I can tell you what doesn't happen: it doesn't get assigned the value 1231231231231236.
Hard to know without more details, but if your ints are 32-bit, then the value you've passed is outside the allowable range, which will no doubt be represented as something other than the value you've passed. You may want to consider using unsigned int instead.
The given number is too large for integer in C. Probably it only accepted a part of it. Try Printing the value of n.