Write a C program to print integers from 1 to n leaving values that are divisible by x.
int main() {
int i, n, x, count = 0;
printf("Enter the value for x:");
scanf("%d", &x);
printf("Enter the value for n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
/* skip values that are divisible by x */
if (i % x == 0)
continue;
/* print values thar are indivisible by x */
printf("%-3d ", i);
count++;
if (count % 10 == 0)
printf("\n");
}
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for x:7
Enter the value for n:100
1 2 3 4 5 6 8 9 10 11
12 13 15 16 17 18 19 20 22 23
24 25 26 27 29 30 31 32 33 34
36 37 38 39 40 41 43 44 45 46
47 48 50 51 52 53 54 55 57 58
59 60 61 62 64 65 66 67 68 69
71 72 73 74 75 76 78 79 80 81
82 83 85 86 87 88 89 90 92 93
94 95 96 97 99 100
Enter the value for x:7
Enter the value for n:100
1 2 3 4 5 6 8 9 10 11
12 13 15 16 17 18 19 20 22 23
24 25 26 27 29 30 31 32 33 34
36 37 38 39 40 41 43 44 45 46
47 48 50 51 52 53 54 55 57 58
59 60 61 62 64 65 66 67 68 69
71 72 73 74 75 76 78 79 80 81
82 83 85 86 87 88 89 90 92 93
94 95 96 97 99 100
No comments:
Post a Comment