Take integer and find divisors,prime? & how many divisors

  • Thread starter Md. Abde Mannaf
  • Start date
  • Tags
    Integer
In summary, the conversation discusses writing a C program that takes an integer and finds all divisors/factors, determines if it is prime or not, and prints the number of divisors. The program uses two separate code snippets, with the first one calculating divisors and the second one checking for prime numbers. The individual is seeking help in combining the two programs and also mentions the use of code tags for readability. It is also suggested to use the square root of the number as the maximum check for prime numbers.
  • #1
Md. Abde Mannaf
20
1
write a c programs Take integer and find all divisors/Factors , print is it prime or not? and how many divisors contain print it?
how to solve in one program and how many divisors/Factors
contains

my code:
C:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int number,i;
    printf("This program calculate Divisors\n ");
    printf("enter number= ");
    scanf("%d",&number);
    printf("Divisors are= ");
    for(i=1;i<=number;i++)
{
    if (number%i==0 )
        {
            printf("%4d",i);
        }
}
    getch();
    return 0;
}

and

#include<stdio.h>

main()
{
int n, c = 2;

printf("Enter a number to check if it is prime\n");
scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);

return 0;
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
What are the problems you are having?
Also in the future when posting code please use the
Code:
 ['code'] ['/code'] (without the ') tags

it makes the code easier to read
 
  • #3
Just a comment: You do not have to check further than the square root of n. But since that probably is not an integer use:
Code:
int maxcheck = ceil(sqrt(n));
 
  • #4
cpscdave said:
What are the problems you are having?
Also in the future when posting code please use the
Code:
 ['code'] ['/code'] (without the ') tags

it makes the code easier to read
how to combine two program in one & how many number of divisor ?? print in program.
 

Related to Take integer and find divisors,prime? & how many divisors

1. How do I take an integer and find its divisors?

To find the divisors of an integer, you can use a simple loop that checks for all numbers from 1 to the given integer. If the remainder of the integer divided by the current number is 0, then that number is a divisor. You can also use the square root of the integer as the upper limit of the loop for more efficient code.

2. What is the difference between a divisor and a prime number?

A divisor is a number that evenly divides into another number without leaving a remainder. A prime number is a number that is only divisible by 1 and itself. In other words, a prime number has exactly two divisors, whereas a non-prime number has more than two divisors.

3. How can I determine how many divisors an integer has?

The number of divisors an integer has can be found by counting the number of times the integer can be divided by numbers from 1 to the integer itself. However, for larger integers, this method can be time-consuming. A more efficient method is to use the prime factorization of the integer and apply a formula to calculate the number of divisors.

4. What is the significance of finding the divisors and prime numbers of an integer?

Finding the divisors and prime numbers of an integer can have various applications in mathematics and computer science. It can be used in number theory, cryptography, and prime factorization algorithms. It can also be helpful in determining the factors of a number and solving problems related to factors and multiples.

5. Can an integer have an infinite number of divisors?

No, an integer can only have a finite number of divisors. This is because an integer can only be divided by numbers up to its own value. For example, the integer 10 can only have a maximum of 10 divisors, which are 1, 2, 5, and 10. Furthermore, prime numbers only have two divisors, so they cannot have an infinite number of divisors.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
743
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
905
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top