Prime factorization, changing output. - c

Hello guys i have this code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int num,i,j;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
for (i=2;i<=num;i++){
if (num%i==0){
a=1;
if (i==num){
printf(" %ld ",num);
} else {
printf(" %ld *",i);
num/=i;
}
}
}
if (a==0){
printf("%ld ",num);
}
return 0;
}
so let's say i input 40,
it gives me
The Prime Factorization: 2 * 4 * 5
this is correct but, how could I make it output the "2 * 4 * 5"
as "2 ^ 3 * 5"?

Since a prime can appear more than once in the factorization you can't just move on to the next candidate without first testing the current prime until the number is no longer divisble by it.
And to get the nice printout you're after, you can keep a count variable as shown below:
#include <stdio.h>
int main(void) {
unsigned int num,i,count;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
i = 2;
while(num>1){
if (num%i==0){
a=1;
count = 1;
num /= i;
// Exhaust each prime fully:
while (num%i==0) {
count++;
num /= i;
}
printf("%ld",i);
if (count > 1) {
printf(" ^ %ld", count);
}
if (num > 1) {
printf(" * ");
}
}
i++;
}
if (a==0){
printf("%ld ",num);
}
return 0;
}

something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,i,j,count=0;
int a=0;
printf("Please input a number: ");
scanf("%d",&num);
printf("The Prime Factorization: ");
for (i=2;i<=num;i++){
count=0;
a=0;
while(num%i==0){
a=1;
++count;
num/=i;
}
if(a==1)
{
printf("%d ^ %d *",i,count);
}
}
if (a==0){
printf("%ld ",num);
}
return 0;
}

Related

Letting user try inputting again after entering incorrect option

So I have some code that generates a set of random numbers. Afterwards, it asks the user if they would like to produce another set of random numbers. The problem I'm having is that I want the user to only input y for yes or n for no, and if they don't input those to options, tell them that is an invalid input and to try again. I cannot figure out how to do this. Here is the code:
// C program for generating a
// random number in a given range.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size:\n");
scanf("%d", &lower);
printf("\nMaximum number size:\n");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated:\n");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
scanf(" %c",&c);
printf("\n");
if (c=='n'){
exit(0);
}
do {
printf("Please enter valid input!\n");
scanf(" %c", &c);
if (c=='n'){
exit(0);
}
} while (c!='y'||c!='n');
} while(c=='y');
return 0;
}
Any help would be appreciated.
This answers your question, but on a side note, I usually like putting the conditional in the first question so if the user enters zero then the app will quit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Generates and prints 'count' random
// numbers in range [lower, upper].
void printRandoms(int lower, int upper,
int count) {
int i;
printf("\nGenerated Numbers:\n");
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;
printf("%d ", num);
}
}
int main() {
int lower, upper, count;
char c='y';
do {
printf("Minimum number size: ");
scanf("%d", &lower);
printf("\nMaximum number size: ");
scanf("%d", &upper);
printf("\nAmount of numbers to be generated: ");
scanf("%d", &count);
// Use current time as
// seed for random generator
srand(time(0));
printRandoms(lower, upper, count);
printf("\n\nGenerate new set? (y/n)\n");
do {
printf("\t\t==> ");
scanf(" %c", &c);
c &= 0x5f; // convert to uppercase
if (c=='N') exit(0);
} while (c != 'Y');
} while (1);
return 0;
}

How to write a series using C for loop?

I am having trouble with writing code for a series, I believe it is something with my if statement but I am stumped. The series is supposed to be
but I keep getting the wrong output. This is my code:
#include <stdio.h>
int n,t=1,nextTerm,sum=0,i;
int main() {
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(t*t);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
You have to check i+1 instead of i and replace t*t with i*i. You don't need any extra variable like t.
#include <stdio.h>
int main()
{
int n,nextTerm,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if ((i+1)%2 == 0)
nextTerm = 1;
else
nextTerm = -1;
sum=sum+(nextTerm*i*i);
}
printf("The value of the series is: %d\n",sum);
return 0;
}
Series be like this:
1, -4, 9, -16, ...
Output:
Enter an integer number:4
The value of the series is: -10
I don't know if I understood it correctly, it should be something like this?:
#include <stdio.h>
int main() {
int n,t=1,firstPart,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
printf("The serie is: \n");
for(i=1;i<=n;i++) {
if (i%2 == 0) {
firstPart = 1;
}
else {
firstPart = -1;
}
t=firstPart*(i*i);
printf(" %d\t ", t);
sum=sum+t;
}
printf("\nThe value of the series is: %d\n",sum);
return (0);
}
for n = 4, the series is:
-1 / 4 / -9 / 16 /
and the sum is 10
Replace (t*t) with (i*i)
#include <stdio.h>
int main() {
int n,i,sum=0,nextTerm,t;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(i*i);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
Output -
Enter an integer number:4
The value of the series is: -10

How to make a variable equal to a string of other variables in C

This is the full code, the dig variables are user inputs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int dig1=0;
int dig2=0;
int dig3=0;
num=dig1,dig2,dig3;
fflush(stdin);
printf("Please enter the first digit of your three digit number:");
scanf("%d", &dig1);
fflush(stdin);
printf("Please enter the second digit of your three digit number:");
scanf("%d", &dig2);
fflush(stdin);
printf("Please enter the third digit of your three digit number:");
scanf("%d", &dig3);
if (num==(dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3))
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
How could I make the variable "num" equal to all of the inputs for "dig1", "dig2", and "dig3". As in if dig 1 was 2 and dig 2 was 4 and dig 3 was 6, num would be 246. Please help!
Your problem can be solved this way:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int dig[3];
int i,j = 100;
int result = 0;
for(i = 0;i<3;i++){
fflush(stdin);
printf("Please enter the %d digit of your three digit number:",i+1);
scanf("%d", &dig[i]);
}
for(i = 0;i<3;i++){
result += pow(dig[i],3);
num += dig[i] * j;
j/=10;
printf("%d * %d = %d\n",dig[i],j,num);
}
if (num == result)
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
You could multiply these numbers according to their significant value:
int num = dig1 + dig2 * 10 + dig3 * 100;

Variable getting the wrong value?

Hello guys I need to make a program in which you input a number n.
It's not specified how large is n going to be so theres no limit.
The output is supposed to be: example n = 123456
123456 - 12345 + 1234 - 123 + 12 - 1 = 112233
1 + 12 + 123 + 1234 + 12345 + 123456 = 137171
I have the first part done, and the second as well but when I am printing the second equation it printing values that are not what I calculated.
here's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0)
{
if(a==0)
{
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
}
else if (a==1)
{
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>10)
{
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b;b>0;b--)
{
base=10^b;
num2=c/base;
if (b==1)
{
printf("%ld",num2);
}
else
{
printf("%ld + ",num2);
}
sum2+=num2;
}
printf("= %ld",sum2);
return 0;
}
I know I have extra values and that I'm printing others that are not what I state as needed, I'm just checking what values are incorrect. I think I'm getting the wrong values for num2=c/base; because of how I'm printing it or because of the variable type, I'm trying with the number 5005005, num2 the first time should be 5 and I am getting 417k ish. Any help is appreciated.
edit: I changed the power error, but now the program is crashing.
new code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>10){
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b;b>=0;b--){
base= powl(10,b);
num2=c/base;
if (b==0){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
printf("%ld",sum2);
}
printf("= %ld",sum2);
return 0;
}
edit2: fixed it, I still don't know why it crashes when using >=0
here's the fix:
for(b;b>0;b--){
base= powl(10,b);
num2=c/base;
if (b==1){
printf("%ld + ",num2);
sum2+=num2;
num2=c;
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
printf("%ld",sum2);
}
You cannot calculate power via ^ operator: it is xor operator.
d>10 will lead to wrong result when the top two digit of input is 10.
Inclemented b because the last number lacked.
Fixed code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>=10){
b++;
d/=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(num2=c,b++;b>0;b--){
if (b==1){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
num2/=10;
}
printf("= %ld",sum2);
return 0;
}
UPDATE:
To get 1 + 12 + 123 + 1234 + 12345 + 123456,
you can use base to calculate the numbers.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base=1;
int a=0;
printf("Enter an integer >=0: ");
scanf("%ld",&num);
c=num;
sum=num;
printf("%ld ",num);
while(num>0){
if(a==0){
num/=10;
sum-=num;
printf(" - %ld",num);
a=1;
} else if (a==1){
num/=10;
sum+=num;
printf(" + %ld",num);
a=0;
}
}
printf("= %ld\n",sum);
d=c;
printf("d: %ld\n ",d);
while(d>=10){
b++;
d/=10;
base*=10;
printf("%ld\n",d);
}
printf("b:%lu\n",b);
printf("c: %lu\n",c);
for(b++;b>0;b--){
num2=c/base;
if (b==1){
printf("%ld",num2);
}else{
printf("%ld + ",num2);
}
sum2+=num2;
base/=10;
}
printf("= %ld",sum2);
return 0;
}

recursive func to find prime factors

i made a recursive function to find the prime factors of a number but it has a bug which makes turbo c quit. please help
#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
int num;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
i=num
getch();
}
int primefactor(int num,int i)
{
if(i==2)
return 1;
if(num%i==0)
{
if(prime(num))
{
printf(",%d",num);
num=num/i;
i++;
}
}
i--;
primefactor(num,i);
return 0;
}
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i==0)
flag=0;
}
return flag;
}
void main(void)
{
int num,i=num; // (*)
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
getch();
}
What value do you think i will have in (*)?
Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:
void main(void)
{
int num,i;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
i = num; // assignment goes here.
primefactor(num,i);
getch();
}
(little too sleepy to write good code.. so am sorry in advance for any bugs :p )
a simpler non recursive version
printPrimeFactors(int num) {
for (i = 2; i < sqrt(num); i=getNextPrime()) {
if (num %i)
printf("%d", i);
}
}
if you have to use recursion
void factorization(int x, int i=2)
{
if(x==1)
return;
if(x%i==0&&isPrime(i))
{
printf("%d ",i);
factorization(x/i,i);
}
else
factorization(x,i+1);
}
Full recursive solution in c++ (for c replace cout lines with printf):
void printPrimeFactors(int num)
{
static int divisor = 2; // 2 is the first prime number
if ( num == 1 ) //if num = 1 we finished
{
divisor = 2; //restore divisor, so it'll be ready for the next run
return;
}
else if ( num % divisor == 0 ) //if num divided by divisor
{
cout << divisor << " "; //print divisor
printPrimeFactors( num / divisor ); //call the function with num/divisor
}
else //if num not divided by divisor
{
divisor++; //increase divisor
printPrimeFactors( num );
}
}
The best way to implement prime factorization with low overhead function calls would be . . .
void factors(int number)
{
int divisor = 2;
if (number == 1) { cout << "1"; return; }
while ((number % divisor) && (number > divisor)) divisor++;
cout << divisor << ", ";
factors(number / divisor);
}
The number of function calls (recursion) is equal to the number of prime factors, including 1.
I did this in C. Depending on the compiler, minor changes might be needed to make in the program.
#include<stdio.h>
int primefact(int);
int main()
{
int n;
printf("Enter a number whose prime factors are to be calculated : \n");
scanf_s("%d", &n);
printf("Prime factors of %d are : ");
primefact(n);
printf("\n");
return 0;
}
int primefact(int n)
{
int i=2;
while(n%i!=0)
i++;
printf("%d ", i);
if(n==i)
return 0;
else
primefact(n/i);
}
Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?
#include<stdio.h>
#include<stdlib.h>
int ar[10]={0};
int i=0,j=2;
void P(int n)
{
if(n<=1){
return ;
}
else{
if(n%j == 0){
printf("%d\t",j);
n=n/j;
}
else{
j++;
}
P(n);
}
}
int main(void)
{
int n;
printf("Enter n = ");
scanf("%d",&n);
P(n);
printf("\n");
return 0;
}
// recursivePrime.cpp
// Purpose: factor finding for an integer
// Author: Ping-Sung Liao, Kaohsiung,TAIWAN
// Date: 2017/02/02
// Version : 1.0
// Reference:
// http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int primefactor(int num,int i);
int main(void)
{
int num, i;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
i=int ( sqrt (num) );
primefactor(num,i);
system("pause"); // instead of getch()
}
int primefactor(int num,int i)
{ printf("num %d i=%d\n", num, i);
if (i==1)
printf("prime found= %d\n", num); // prime appearing in he variuable num
else if(num%i==0)
{ primefactor( int (num/i) , int ( sqrt(num/i) ) );
primefactor( i , int (sqrt ( i ) ) );
}
else
{ i--;
primefactor(num,i);
}
return 0;
}
#include <`stdio.h`>
void pf(int,int);
int main()
{
int a,i=2;
printf("Enter the Number:\n");
scanf("%d",&a);
pf(a,i);
}
void pf(int x,int y)
{
if(x==1)
return 1;
else
{
if(x%y==0)
{printf("%d\t",y);
pf(x/y,y);
}
else
{
y++;
pf(x,y);
}
}
}
We need not write function to calculate next prime number. If for example, num is 24 and we continously divide it by 2 until it is no longer divisible by 2, then no other multiples of 2 can divide the number either. So ultimately only(probably) prime numbers can perfectly divide any positive integer number.
Here is my code: (I've written source code for both iterative as well as recursive logic)
#include<stdio.h>
void pfactors_rec(int, int);
void pfactors(int);
int main()
{
int num;
printf("Enter a positive integer number\n");
scanf("%d", &num);
printf("\nPrime Factors of %d without using recursion\n", num);
pfactors(num);
printf("\nPrime Factors of %d using recursion\n", num);
pfactors_rec(num, 2);
return 0;
}
void pfactors_rec(int num, int count)
{
if(num < 1)
return;
else if(num % count == 0)
{
printf("%d\n", count);
pfactors_rec(num / count, count);
}
else
{
pfactors_rec(num, count+1);
}
}
void pfactors(int num)
{
int count;
for(count = 2; (num > 1); count++)
{
while(num % count == 0)
{
printf("%d\n", count);
num = num / count;
}
}
printf("\n");
}
Implementation in java..
public class PrimeFactor {
public int divisor=2;
void printPrimeFactors(int num)
{
if(num == 1)
return;
if(num%divisor!=0)
{
while(num%divisor!=0)
++divisor;
}
if(num%divisor==0){
System.out.println(divisor);
printPrimeFactors(num/divisor);
}
}
public static void main(String[] args)
{
PrimeFactor obj = new PrimeFactor();
obj.printPrimeFactors(90);
}
}

Resources