Input

The input begins with two positive integers n k (n, k<=10^7). The next n lines of input contain one positive integer ti, not greater than 109, each.
Output

Write a single integer to output, denoting how many integers ti are divisible by k.
my code is.........

#include<stdio.h>
     
    
    int main()
    {
    int t,i=1,j=0,k;
  int n;
  
    scanf("%d%d", &t,&k);
  if(t>0&&k>0)
  {
    while(i<=t){
    scanf("%d",&n);
   
    if(n%k==0)
    j++;
    i++;
}
    printf("%d",j);
}
    
    return 0;
    }

Whats wrong?

Dani AI

Generated

The wording "how many integers ti are divisible by k" should be read literally (count ti such that ti % k == 0). correctly flagged the ambiguity; if the intent were the opposite it would be k % ti == 0 — clarify that if this is from a judged problem. asked for an example; a quick sample is useful when testing (e.g. n=3, k=2, inputs 4 7 6 -> answer 2).

Common reasons the posted program from can behave incorrectly:

  • Variable-name confusion and unchecked scanf return values can hide input errors.
  • Division by zero if k == 0 (even if constraints say positive, defensive checks help).
  • I/O performance: reading up to 10^7 integers with plain scanf in a loop often times out on online judges.
  • On some platforms, use of non-32-bit ints could cause overflow; ti up to 10^9 fits a 32-bit signed int but still merits attention.

A robust pattern in C is: read n and k, check k != 0, then loop n times reading each ti and test divisibility. For large n prefer a fast integer parser (getchar_unlocked or buffered fread) and keep the counter in a 64-bit type if n can be large. Example fast-reader approach:

#include <stdio.h>

static inline int fast_int(void) {
    int c = getchar_unlocked(), x = 0;
    while (c <= ' ') c = getchar_unlocked();
    for (; c > ' '; c = getchar_unlocked()) x = x*10 + (c - '0');
    return x;
}

int main(void) {
    int n = fast_int();
    int k = fast_int();
    long long count = 0;
    for (int i = 0; i < n; ++i) {
        int t = fast_int();
        if (k != 0 && t % k == 0) ++count;
    }
    printf("%lld\n", count);
    return 0;
}

Notes: replace getchar_unlocked with a portable alternative (fread-based parser) if compiling on Windows. Always test with small examples and with edge values (k==1, k larger than all ti, k==0 if unspecified).

Recommended Answers

All 2 Replies

will u please give me exampl and its expected output i am not getting your problem

Those requirements are odd. Given the ranges of ti and k , I would expect to check if ti evenly divides k , not if ti is divisible by k . Any k larger than 109 in the latter case is completely predictable and need not be checked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.