I'm trying to figure out a way to solve this problem. The user has to input a number N, and the program has to print the numbers that have GCD= 1 between them and N.
If the input is 5, then the program should print "1 2 3 4".
For input 6, then print should be "1 5".
Yet my code doesn't print anything. Could anyone help me out?
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b)
{
if (a==b)
return a;
else
if(a<b)
return gcd(a, b-a);
else
return gcd(a-b, b);
}
int main()
{
int i=0;
long int n;
scanf("%ld", &n);
if (n<1 || n>999999999)
printf("Wrong Input");
else
{
for(i=0; i<=n ; i++)
{
gcd(n, i);
if (gcd(n,i)==1)
printf("%d ", i);
}
}
return 0;
}
I changed the loop
for (int i=0;i<=n;i++) to (int i=1;i<=n;i++) because zero has infinity of divisors
int main()
{
int found=0;
long n;
scanf("%ld", &n);
if (n<1 || n>999999999)
printf("Wrong Input");
else
{
for(int i=1; i<=n ; i++)
{
gcd(i, n);
if (gcd(n,i)==1)
printf("%d ", i);
}
}
return 0;
}
int gcd(int a, int b)
{
if (a==b)
{
return a;
}
else if(a<b)
{
return gcd(a, b-a);
}
else
{
return gcd(a-b, b);
}
}
Try this:
int _gcd(int a, int b)
{
int c = a % b;
if (c == 0)
return b;
return _gcd(b, c);
}
int gcd(int a, int b)
{
return a > b ? _gcd(a, b) : _gcd(b, a);
}
In addition to that, you obviously need to change this:
for (i = 0; i <= n; i++)
To this:
for (i = 1; i < n; i++)
Related
I need help with the program to display first 4 perfect numbers in the standard output and also funciton perfect(int,int*). Arguments of this function are natural number and the adress where you neeed to write the adders (of the perfect number I suppose). Function has to return 1 if the number is perfect, and 0 if it's not. This is what I've done so far. Help please.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int perfect(int,int*);
int main()
{
int *arr,a;
int i,j;
perfect(a,arr);
}
int perfect(int n,int *arr)
{
int lim=8128,i,sum;
for(n=1;n<=lim;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
printf("%d ",n);
}
}
I think this code is helpful for you. The perfect function returns 1 when perfect otherwise return 0. The global array divisor which is used for collecting the addr. When the perfect function returns 1 then I print the divisor array and initiate the deivisor_count =0. Please have a look:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int divisor[1000], deivisor_count = 0;
int perfect(int n)
{
int sum=0, i, j = 0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
divisor[deivisor_count]= i;
deivisor_count = deivisor_count + 1;
}
}
if(n==sum){
return 1;
}
return 0;
}
int main()
{
int i, j =0, is_perfact, n=100000, k;
for (i =2 ; i<=n; i++){
deivisor_count = 0;
is_perfact = perfect(i);
if(is_perfact == 1){
j = j + 1;
for(k = 0; k <deivisor_count; k++){
printf("%d", divisor[k]);
if (deivisor_count -1 == k){
printf("=");
}
else{
printf("+");
}
}
printf("%d\n", i);
}
if (j==4){
break;
}
}
return 0;
}
I am trying to write a program which calculates some bags and weights. I wrote it without using functions but I have to use functions and I am really bad at it.
The code normally works, but I just can't implement it with functions. It stops working after printing array A, and just 0s when printing array B.
My code is:
#include <stdio.h>
#include <math.h>
int f1(int N);
int f2(int N);
int f3(int N, float A[20]);
int main(void)
{
int N;
f1(N);
return 0;
}
int f1(int N)
{
for(;;)
{
printf("Enter N(the number of bags) (Between 1 and 20): ");
scanf("%d", &N);
if (N < 1 || N > 20)
{
continue;
}
else
{
break;
}
}
f2(N);
}
int f2(int N)
{
float A[20];
int i;
for(i=0; i<N;i++)
{
printf("Enter the weight of the bag with potatoes %d: ", i+1);
scanf("%f", &A[i]);
}
printf("\n\nThe weights of the initial bags (the A array):\n");
for(i=0; i<N;i++)
{
printf("%.1f " ,A[i]);
}
f3(N, &A[20]);
}
int f3(int N, float A[20])
{
int i;
float B[10];
printf("\n\nNow we equalize the weights of bags.\n");
if (N%2 == 0)
{
for(i=0;i<N/2 ;i++)
{
B[i] = fabsf(A[i] - A[N-1-i]);
}
}
else
{
for(i=0;i<N/2 ;i++)
{
B[i] = fabsf(A[i] - A[N-1-i]);
}
B[N/2] = A[N/2];
}
if (N%2 == 0)
{
for (i=0; i<N/2; i++)
{
if (A[i] < A[N-1-i])
{
A[N-1-i] = A[i];
}
else
{
A[i] = A[N-1-i];
}
}
}
else
{
for (i=0; i<N/2; i++)
{
if (A[i] < A[N-1-i])
{
A[N-1-i] = A[i];
}
else
{
A[i] = A[N-1-i];
}
}
A[N/2] = 0;
}
printf("\nThe weights of the new bags (the B array):\n");
if (N%2 == 0)
{
for(i=0; i<N/2 ;i++)
{
printf("%.1f " ,B[i]);
}
}
else
{
for(i=0; i<N/2 ;i++)
{
printf("%.1f " ,B[i]);
}
printf("%.1f", B[N/2]);
}
printf("\nThe new weights of the initial bags (the A array):\n");
for(i=0;i<N;i++)
{
printf("%.1f ", A[i]);
}
}
To pass an array to a function just use its name.
f3(N, &A[20]);
should be
f3(N, A);
To pass an array or pointer as an argument when calling a function in C, you just need to pass it name, in your case,
f3(N, A);
Also, when declaring the function, the length of the array doesn't matter, because C performs no bounds checking for formal parameters. Although it will work this way, it is best to change
int f3(int N, float A[20])
to
int f3(int N, float A[])
or
int f3(int N, float* A)
I was trying to print Pascal's Triangle using the following code. But after printing the first '1' the compiler runs into an error and I have to manually stop the execution. What might be the error in the code?
EDIT: fact function has been changed as shown with no difference whatsoever.
I'm using Codeblocks 10.05. A dialog window pops up and says that .exe has stopped working and Windows is searching for a solution.
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<2*row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
if(a==0)
printf("%d",1);
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num == 1)
{
return 1;
}
else return num*fact(num-1);
}
Your problem is that fact(0) will go into an infinite loop calling fact(-1), then fact(-2), and so on. Eventually it'll crash due to recursion running out of stack space.
How does fact(0) happen? when fact(a-b) is called when a == b.
This currently happens when row == 2, k == 1.
There are two mistakes that I can see
fact function: it's infinitely recursive
improper bounds of loop which calls comb function. There are row + 1 elements in each row, not 2 * row - 1. your formula works only for the first two rows.
Try this code
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n;
printf("Height: ");
scanf("%d",&n);
for(int row = 1; row <= n; row++)
{
if( row == 1)
printf(" ");
for(int j = 0;j < n - row; j++)
{
printf(" ");
}
for (int k = 0;k < row + 1;k++)
{
if ( row != 1)
comb(row,k);
else
{
printf("1");
break;
}
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
printf("%d ",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num > 1)
return num * fact(num-1);
else
return 1;
}
int main(){
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<=row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b){
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num){
if(num < 2)
return 1;
else
return num*fact(num-1);
}
#include <stdio.h>
int factor_power(int n,int d);
int main()
{
int input;
do
{
printf("Enter an integer (> 1): ");
scanf("%d",&input);
}while(input<2);
printf("%d = ", input);
int current=input;
int i;
for(i=2; i<=current; i++)
{
int power=power_factor(current,i);
if(power!=0)
{
current=(int) (current/pow(i, power));
printf("%d^%d * ",i,power);
}
}
return 0;
}
int power_factor(int n, int d)
{
int power=0;
if(d<=n)
{
while(n%d==0)
{
power++;
n=n/d;
}
return power;
}
return 0;
}
Hello, I am new to C. I have a problem with the output of the code above. If you run the code you will see there is a extra * at the end of the output. Since C doesnt have a string class, how could I get rid of the * at the end. I know appending string is a options but is there a quicker and efficient way of solving this problem?
Print the * in an alternative way.
int current=input;
int i;
int first_term = 1;
for(i=2; i<=current; i++)
{
int power=power_factor(current,i);
if(power!=0)
{
current=(int) (current/pow(i, power));
if (!first_term)
printf(" * ");
first_term = 0;
printf("%d^%d",i,power);
}
}
I was trying to develop a code to compute the [prime factors][1] of a number, but I am getting nothing as output. Can anyone point out where I am making the mistake?
#include<stdio.h>
#include<math.h>
int prime_check(int i)
{
int j;
for(j=2;j<i;j++)
{
if(i%j==0)
return 0;
}
return i;
}
void prime(int n)
{
double c=sqrt(n);
int i;
int p[10];
//printf("factors are: ");
for(i=1;i<=c;i++)
{
p[i]=prime_check(i);
//printf("%d ",p[i]);
if(n % p[i] == 0)
printf("%d ",p[i]);
}
}
main()
{
//printf("enter the number:\t");
int num=36;
//scanf("%d",&num);
prime(num);
}
TO EVERYONE I GOT THE ANSWER! Thanks Jeff Mercado for showing me my mistake
void prime(int n)
{
int c= floor(sqrt(n)); //updated code
int i;
int p[10];
//printf("factors are: ");
for(i=1;i<=c;i++)
{
p[i]=prime_check(i);
if(p[i] == 0) //added check
continue;
//printf("%d ",p[i]);
if(n % p[i] == 0)
printf("%d ",p[i]);
}
}