This blog is under construction

Monday, 10 June 2013

C program to print first n positive integers that are divisible by x

Write a C program to print first N positive integers that are divisible by X.



  /* C program to print first N positive integers that are divisible by X */
  #include <stdio.h>
  int main() {
        int i = 1, count = 0, n, x;
        printf("Enter the value for n:");
        scanf("%d", &n);
        printf("Enter the value for x:");
        scanf("%d", &x);
        while (1) {
                if (i % x == 0) {
                        /* nos divisible by x */
                        printf("%d ", i);
                        count++;
                }

                if (count == n)
                        break;
                i++;
        }
        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:10
  Enter the value for x:8
  8 16 24 32 40 48 56 64 72 80 



No comments:

Post a Comment