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

Answers were Sorted based on User's Feedback



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

Answer / sreejesh1987

//This code displays prime factors only

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,x,flag=0,d,count=0;
clrscr();
printf("\tPRIME CHECK\n");
printf("Enter a no:");
scanf("%d",&n);

for(j=2;j<n,n%j!=0;j++)
if(n-1==j)
{
printf("\n\t%d is a prime",n);

flag=1;
}

if(!flag)
{
printf("\nNumber %d is not a prime",n);
printf("\nIts factors are:\n\t");

x=2;
while(n!=1)
{

while(n%x==0)
{
n=n/x;
printf("%d ",x);
count++;
}
x++;
}
printf("\nNumber of factors:%d",count);
}
getch();
}

Is This Answer Correct ?    3 Yes 1 No

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

Answer / 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

More C Interview Questions

can u suggest me am in a confusion to choose whether to go to c programming or a software testing . am a graduate in B.sc(electronics).

1 Answers  


Can 'this' pointer by used in the constructor?

0 Answers  


What are the advantage of c language?

0 Answers  


Is it fine to write void main () or main () in c?

0 Answers  


When should you not use a type cast?

0 Answers  






given the piece of code int a[50]; int *pa; pa=a; to access the 6th element of the array which of the following is incorrect? a.*(a+5) b.a[5] c.pa[5] d.*(*pa + 5)

6 Answers   amu, TCS,


number 2 plssssss help !!....using array.. turbo c.. create a program that will accept a number and determine if it is a happy number or an unhappy number.. example: enter a number : 7 7*7=49 then 4 and 9 4*4 and 9*9== 16 + 18 gives you 97 then 9 and 7 9*9 and 7*7 == 81 + 49 gives you 130 then 1 and 3 1*1 and 3*3 == 1 + 9 gives you 10 1*1 gives you 1 sample output: 7= 49= 16+81= 97= 81+49=130 =1+9=10 =1 "7 is a happy number" . if the last number is 2 then the number being inputed is not a happy number.

3 Answers  


What is the best organizational structure?

0 Answers  


What is modeling?

0 Answers  


What are the 4 types of programming language?

0 Answers  


What is the sizeof () operator?

0 Answers  


What is the difference between volatile and const volatile?

0 Answers  


Categories