It takes a lot of time to compile, and some random number shows up, apparently the scanf() doesn't ask for the input
#include <stdio.h>
int main() {
int a;
//a = 1472464;
scanf ("%d", &a);
if ((a % 6) && (a %4) == 0)
{
printf("Input %d is divisible by 6 and 4\n", a);
}
else {
printf(" Input %d is not divisible by 6 and 4\n", a);
}
printf("Hello, World!\n");
return 0;
}
This line is wrong:
if ((a % 6) && (a %4) == 0)
It should be:
if ((a % 6) == 0 && (a %4) == 0)
I don't see any other obvious problem with the code.
The expression (a % 6) && (a %4) == 0 does not compare both modulo-operations with zero. Instead it does (a % 6) which will result on a number between 0 and 5, and use that as a boolean value that it then uses with the result of (a %4) == 0.
Instead you need to do each comparison separately: (a % 6) == 0 && (a % 4) == 0
The important thing to know here is that in C only zero and a null pointer is considered "false". Anything that is not zero (or a null pointer) is true.
That means that if a for example is 4 then a % 6 will be "true" since a % 6 is 4 which is not zero. Conversely when a is for example 6 then a % 6 will be 0 which is "false".
So using only a % 6 will actually give the opposite result to what you want, it will be "true" when a is not evenly dividable by 6.
Related
if ((a % 5) && (a % 11))
printf("The number %d is divisible by 5 and 11\n", a);
else
printf("%d number is not divisible by 5 and 11\n", a);
How will the logical && operator work if I don't add == 0 in the expression, if there is no remainder, will it look for the quotient? and the quotient will always be a non zero term so the programme will always return true.
In your code
if ((a % 5) && (a % 11))
is the same as
if ( ((a % 5) != 0) && ((a % 11) != 0 ) )
Any non-zero value is taken as TRUTHY.
According to the C Standard (6.5.13 Logical AND operator)
3 The && operator shall yield 1 if both of its operands compare
unequal to 0; otherwise, it yields 0. The result has type int.
In the expression used in the if statement
if ((a % 5) && (a % 11))
if each operand a % 5 and a % 11 is unequal to 0 then the expression evaluates to logical true. That is when a is not divisible by 5 and is not divisible by 11 then the expression evaluates to true and as a result a wrong message is outputted in this statement
printf("The number %d is divisible by 5 and 11\n", a);
To make the output correct you should change the expression in the if statement the following way. Pay attention to that you need also to change the message in the second call of printf.
if ((a % 5 == 0) && (a % 11 == 0 ))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
printf("The number %d is divisible by 5 and 11\n", a);
else
printf("%d number is either not divisible by 5 or by 11\n", a);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#Saurav's answer best describes about your problem. In addition to it, if you want a solution in case you are not in mood to add == 0, then you could just simply use ! (NOT) operator:
if (!(a % 5) && !(a % 11))
Now it will show divisible only when both of the expression has zero values (i.e. no remainder - like the number 55).
Please explain to me why this code is wrong for the task and below I have explained all the four conditions -[][1]
#include stdio.h
int main()
{
int n;
scanf ("%d", &n); //taking input
if (n / 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (n % 2 == 0 && 2 <= n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (n % 2 == 0 && 6 <= n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n % 2 == 0 && n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
this is the image for the question[1]: https://i.stack.imgur.com/OtY7o.png**
Testing for Odd or Even
n / 2 != 0 does not test whether n is odd. n/2 calculates the quotient that results from dividing n by 2 (rounding any fraction down). So 0/2 is 0, 1/2 is 0, 2/2 is 1, 3/2 is 1, 4/2 is 2, and so on. So n / 2 != 0 is true for all n other than −1, 0, and 1.
To test whether a number is odd, you can use n % 2 != 0. n%2 calculates the remainder from the division. If it is zero, n is even. If n is not zero, n is odd.
Using Else Efficiently
Once you have tested whether n is odd using n % 2 != 0, you do not have to test whether it is even in the else clauses. The else expressions and their statements will be evaluated only if the if expression is false, which happens (after the correction above) only when n is even. So we do not need to test again.
Testing For an Interval
In C, 2 <= n <= 5 does not test whether n is between 2 and 5. It is parsed as (2 <= n) <= 5. This is evaluated by comparing 2 to n, which produces 0 (if false) or 1 (if true). This result, 0 or 1, is then used in … <= 5. Since 0 and 1 are both less than or equal to 5, the result is always 1 (for true).
To test whether n is greater than or equal to 2 and less than or equal to 5, you must write this out explicitly: 2 <= n and n <= 5, which we join with the “and” operator, &&: 2 <= n && n <= 5.
Other Issues
The proper form for including stdio.h is #include <stdio.h>, not #include stdio.h.
A proper declaration for main is int main(void), not int main().
Corrected Program
A program with these issues corrected is:
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n); //taking input
if (n % 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (2 <= n && n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (6 <= n && n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
There is a typo in the line:
if (n / 2 != 0)
The compiler will not complain, but you will get unexpected results at run time.
Here you meant to check if the remainder of division by 2 is not equal to zero (i.e.: modulus operator), and not the division by 2. This line should be
if (n % 2 != 0)
Second thing: you can't tell C to compare values in ranges like this 2 >= n >= 4. You will have to split the comparison into 2 comparisons. This line:
else if (n % 2 == 0 && 2 <= n <= 5)
Should be:
else if (n % 2 == 0 && 2 <= n && n <= 5)
You will need to fix all the lines that have this comparison as well.
This question already has answers here:
modulo operation on negative numbers [duplicate]
(2 answers)
Closed 5 years ago.
I am trying to see if two input numbers (integers) including negative numbers go into 6 evenly (remainder is 0). This is the code I was trying.
if((in1)%6 == 0 && (in2)%6 == 0){
printf("Divisible: both\n");
}
else if((in1)%6 == 0 && (in2)%6 > 0){
printf("Divisible: only %i\n",in1);
}
else if((in1)%6 > 0 && (in2)%6 == 0){
printf("Divisible: only %i\n",in2);
}
else{
printf("Divisible: neither\n");}
This works for all positive integer but for any negatives the printed code is always "Divisible: neither" any help as to how I can show both positive and negative numbers divisible by six with a remainder of 0 would be really helpful
You could use != 0 instead of > 0. In C, % of negative number will give a negative result (or zero).
This is because a / b is defined as truncation-towards-zero since C99 (in C90 it was implementation-defined). And a % b is defined as a - (a / b) * b.
Note that you actually do not need this test at all; you can rely on the behaviour of if...else not entering the else case if the if case was satisfied, e.g.:
if ( in1 % 6 == 0 && in2 % 6 == 0 )
{
// ...
}
else if ( in1 % 6 == 0 )
{
// would not reach here if in2 % 6 == 0
}
else if ( in2 % 6 == 0 )
{
// would not reach here if in1 % 6 == 0
}
else
Another consideration, rather than oblige code to test numbers 3 times, re-write to perform only 2 test on the numnbers.
if (in1 % 6) {
if (in2 % 6) {
printf("Divisible: both\n");
} else {
printf("Divisible: only %i\n",in1);
}
} else {
if (in2 % 6) {
printf("Divisible: only %i\n",in2);
} else {
printf("Divisible: neither\n");}
}
}
I'm a first time programmer trying to complete a simple command line program as part of the first assignment for an online course I am taking, but I seem to have hit a roadblock that I can't figure out with GDB or my own research.
After hours of rewrites, and hours of debugging, I finally got the code below to compile. The program is supposed to take a credit card number as an input, and then check whether it's valid per the specifications of the assignment. I used a test number from here: PayPal Test Credit Cards
The odd thing is, when I enter an AMEX card number, it correctly produces the text "AMEX", but when I try a Visa or a Master Card, it prints "INVALID".
In GDB I broke at the Verify function and it seems to incorrectly skip these two if/else if statements without proceeding to the Checksum function even though conditions appear to be met.
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
...
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
...
The AMEX line of code that correctly executes is:
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
The arguments for all three lines seem to be formatted exactly the same. That is far as I could get in GDB though. I would print totalDigits, firstDigit, and secondDigit in GDB right before stepping through the above two non-executing lines and everything looked correct. So I'm stumped, why is the AMEX line executing, but not the others?
Thanks in advance everyone. This is the first program after hello.c that I've tried to write, so I am open to absolutely any criticism or suggestions if it looks like I'm doing something weird/wrong.
Full code:
checker.c
#include <stdio.h>
#include <stdlib.h>
int MAX = 16;
int* DigitSort(unsigned long long x, int* array);
int Verify(int* array);
int main (void)
{
int* output = malloc (sizeof(int) * (MAX + 2)); // creates a blank array for the individual digits of the card number.
unsigned long long userInput = 0;
do
{
printf("Please enter a credit card number:\n");
scanf("%lld", &userInput);
}
while (userInput <= 0); // checks to make sure the user entered a number.
switch(Verify(DigitSort(userInput, output))) // sorts the user's input into individual digits and verifies the card type and validity.
{
case 1 :
printf("VISA\n");
break;
case 2 :
printf("MASTERCARD\n");
break;
case 3 :
printf("AMEX\n");
break;
case 0 :
printf("INVALID\n");
break;
default :
printf("INVALID\n");
}
free(output);
return 0;
}
int Verify(int* array) // verifies whether or not a card number is valid. Must pass the function a sorted array of individual digits.
{
int* cardNumber = array;
int firstDigit = cardNumber[0];
int secondDigit = cardNumber[1];
int totalDigits = 0;
int Checksum(int* cardNumber, int totalDigits);
int i = 0;
while (firstDigit >= 1 && cardNumber[i] >= 0) // this step counts the number of digits in the array.
{
totalDigits = totalDigits + 1;
i++;
}
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
{
return 1;
}
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
{
return 2;
}
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
{
return 3;
}
else // if the card number doesn't match any of the above conditions or fails the checksum, an 'I' for Invalid is returned.
{
return 0;
}
}
int* DigitSort(unsigned long long x, int* array) // takes a long long as input and sorts it into individual digits
{
int* arrayReversed = malloc (sizeof(int) * (MAX + 2)); // creates a new array to hold the reversed order of digits.
int i = 0;
arrayReversed[0] = 0;
if (i < (MAX - 1) && x >= 10)
{
do
{
arrayReversed[i] = x % 10;
x = x / 10;
i++;
}
while (i < (MAX -1) && x >= 10);
}
if (i < MAX && x >= 1 && x <= 9)
{
arrayReversed[i] = (int) x;
x = (x - x);
}
if (x == 0)
{
int j = 0;
do
{
array[j] = arrayReversed[i]; // sorts the digits from the reversed array and places them into the sorted array.
j++;
i--;
}
while (j < MAX && i >= 0);
array[j] = -1;
}
free(arrayReversed);
return array;
}
int Checksum(int* cardNumber, int totalDigits)
{
int sum1 = 0;
int sum2 = 0;
int i = (totalDigits - 2);
int j = (totalDigits - 1);
while (i >= 0)
{
sum1 = ((cardNumber[i] * 2)%10) + ((cardNumber[i] * 2)/10) + sum1;
i -= 2;
}
while (j >= 0)
{
sum2 = (cardNumber[j] + sum2);
j -= 2;
}
if (((sum1 + sum2) % 10) == 0)
{
return 0;
}
else
{
return 1;
}
}
Your first problem is here:
if (firstDigit == 4 && totalDigits == (13 | 16) && ...
You need to write:
if (firstDigit == 4 && (totalDigits == 13 || totalDigits == 16) && ...
Your first check is looking for 0x1D == 29 as the number of digits (because, as paisanco points out in a comment, the | operator is the bitwise OR operator), and no credit card needs 29 digits (yet, and not for a long time to come). Note the extra parentheses for clarity and accuracy. Don't mess around risking removing them — the code won't work properly again. And in general, be explicit if your condition has both && and || operators and use parentheses to group terms explicitly.
You have similar problems elsewhere. As it happens, (4 | 7) is the same value as 7, so the condition works when the second digit is 7 (but not when it is 4). But it doesn't mean what you intended it to mean.
Computer languages don't work the same as human languages. Get used to writing out the condition somewhat more verbosely. Some other languages provide shorthands for these conditions; C is not such a language.
I wanted to check if a fraction 2 4 (for example) can be simplified to 1 2!!
However logical condition fails.
#include <stdio.h>
int main()
{
int a,b,live=1;
printf("\n\nInput integers for fraction:");
scanf(" %d%d",&a,&b);
while(live){
if(!(a%2 && b%2)){
a/=2;
b/=2;
}else if(!(a%3 && b%3)){
a/=3;
b/=3;
}else if(!(a%5 && b%5)){
a/=5;
b/=5;
}else if(!(a%7 && b%7)){
a/=7;
b/=7;
}else live--;
}
printf("Simplified Fraction is %d/%d",a,b);
}
The condition a%2 is equivalent to a%2 != 0, i.e. it tests if a is not divisible by 2. From De Morgan's Laws, the condition if(!(a%2 && b%2)) is equivalent to if(!(a%2) || !(b%2)) or if((a%2 == 0) || (b%2 == 0)), which is not what you want.
You really want to test if((a%2 == 0) && (b%2 == 0)) -- that is, if both are divisible by 2, not if either is divisible by 2. Writing it this way is also much less confusing.
And it should also be obvious that in order to simplify any fraction, you need to test for all possible prime factors, which is impossible to do with a finite number of if statements. The recommended way of doing this is to use the Euclidean algorithm to determined the greatest common divisor of the numerator and denominator, and then you divide both by the GCD to get the fraction in reduced form.
(!(a%2 && b%2)) will yield true even if only one of a%2 or b%2 holds.
Have a look at the following example:
3/4 -> a%2 == 0, b%2 == 1 -> (a%2 && b%2) == 0 -> (!(a%2 && b%2)) == 1
You are looking for (a%2 == 0 && b%2 == 0) instead of your condition, and similarly for other conditions.
An "after an accepted answer" answer.
This does not detail the issues with OP's code nicely like #Adam Rosenfield, but does address the larger OP desire of "I wanted to check if a fraction 2 4 (for example) can be simplified to 1 2!!" in a general way.
Use the Euclidean Algorithm to find the greatest-common-denominator, then divide a,b by it. No need to generate prime number list. Very fast.
// Euclidean Algorithm
unsigned gcd(unsigned a, unsigned b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
#include <stdio.h>
int main() {
int a, b;
for (;;) {
printf("\nInput positive fraction like 12/30: ");
if (scanf("%u/%u", &a, &b) != 2)
break;
unsigned g = gcd(a, b);
a /= g;
b /= g;
printf("Simplified Fraction is %u/%u", a, b);
}
return 0;
}
In addition to the logical or issue identified by others, you also have an infinite loop with your while condition. You don't need (or want) to loop with your current code. Try this
#include <stdio.h>
int main ()
{
int a, b;
printf ("\n\nInput integers for fraction:");
scanf (" %d%d", &a, &b);
while (a % 2 == 0 && b % 2 == 0)
{
a /= 2;
b /= 2;
}
while (a % 3 == 0 && b % 3 == 0)
{
a /= 3;
b /= 3;
}
while (a % 5 == 0 && b % 5 == 0)
{
a /= 5;
b /= 5;
}
while (a % 7 == 0 && b % 7 == 0)
{
a /= 7;
b /= 7;
}
printf ("Simplified Fraction is %d/%d", a, b);
}
Output(s) with your given input(s)
Input integers for fraction:2 4
Simplified Fraction is 1/2
Input integers for fraction:8 24
Simplified Fraction is 1/3