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

  • Thread starter Thread starter Md. Abde Mannaf
  • Start date Start date
  • Tags Tags
    Integer
Click For Summary

Discussion Overview

The discussion revolves around writing a C program that takes an integer input, finds all its divisors, checks if the number is prime, and counts the number of divisors. The scope includes programming challenges and code optimization.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a C program that calculates divisors and checks for primality but seeks guidance on combining the functionalities into a single program.
  • Another participant asks about the specific problems being faced with the code and suggests using code formatting for clarity.
  • A different participant comments that checking for factors can be optimized by only checking up to the square root of the number, suggesting the use of the ceiling function for non-integer results.
  • A follow-up post reiterates the request for combining the two programs and asks how to print the number of divisors.

Areas of Agreement / Disagreement

Participants have not reached a consensus on how to effectively combine the programs or on the specific issues being encountered. Multiple viewpoints on optimization and code structure are present.

Contextual Notes

There are unresolved questions regarding the integration of the two programs and the method for counting divisors. The discussion also highlights potential limitations in the initial code provided by the first participant.

Who May Find This Useful

Individuals interested in programming in C, particularly those looking to solve problems related to number theory, divisors, and prime checking.

Md. Abde Mannaf
Messages
20
Reaction score
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
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
 
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));
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
7
Views
2K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
4
Views
3K
Replies
47
Views
5K