prime factors of a number - c

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]);
}
}

Related

Given thr GCD(=1) and one number, find the second number

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++)

Displaying perfect numbers and their adders

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;
}

How to pass arrays to functions

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)

Finding next smallest palindrome, infinite loop

I am writing a code to find the next smallest palindrome(integer) . I am (must) using array to deal with too large numbers like below:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
void check_pal(int a[],int max)
{
int i,j,ctr,k;
while(1)
{
for(i=0;i<max;i++)
printf("%d",a[i]);
ctr=0;
k=max-1;
while(a[k]==9)
{
a[k--]=0;//add corner case when k==0
}
a[k]++;
for(i=0,j=max;i<max/2;i++,j--)
{
printf("%d",i);
if(a[i]!=a[j])
{
ctr=1;
break;
}
}
if(ctr==0)
for(i=0;i<max;i++)
{
printf("%d",a[i]);
if(i==max-1)
return;
}
}
}
void int_convert(char * m,int a[] )
{
int i,max;
for(i=0;i<strlen(m);i++)
{
// printf("%c",m[i]);
a[i]=m[i]-'0';
}
max=strlen( m);
printf("%d\n",max);
check_pal(a,max);
}
void main()
{ int a[200],max;
char * m=malloc(sizeof(char)*200);
scanf("%s",m);
int_convert(m,a);
getch();
}
The output result is an infinite loop .
For e.g. for input 45 the output must be 55 but it is resulting in 0000000 ..
Please tell me where I am wrong .
It is not difficult to recognize palindromes:
int is_palindrome(int a[], int max) {
for (int i = 0; i < max/2; i++) {
if (a[i] != a[max-i-1]) {
return 0;
}
}
return 1;
}
It is not difficult to increment the value:
void next_value(int a[], int max) {
int i = max - 1;
a[i]++;
while (i > 0 && a[i] > 9) {
a[i] = 0;
a[i-1]++;
i--;
}
}
It's easy to display the value:
void show(int a[], int max) {
for (int i = 0; i < max; i++) {
printf("%d", a[i]);
}
printf("\n");
}
With this support it's trivial to find the smallest following palindrome:
void check_pal(int a[], int max) {
while (!is_palindrome(a, max)) {
next_value(a, max);
}
show(a, max);
}
By the way, I would call the function find_pal rather than check_pal.

Pascals Triangle

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);
}

Resources