This blog is under construction

Saturday, 22 June 2013

C program to print numbers from 1 to n that are divisible by x and indivisible by y

Write a C program to print numbers from 1 to n that are divisible by x and  not divisible by y.


  #include <stdio.h>
  int main() {
        int n, x, y, i;
        printf("Enter the value for n:");
        scanf("%d", &n);
        printf("Enter the value for x:");
        scanf("%d", &x);
        printf("Enter the value of y :");
        scanf("%d", &y);

        for (i = 1; i <= n; i++) {
                /*
                 * print nos that are divisible by x
                 * and indivisible by y
                 */
                if (i % x == 0) {
                        if (i % y == 0) {
                                continue;
                        } else {
                                printf("%d ", i);
                        }
                }
        }
        printf("\n");
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value for n:100
  Enter the value for x:5
  Enter the value of y :3
  5 10 20 25 35 40 50 55 65 70 80 85 95 100 


No comments:

Post a Comment