Pascals Triangle - c

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

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

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.

How could I do this string operation?

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

prime factors of a number

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

Resources