I have a code that gets a number from input and then puts it into array, and then prints said array. But there is very strange side-effect, when I enter 13 digit number, my last digit is wrong. If I enter 15 or 16 digit number everything is fine. I can't understand what's happening! Here is the code:
numberDigits is either 12, 14 or 15, based on the number of digits in number.
int myNumber[numberDigits];
for (int i = 0; i <= numberDigits; i++)
{
myNumber[numberDigits - i] = number % 10;
number = number / 10;
}
printf("\n");
for (int i = 0; i <= numberDigits; i++)
{
printf("%i",myNumber[i]);
}
EDIT:
My number is initialized as long long int. Here is a check to verify number of digits:
if (number / 1000000000000 >= 1 && number / 1000000000000 < 10)
{
numberDigits = 12;
}
if (number / 100000000000000 >= 1 && number / 100000000000000 < 10)
{
numberDigits = 14;
}
if (number / 1000000000000000 >= 1 && number / 1000000000000000 < 10)
{
numberDigits = 15;
}
EDIT: Last update, Yes, I have updated my code and it works as expected with your suggestions, but I need to understand why my code doesn't work. I think that maybe array[12] actually holds 12 elements and not 13, and when I access element array[12] with it, I get some random value from memory, but why this doesn't happen with array[14] or array[15] with 15 and 16 digit numbers respectively. And why program does not crash as it should when you access array at wrong position. Original question below with old code that doesn't work.
EDIT: Here is whole listing for those interested.
#include <stdio.h>
#include <cs50.h>
int verify (long long int number);
int main(void)
{
printf("Please enter card number: \n");
long long int number = GetLongLong();
// AMEX uses 15 digits
if (number > 100000000000000 && ((number / 10000000000000 == 34) || (number / 10000000000000 == 37)))
{
printf("AMEX");
}
if (number > 1000000000000 && number < 10000000000000 && (number / 1000000000000 == 4))
{
printf("VISA");
}
if (number > 1000000000000000 && number < 9999999999999999 && (number / 1000000000000000 == 4))
{
printf("VISA");
}
if (number > 1000000000000000 && number < 9999999999999999 && (number / 100000000000000 == 51 || number / 100000000000000 == 52 || number / 100000000000000 == 53 || number / 100000000000000 == 54 || number / 100000000000000 == 55))
{
printf("MASTERCARD");
}
return verify(number);
}
int verify (long long int number)
{
int numberDigits;
if (number / 1000000000000 >= 1 && number / 1000000000000 < 10)
{
numberDigits = 12;
}
if (number / 100000000000000 >= 1 && number / 100000000000000 < 10)
{
numberDigits = 14;
}
if (number / 1000000000000000 >= 1 && number / 1000000000000000 < 10)
{
numberDigits = 15;
}
int myNumber[numberDigits];
for (int i = 0; i <= numberDigits; i++)
{
myNumber[numberDigits - i] = number % 10;
number = number / 10;
}
printf("\n");
for (int i = 0; i <= numberDigits; i++)
{
printf("%i",myNumber[i]);
}
return 0;
}
Your for loop should look like this:
for (int i = 0; i < numberDigits; i++)
Since indexes are zero-based, the last one will be numberDigits - 1. And so <= would go past the end.
I think it should be something like this:
for(int i = 0; i < numberDigits; ++i) printf("%d",myNumber[i]);
instead of:
for(int i = 0; i <= numberDigits; ++i) printf("%i",myNumber[i]);
And in the first loop:
myNumber[numberDigits - (i + 1)] = number % 10;
Also, you could use something like this to know the number of digits:
int digits(long long num) {
if(!num) return 1;
return log10(num) + 1;
}
Related
I need to make a program to identify whether the input number is a Smith number or not.
Here is my code:
#include <stdio.h>
void smith(int n) {
int num, sd = 0, sf = 0, i, t, c, j;
num = n;
while (num > 0) {
sd = sd + (num % 10); // Sum of digits of input number
num = num / 10;
}
num = n;
while (num > 1) // To calculate factors of the number
{
for (i = 1; i <= num; i++) {
if (num % i == 0) break;
}
c = 0;
t = i;
for (j = 1; j <= i; j++) // To check if the numbers are prime
{
if (i % j == 0) c++;
}
if (c == 2) {
while (i > 0) {
sf = sf + (i % 10);
i = i / 10;
}
}
num = num / t;
}
if (sd == sf) {
printf("Smith Number");
} else
printf("Not a Smith Number");
}
int main() {
int n;
printf("Enter a number");
scanf("%d", &n);
smith(n);
}
Every time I try to run the code, it just doesn't give an output.
It just takes an input and then probably goes into an infinite loop.
Because the value of t is always 1, num is always stay the same, while loop endlessly. You can fix it like this:
for (i = 2; i <= num; i++) {
if (num % i == 0) break;
}
In
for (i = 1; i <= num; i++) {
if (num % i == 0) break;
}
You always get 1 as factor(every number is divisible by 1) and the only factor you consider is one. As you divide by one, you never get your number smaller. You should start that loop at 2.
The problem is that you are introducing an infinite loop (a very elusive one). Look at these lines of code:
for (i = 1; i <= num; i++) {
if (num % i == 0) break;
}
You are testing if the num is divisible by 1 (which is true for every number), so it will always break, and i will be 1.
Later on, you divide num by this number until num is less than or equal to one, but dividing by one yields the same number, so the loop will never end. If the loop never ends, then your program will stall, hence the program never printing a result.
The solution is simple: start your factoring loop at 2, not 1. Like this:
for (i = 2; i <= num; i++) {
if (num % i == 0) break;
}
1 is a factor of every number, so it is not a useful factor.
/Write a program to determine the total number of prime numbers below 1000,000,000 have the sum of their digits equal to 14? Make sure the execution time is few seconds./
#include<stdio.h>
#include<math.h>
int main() {
int i, j, count = 0, temp = 0, n, ans = 0, tot = 0;
for (i = 1; i <= 1000000000; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0) {
n = i;
while (n != 0) {
temp = n % 10;
n = n / 10;
ans = ans + temp;
}
if (ans == 14) {
tot++;
printf("%d,", i);
}
ans = 0;
temp = 0;
}
count = 0;
}
// printf("%d:\n",tot);
return 0;
}
Two simply improvements (amongst other):
1: Rather than iterate to i/2, iterate to the square root of i - that is j*j <= i.** This is a huge speed-up.
2: Quit loop once a factor found.
// for(j=2;j<=i/2;j++) {
// if(i%j==0) {
// count++;
// }
//}
for(j=2;j<=i/j;j++) { // _much_ lower limit
if(i%j==0) {
count++;
break; // No need to find more factors: `i` is not a prime.
}
}
Functionality: Inside if(count==0), I'd expect ans == 0 before while(n!=0).
** Use j<=i/j to prevent overflow. A good compiler will see a nearby i%j and often perform both i/j, i%j for the time cost of one.
The digit-sum function could also use a early return like:
int dsum14(int n) {
int sum = 0;
for (; n; n /= 10)
if ((sum += n % 10) > 14)
return 0;
return sum == 14 ? 1 : 0;
}
But how to combine the (efficient) prime search and this sum condition?
int n, cnt = 0;
for (n = 3; n < 1000*1000*1000; n += 2)
if (n%3 && n%5 && dsum14(n) && n%7 && n%11 && n%13)
cnt++;
This gives 77469 in 1.5 seconds. With dsum() at either end of the logical chain it is almost double.
The && n%7 && n%11 && n%13 part would be replaced by a function using a list of primes up to about 32000 (square root of max).
...or you can optimize it to 0.1 seconds, by tweaking the digsum function.
There are "only" 575 three-digit numbers 000-999 with sum 14 or less. So I prepare them and combine three of them to get a 9-digit number. Generating them instead of filtering them.
The tail looks like:
920000021
920000201
920001011
920010011
920100011
920100101
920101001
921001001
931000001
total count: 22588
real 0m0.098s
user 0m0.100s
sys 0m0.002s
And the start:
59
149
167
239
257
293
347
383
419
Not 100% sure if it's correct, but the total count also seems reasonable.
It all relies on the given max of 1000 Mio. digsum_prime() uses it to build the candidate number from three (almost) equal parts.
Code:
#include <stdio.h>
#include <stdlib.h>
int parr[5000] = {3};
struct {
int tri, sum;
} ts[999];
void primarr(void) {
int maxn = 32000;
int i = 1;
for (int n = 5; n < maxn; n += 2)
for (int div = 3;; div += 2) {
if (!(n % div))
break;
if (div*div > n) {
parr[i++] = n;
break;
}
}
}
int isprime(int n) {
for(int i = 0;; i++) {
if (!(n % parr[i]))
return 0;
if (parr[i]*parr[i] > n)
return 1;
}
}
int dsum(int n) {
int sum = 0;
for (; n; n /= 10)
sum += n % 10;
return sum;
}
int tsarr(void) {
int i = 0;
for (int n = 0; n < 1000; n++) {
int digsum = dsum(n);
if (digsum <= 14) {
ts[i].tri = n;
ts[i].sum = digsum;
i++;
}
}
return i;
}
int digsum_prime() {
int cnt = 0;
int tslen = tsarr();
printf("tslen: %d\n", tslen);
int high, mid, low;
int sum, num;
for (high = 0; high < tslen; high++) {
if(ts[high].sum > 13)
continue;
for (mid = 0; mid < tslen; mid++) {
if(ts[mid].sum + ts[high].sum > 13)
continue;
sum = ts[mid].sum + ts[high].sum;
for (low = 0; low < tslen; low++)
if (ts[low].tri % 2)
if(ts[low].sum + sum == 14) {
num = ts[high].tri * 1000*1000
+ ts[mid] .tri * 1000
+ ts[low] .tri;
if (isprime(num)) {
cnt++;
printf("%d\n", num);
}
}
}
}
return cnt;
}
int main(void) {
primarr();
printf("total count: %d\n", digsum_prime());
}
Changing 13-13-14 to 3-3-4 (but same preparation part) gives an overview - in 0.005 s!
tslen: 575
13
31
103
211
1021
1201
2011
3001
10111
20011
20101
21001
100003
102001
1000003
1011001
1020001
1100101
2100001
10010101
10100011
20001001
30000001
101001001
200001001
total count: 25
real 0m0.005s
user 0m0.005s
sys 0m0.000s
Make sure the execution time is few seconds.
oops
But the limits of OP are well chosen: a naive approach takes several seconds.
BTW I know that's not the most efficient way to do it but if I wanted to do it like I did, what did I do incorrectly? The task was: Given a five digit integer, print the sum of its digits.
Constraint: 10000 <= n <= 99999
Sample Input: 10564
Sample Output: 16
My code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, sum;
int remainder_array[4] = { n % 1000; n % 100, n % 10, n };
int digits_array[4];
scanf("%d", &n);
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d", n / 1000);
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3];
printf("%d", sum);
}
return 0;
}
your algorithm is far too complicated, It can be done much easier way without arrays.
int sumof5LSD(int x)
{
int result = 0;
for(int digit = 1; digit <=5; digit++)
{
result += abs(x % 10);
x /= 10;
}
return result;
}
int main(void)
{
printf("SUM: %d", sumof5LSD(10564));
}
https://godbolt.org/z/bcdM8P
or if you are not allowed to use loops:
int sumof5LSD(int x)
{
int result = 0;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
return result;
}
It is good to use functions to perform similar tasks.
Version with scanf
int main(void)
{
int n;
scanf("%d", &n);
printf("SUM of 5 digits of %d = %d", n, sumof5LSD(n));
}
it will also calculate the sum of 5 digits of the negative number
There are multiple issues in your code:
you initialize remainder_array from the value of n before reading the value of n.
the initializer is incorrect: the separator should be ,, not ;.
you start the statement inside the if body with else, which is a syntax error.
the test if (remainder_array[0] = 0) sets remainder_array[0] to 0 and evaluates to false.
remainder_array is misspelt a reminder_array
Your approach is fine, but you should intialize remainder_array with the actual remainders (5 of them), after reading and checking n:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int remainder_array[5] = { n / 10000, n / 1000 % 10, n / 100 % 10, n / 10 % 10, n % 10 };
int sum = remainder_array[0] + remainder_array[1] + remainder_array[2] +
remainder_array[3] + remainder_array[4];
printf("%d\n", sum);
}
return 0;
}
Note that you don't actually need this remainder_array, you could just write:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int sum = n / 10000 + n / 1000 % 10 + n / 100 % 10 + n / 10 % 10 + n % 10;
printf("%d\n", sum);
}
return 0;
}
Here is a more readable and more generic version:
#include <stdio.h>
int main() {
int n;
if (scanf("%d", &n) == 1 && 10000 <= n && n <= 99999) {
int sum = 0;
while (n >= 10) {
sum += n % 10;
n = n / 10;
}
sum += n;
printf("%d\n", sum);
}
return 0;
}
Apart from the errors and modifications what #P_J_ and #chqrlie has mentioned I have noticed some major logical errors and misunderstanding of a basic concept in your code(assuming that first else if is replaced by if)
you have given else if statement repeatedly, now what this does is that whenever the first condition it encounters is true it executes the block inside and exits from the branch i.e remaining statements after else if is not executed, this might cause a major logical error in your program.
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d", n / 1000);
}
Now in this picture if you notice the output you can see that the digits_array[1-3] are 0 this is because of the reason mentioned above(it is zero cause I have initialized it beforehand) hence the sum is zero.
And the second logical error is that you are dividing a 5 digit number by 1000 this will give you thousand's place i.e in example 10546 this step will result in 10 this is wrong, take another example 12233 not the sum for the digits should result in 11 but you will get 20 because when you divide 12233 by 1000 the first value of digits_array (digit_array[0]) is 12 so the entire output goes wrong. so to correct this divide it by 10000 (only for this program statement as it has 5 digits).
But still, if you wish to continue without changing the algorithm then this code should work fine.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, sum;
int digits_array[5]={0}; //5 because it is a five digit number.
scanf("%d", &n);
int remainder_array[] = { n % 1000, n % 100, n % 10, n };//This is still a useless array this is kept so as to enter if.
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
if (remainder_array[0] != 0) {
digits_array[0] = (n) / 10000;//subtracting n with contents of remainder_array added to the complexity of the algorithm so took out the statement.
n =(n)%10000;
} if (remainder_array[1] != 0) {
digits_array[1] = (n) / 1000;
n =(n)%1000;
} if (remainder_array[2] != 0) {
digits_array[2] = (n) / 100;
n =(n)%100;
} if (remainder_array[3] != 0) {
digits_array[3] = (n)/10;
n=(n)%10;
}
digits_array[4]=n;//the last element of the number
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3]+digits_array[4];
printf("%d",sum);
return 0;
}
Note: This program is not the best way and some changes are made refer to other answers for a more effective code and get the basics right before going to nested if condition.
Enjoy coding
I'm very new to C language much less programming in general and am working through the cs50 edx course. I encountered this problem in week 1 where I'm supposed to validate credit cards given their characteristics (they're in the comments).
After hours worth of research and trials, I managed to solve it though I think it's way too long and I am 90% sure it can be shortened with loops. I added the long version that works and an iteration of it which uses loops that I just can't get to work. Any advice would help and be very much appreciated!
#include <stdio.h>
#include <cs50.h>
int main(void)
{
//user input for credit card number
long creditCard = get_long("Credit Card: ");
//initialize array for finding digits
int digit[16];
int oddDigits[8];
//separate all digits as variables in array digits[]
digit[0] = ((creditCard / 1000000000000000) % 10);
digit[1] = ((creditCard / 100000000000000) % 10);
digit[2] = ((creditCard / 10000000000000) % 10);
digit[3] = ((creditCard / 1000000000000) % 10);
digit[4] = ((creditCard / 100000000000) % 10);
digit[5] = ((creditCard / 10000000000) % 10);
digit[6] = ((creditCard / 1000000000) % 10);
digit[7] = ((creditCard / 100000000) % 10);
digit[8] = ((creditCard / 10000000) % 10);
digit[9] = ((creditCard / 1000000) % 10);
digit[10] = ((creditCard / 100000) % 10);
digit[11] = ((creditCard / 10000) % 10);
digit[12] = ((creditCard / 1000) % 10);
digit[13] = ((creditCard / 100) % 10);
digit[14] = ((creditCard / 10) % 10);
digit[15] = (creditCard % 10);
//double the value of every other digit as oddDigits[]
oddDigits[0] = digit[0] * 2;
oddDigits[1] = digit[2] * 2;
oddDigits[2] = digit[4] * 2;
oddDigits[3] = digit[6] * 2;
oddDigits[4] = digit[8] * 2;
oddDigits[5] = digit[10] * 2;
oddDigits[6] = digit[12] * 2;
oddDigits[7] = digit[14] * 2;
//the first part in finding the sum of every other digit
for (int o = 0; o < 8; o++) {
if (oddDigits[o] > 9) {
oddDigits[o] -= 9;
}
}
//part 2 of finding the sum of every other digit
int sum = 0;
for (int s = 0; s < 8; s++) {
sum += oddDigits[s];
}
//sum of everything else
for (int s2 = 1; s2 < 16; s2 += 2) {
sum += digit[s2];
}
//check if last digit is 0
int check = sum % 10;
//last part of validation
if (check != 0)
printf("INVALID\n");
//type of card
else {
if (digit[0] == 0 && digit[1] == 3 && (digit[2] == 4 || digit[2] == 7)) //15 digits starting with either 34 or 37
{
printf("AMAX\n");
}
else if (digit[0] == 5 && (digit[1] == 1 || digit[1] == 2 || digit[1] == 3 || digit[1] == 4 || digit[1] == 5)) //16 digits starting with either 51 - 54
{
printf("MASTERCARD\n");
}
else if (digit[0] == 4) //16 digits starting with 4
{
printf("VISA\n");
}
else if (digit[0] == 0 && digit[1] == 0 && digit[2] == 0 && digit[3] == 4) //13 digits starting with 4
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
The only part I changed is where I try to get every digit of the number using for loops and the part where I try to double every other digit... I did a test where after the loop it will print all the digits separately and it always prints 1 no matter what and I'm wondering what I'm doing wrong here...
//initialize array for finding digits
int digit[16];
int oddDigits[8];
//separate all digits as variables in array digits[]
for (int d = 0; d < 16; d++) {
for (long i = 1000000000000000; i > 9; i /= 10) {
digit[d] = ((creditCard / i) % 10);
}
}
//double the value of every other digit as oddDigits[]
for (int n = 0; n < 8; n++) {
for (int t = 0; t < 15; t += 2) {
oddDigits[n] = digit[t]*2;
}
}
While you are separating all digits as variables in array digits[], the for loop runs till.
digit[14] = ((creditCard / 10) % 10);
to fix this change it as
for (long i = 1000000000000000; i > 0; i /= 10)
{
digit[d] = ((creditCard / i) % 10);
}
ie change 9 to 0. in this way, the last iteration of the loop will run
digit[15] = ((creditCard/1) % 10);
which is same as
digit[15] = (creditCard % 10);
now see, the outer loop is running 16 times. the inner loop is also running 16 times and the inner loop is running only once per iteration of the outer loop. So one of the loops is redundant. remove the outer loop. keep the d variable as 0 and increment it by 1 after dividing the digit. i.e.
int d = 0;
//separate all digits as variables in array digits[]
for (long i = 1000000000000000; i > 9; i /= 10)
{
digit[d] = ((creditCard / i) % 10);
d++;
}
You can also do it like this
long i = 1000000000000000;
for (int d = 0; d < 16; d++)
{
digit[d] = ((creditCard / i) % 10);
i /= 10;
}
Either one will work.
Now this part //double the value of every other digit as oddDigits[]
It can also be done with 1 loop. In your code, the variable t is just double of the variable n in each step. So, you don't need the t variable too. (just replace t by 2*n)
//double the value of every other digit as oddDigits[]
for (int n = 0; n < 8; n++)
{
oddDigits[n] = digit[2*n]*2;
}
I completed a challenge in HackerEarth using C but the solution is only partially accepted. I tried to change the data type also but no success.
Question is:
You are given an array A of size N that contains integers. Here, N is an even number. You are required to perform the following operations:
Divide the array of numbers in two equal halves
Note: Here, two equal parts of a test case are created by dividing the array into two equal parts.
Take the first digit of the numbers that are available in the first half of the array (first 50% of the test case)
Take the last digit of the numbers that are available in the second half of the array (second 50% of the test case)
Generate a number by using the digits that have been selected in the above steps
Your task is to determine whether the newly-generated number is divisible by 11.
My solution is:
#include <stdio.h>
#include <math.h>
int main(){
int N;
scanf("%d",&N);
int A[N];
for(int i = 0;i < N;i++)
scanf("%d",&A[i]);
long int sum = 0;
for(int i = 0;i < N/2;i++){
int digits =(int)log10(A[i]);
int first_digit = (int)(A[i] / pow(10,digits));
sum = (sum*10) + first_digit;
}
for(int i = N/2;i < N;i++){
int last_digit = A[i] % 10;
sum = (sum*10)+last_digit;
}
sum % 11 == 0? printf("OUI"):printf("NON");
return 0;
}
The problem says that N will be upto 100,000.
Dealing with such many digits, the calculation of sum will cause overflow.
You should divide sum by 11 and take modulo after each update of sum.
You shouldn't use floating-point numbers or built-in integer types for this kind of problems, because their precisions wouldn't be enough. An integer number is divisible by 11 if the alternating sum of its decimal digits is divisible by 11. So a simple solution would be:
#include <stdio.h>
#include <string.h>
int main (void)
{
int sum = 0, sign = 1;
int n, i;
scanf("%d", &n);
for (i = 0; i < n; ++i) {
char num[100];
scanf("%s", num);
sum += sign * (num[i < n / 2 ? 0 : strlen(num) - 1] - '0');
sign = -sign;
}
puts(sum % 11 == 0 ? "OUI" : "NON");
return 0;
}
Note: Error checking and input validation is omitted for brevity.
100 pecent accepted hackerearth Divisible problem:
#include<stdio.h>
int main() {
long int N;
scanf("%ld", &N);
long int a[N];
int m = 0, n = 0, i;
for (i = 0; i < N; i++)
scanf("%ld", &a[i]);
for (i = 0; i < N / 2; i++) {
if (a[i] >= 100000)
a[i] = a[i] / 100000;
else if (a[i] >= 10000)
a[i] = a[i] / 10000;
else if (a[i] >= 1000)
a[i] = a[i] / 1000;
else if (a[i] >= 100)
a[i] = a[i] / 100;
else if (a[i] >= 10)
a[i] = a[i] / 10;
else
a[i] = a[i];
if (i % 2 == 0)
m += a[i];
else
n += a[i];
}
for (i = N / 2; i < N; i++) {
if (i % 2 == 0)
m += (a[i] % 10);
else
n += (a[i] % 10);
}
if ((abs(m - n)) == 0 || ((abs(m - n)) % 11) == 0)
printf("OUI");
else
printf("NON");
}
function divisibilty(n, arr) {
return arr[n - 1] % 10 == 0 ? "Yes" : "No";
}
console.log(divisibilty(5, [185, 125, 165, 211, 814])); // No
console.log(divisibilty(2, [98, 70])); // Yes