Hi can someone help me with a program in the C language? I need to be able to: 1
ID: 3742326 • Letter: H
Question
Hi can someone help me with a program in the C language?
I need to be able to:
1) take user input asking for an integer, then returns integer
2) gives error message if it's a character or string and then ask for another integer
3) use a for loop to generate and print a list of factors of that number
4) printing and factoring must in separate functions, so the factoring function should be kinda like this: int factorFunction (int,int);
Sorry, I'm new to C programming and I just need some help with this so that I can work on the more complicated stuff.
Thanks!
Explanation / Answer
#include <stdio.h> /* printf, scanf */
#include <stdlib.h> /* atoi , atoi converts the string into integer if it is integer else return 0 for char string */
int factorFunction(int*,int);
void print_fact(int* , int );
int main ()
{
int num,k=1,num_fact;
int factors[101]; /list of factors of that number/
while(k>0)
{
char buffer[256];
printf ("Enter a number: ");
scanf("%s",buffer);
num = atoi (buffer);
if(num!=0)
{
printf("%d is a integer ",num);
k=-1;
}
else
{
printf("Input is not a integer ");
}
}
num_fact=factorFunction(factors,x); //factorizing function including loop
print_fact(factors,num_fact);
return 0;
}
int factorFunction(int *factors,int x)
{
int i,j=0;
unsigned int n = sizeof(factors)/sizeof(factors[0]); //size of factor array
for(i=1;i<=x;i++)
{
if(x%i==0)
{
factors[j++]=i;
}
}
return j;
}
void print_fact(int *factors, int num_fact)
{
int i;
printf("FACOTRS ARE :--> ");
for (i=0; i<num_fact; i++)
printf("%d ", factors[i]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.