Testing if a number goes evenly into 6? [duplicate] - c

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");}
}
}

Related

Regarding if else statements in C language

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.

C program to print all the prime numbers from 1-300

I have written this code for C to print prime numbers from 1-300
GNU GCC Compiler shows the following error:
error: invalid operands to binary % (have ‘double’ and ‘int’)
if (sqrt(num) == 0 || sqrt(num) % 2 != 0)
My code is this:
#include <stdio.h>
#include <math.h>
int main() {
/*Created by Suvid Singhal Date:- January 2, 2017*/
int num;
printf("Welcome to 1-300 prime numbers C Program!!!");
for (num = 0; num <= 300; num++) {
if (sqrt(num) == 0 || sqrt(num) % 2 != 0) {
printf("%d\n", num);
} else {
continue;
}
}
return 0;
}
You have lots of thing going wrong in your answer.
Let's check them one by one.
Your idea about prime number is not clear. A prime number has no divisior except the number and 1. (1 is not a prime number).
How to check if a number is prime or not?
You can check all the `numbers <=square root of (x). So you need to check if someone (except 1 ) divides the number. If yes then it is not prime else it is.
Implementation Details
int check= 0;
for(int i=2;i<=(int) sqrt(x);i+=1)
if(n%i == 0)
{
check = 1;
n is non-prime;
break; // no need to check
}
if(check == 0)
n is prime
sqrt() return double and ...
Modulous can be applied over an int so you have to cast it to that.
If you want to avoid it all
for(int i=2;i*i<=x;i++)
...
For you to study
Learn about sieve method to get the primes. For 300 numbers probably it doesn't matter what you use but in case it is 10000000 then you will definitely want to give it a read.
% operator is for integers. You may want to use fmod() or an integer cast.
if ( sqrt( num ) == 0 || fmod( sqrt( num ), 2 ) != 0 )
or
if ( sqrt( num ) == 0 || (int) sqrt( num ) % 2 != 0 )
This should fix your compilation errors.
ps: If you are looking for an awesome way to generate prime numbers, see Sieve of Erastosthenes
In the code sample you have given, it is not quite clear how you intend to determine whether a number is prime or not. You have the following condition if (sqrt(num) == 0 || sqrt(num) % 2 != 0) to apparently determine a number as prime. But this condition is syntactically invalid. As others have pointed out, you can't do modulo operation on a non-int number as returned by the sqrt function. What value, do you think, is returned by the operation sqrt(num) % 2 and what has it do with a number being prime? Your usage of continue has also no importance. Maybe you are a little confused between break and continue. I am giving you a sample program. Try to learn what your mistakes are from this:
int isPrime = 1;
for(int i=2;i<=300;i++) {
isPrime = 1;
for(int j=2; j!=i && j <= ceil(sqrt(i));j++) {
if(i%j == 0) {
//Not a prime number
isPrime = 0;
break;
}
}
if(isPrime) {
printf("%d\n", i);
}
}
Changing
if( sqrt(num) == 0 || sqrt(num) % 2!=0 )
to
if( sqrt(num) == 0 || (int)sqrt(num) % 2!=0 )
Should fix compilation errors. See error: invalid operands to binary % when taking modulus of float for more info.

scanf does not ask for input

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.

Given a positive integer, tell whether it is equal to the sum of four consecutive integers

The problem:
Given a positive integer, tell whether it is equal to the sum of four consecutive integers.
Input Format:
Input consists of a single positive integer,
Output Format:
Output consists of a string which is either "yes" or "no". Print yes when the given number is the sum of four consecutive integers and print no otherwise.
Example:
In this example, 2 = -1 + 0 + 1 + 2, and 82 = 19 + 20 + 21 + 22. 5 and 41 are not of the correct
form.
Sample Input 1:
2
Sample Output 1:
yes
Sample Input 2:
41
Sample Output 2:
no
My program is not being accepted and also for some values my result is being repeated, can anyone help me?
#include <stdio.h>
int main()
{
int num,sum1=0,index,sum2=0,Soln=0;
int temp,i;
scanf("%d",&num);
if(num <= 0 )
{
printf("Wrong Input\n");
return 0;
}
for(index=1;;index++)
{
sum1=sum1+index;
if(sum1>=num)
break;
}
if(sum1 == num )
{
if(num == index)
{
printf("yes");
return 0;
}
Soln++;
printf("no" );
for(temp=1;temp<=index;temp++)
printf("no");
}
for(i=1;i<index;i++)
{
sum2+=i;
if((sum1-sum2) == num )
{
if((i+1) == index)
{
//printf("yes");
break;
}
Soln++;
printf("no" );
for(temp= i+1;temp<=index;temp++)
printf("no");
break;
}
}
if( Soln == 0 && num >= 9 && num/2 == (num-1)/2 ) //--> if U don't want more than one ,soln use this
//if( num >= 9 && num/2 == (num-1)/2 ) //--> if U want more than one soln, use this
{
Soln++;
printf("no");
}
if(Soln==0)
{
printf("yes");
}
return 0 ;
}
Take the four consecutive integers k, k+1, k+2, and k+3 and sum them up. This yields 4k + 6. If an integer n is the sum of four consecutive integers, it means that n = 4k + 6 for some integer k. Equivalently, this means that n - 6 = 4k for some integer k, or equivalently that (n - 6) mod 4 = 0. That might help you significantly simplify your program.

'if' statement in C not executing even though conditions are met

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.

Resources