write a progam to display the factors of a given number and
disply how many prime numbers are there?

Answer Posted / kracekumar

to check a number is prime or not ,it is enough to check till square root(floor) of the number is divisible or not.
#include<stdio.h>
#include<math.h>
/*
WHILE COMPILING IN LINUX USE gcc -lm -o factor factor.c and execute ./factor
*/
void isprime(int factor)
{
/*This function will check the given factor of the no is prime or not if so it will print the number factor as (prime)*/
int inner_loop,sqrt_of_factor;
sqrt_of_factor=(int)sqrt((double)factor);//in sqrt function will take float or double argument
if(factor==2)
printf("(prime)");
else
{/*To find out the no is prime or not you need to find out square root and from 2 to square root of the no if the no is not divisble by the no it is called as prime no*/
for(inner_loop=2;inner_loop<=sqrt_of_factor;++inner_loop)
{
if(factor%inner_loop==0)//if it divisble it exit the looop
break;
}
if(inner_loop>sqrt_of_factor)
/*best part is this checking ,if the no(factor) is prime it should be exited the loop with the loop variable value greater than condition varibale value */
printf("(prime)");
}
}
void factor(int no)
{
int start,no_by_2=no/2;
/*no_by_2 is used because factor of a no will be in the range 1 to half the number */
printf("\n factors of %d are %d",no,1);
for(start=2;start<no_by_2;++start)
{
if(no%start==0)
{
printf("\t %d",start);
isprime(start);//checking the factor is prime or not
}
}
printf("\t %d",no);
isprime(no);
}
int main()
{
system("clear");//work only in LINUX
int no;
printf("\n enter the integer no for which factor is to be found:");
scanf("%d",&no);
factor(no);
printf("\n \n=================================================\n");
return 0;
}

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

FILE PROGRAMMING

1868


What is p in text message?

634


What do you know about the use of bit field?

699


 write a program that will open the file, count the number of occurences of each word in the the complete works of shakespeare.  You will then tabulate this information in another file.

1823


What are disadvantages of C language.

756






What is the difference between formatted&unformatted i/o functions?

713


write a programe to accept any two number and check the following condition using goto state ment.if a>b,print a & find whether it is even or odd and then print.and a

1541


What is a string?

754


Can a program have two main functions?

689


How to explain the final year project as a fresher please answer with sample project

584


what is use of malloc and calloc?

1483


How pointers are declared?

639


Explain the process of converting a Tree into a Binary Tree.

2232


What is the difference between constant pointer and constant variable?

842


write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...

1581