If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C language, you can use a for loop to find all the divisors of a given number. You can start the loop at 1 and increment it until it reaches the number itself. Within the loop, you can use the modulus operator (%), which returns the remainder when one number is divided by another, to check if the current loop variable is a divisor of the number. If the remainder is 0, then the current loop variable is a divisor and can be printed or stored in an array.
Here's an example of C code that finds all the divisors of a given number:
Copy code
#include <stdio.h>
int main() {
int n, i;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("All divisors of %d are: ", n);
for(i = 1; i <= n; ++i) {
if (n % i == 0) {
printf("%d ", i);
}
}
return 0;
}
This code prompts the user to enter a positive integer, and then uses a for loop to iterate through all integers from 1 to the entered number. It checks if the current loop variable is a divisor of the entered number by using the modulus operator. If it is, it prints the current loop variable as a divisor.
Comments: 0