0
What's wrong with my code??
#include <stdio.h> int main() { int n, m, flag = 0, i = 0; scanf("%d", &n); int arr[n]; m = n + 1; while (m--) { if (n % m == 0) { flag++; arr[i] = m; i++; } } printf("%d\n", flag); for (i = 0; i < flag; i++) { printf("%d ", arr[i]); } return 0; } When I write same program in for loop instead of while, Its work perfectly. Anyone describes the problem.
3 Answers
+ 1
The error occurs when m reaches 0 and the code attempts to find the modulo of 0.
An easy fix is to use pre-decrement on m instead of post-decrement.
Change from
while (m--)
To
while (--m)
+ 2
Hi. I tested your code. At the "while", i deleted the "--" and added a new line for m--, below the if statement. It worked, meaning that I got some input and no error.
+ 2
hey maybe yo just need more practice but i'm sure you'll get it salaam alaikum