What is wrong with my c program? prime factorization?
-
my program works well but the only problem is in never outputs hte number 2. when i enter the integer 18 its supposed to spit out the prime factorization which is 2 3 3 but it gives me 3 3. whats wrong... #include<stdio.h> int main() { while(3){ printf("\nEnter an integer: "); int n, x, y, z, k; k = 2; x = 2; while(2){ scanf("%d", &n); if (n <= 1) return (0); if (n > 1) break; } printf("The prime factorization of %d is ",n); while(1){ if (n % k == 0) (n = (n/k)); if (n % k != 0) (k = k + 1); if(k > n) break; if (n % k == 0) printf("%d ",k); } } printf("\n"); return 0; }
-
Answer:
Try this though it is crude #include<stdio.h> #include<stdlib.h> int main() { int i,n,rn,r=1,j,k; printf("enter an integer\n"); scanf("%d", &n); rn=n; for(j=2;(r!=rn&&n>1)&&j<n;j++) { for(k=2;k<j;k++) { if(j%k==0)break; } if(j==k){ while(n%j==0){ printf("%d ", j); r=r*j; n=n/j; } } } system("PAUSE"); return 0; }
Nana at Yahoo! Answers Visit the source
Other answers
my program works well but the only problem is in never outputs hte number 2. when i enter the integer 18 its supposed to spit out the prime factorization which is 2 3 3 but it gives me 3 3. whats wrong... #include<stdio.h> int main() { while(3){ printf("\nEnter an integer: "); int n, x, y, z, k; k = 2; x = 2; while(2){ scanf("%d", &n); if (n <= 1) return (0); if (n > 1) break; } printf("The prime factorization of %d is ",n); while(1){ if (n % k == 0) (n = (n/k)); if (n % k != 0) (k = k + 1); if(k > n) break; if (n % k == 0) printf("%d ",k); } } printf("\n"); return 0; }
Jahangir
Related Q & A:
- How Can I use .net dll in C program?Best solution by Stack Overflow
- What is the difference between C# and C#.NET?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- What does the R.F.C.F mean on the back of spain's jersey?Best solution by ChaCha
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.