Hi i wrote this piece of code. It's ment to take in a user input and didive it by 2 to get all it's factors. Then it is ment to divide the factors by 1-100 to check it is a prime number then display the prime factors. Can someone please help me.
#include <stdio.h>
int collection(int a);
int factor (int b);
int main()
{
int a;
printf("Please enter a number: ");
scanf("%d",&a);
collection (a);
return 0;
}
int collection(int a)
{
int b,i;
for(i=1; i<=100; i++)
{
b=a/i;
factor (b);
//printf(" %lf \n",b);
if(a%i==0)
{
// printf("Display the factors %d %t %t %d\n",b,i);
}
}
return 0;
}
int factor(int b)
{
float k,c;
for(k=1; k<=100; k++)
{
c=b/k;
printf(" %d \n",c);
}
return 0;
}
Your friend Nazmul must be in the same computing class.
Let's look at what your program actually does.
It gets an integer "a" from the user.
For each integer i from 1 to 100, it divides a by i and rounds down to the nearest integer, and calls the result b.
Then, each time, it calls the routine, factor(b). This divides b by each number from 1 to 100 and displays ..... something.
Is that what you wanted to do?
Where is your routine for checking whether a number is prime? How does your program actually tell if a number is a factor or not? Or does it just blindly divide through regardless of whether it is dividing by a factor or not.
When writing a program like this, you need to think about what steps your program is going to need to undertake, before writing the code.
E.g:
for each factor of the number a, if the factor is prime, display the factor and divide the number by that factor and go again, until we have only 1 left.
Hope that helps.
Bill.