Displaying perfect numbers and their adders - c

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

Related

Functions and arrays which allows you to print what's the most repeated number

I have a problem with my code, it doesn't print the result I expect. This code allows the user to enter as many numbers as he wishes and then print the most repeated one
Here it is:
#include <stdio.h>
void reading_numbers(int array[]){
int i = 0;
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
}
void most_present_number(int array[], int Max){
int i = 0;
reading_numbers(array);
int current_number = array[i];
int current_number_count = 0;
int most_present_one = 0;
int most_present_one_counter = 0;
while (i < Max) {
if (array[i] == current_number) {
current_number_count++;
i++;
} else {
if (current_number_count > most_present_one_counter){
most_present_one = current_number;
most_present_one_counter = current_number_count;
}
current_number_count = 1;
}
}
printf("This is the most present number %d it is repeated %d times\n", most_present_one,
most_present_one_counter);
}
int main() {
int Max = 0;
int array[Max];
most_present_number(array, Max);
return 0;
}
The problem for me is when I call the function, but I don't know how to fix it
I should have written as a premise but I'm a bit new to C so probably there are some things in this code that don't make sense
I make a procedure to find the result ,int main() must have the size of array (mistake logic),because the procedure take the parameters of the main function (int main ())
Here is my code:
#include<stdio.h>
void most_present_number(int Max,int T[Max])
{
int i = 0;
while (i < Max)
{
printf("Insert the numbers :");
scanf("%d", &T[i]);
i++;
}
int k=0,cpt1=0,cpt=0;
for(int i=0;i<Max;i++)
{
cpt=0;
for(int j=i+1;j<Max;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>=cpt1)
{
cpt1=cpt;
k=T[i];
}
}
printf("This is the most present number %d it is repeated %d times\n",k,cpt1+1);
}
int main()
{
int Max = 0;
do
{
printf("How much long the array will be?\n");
scanf("%d", &Max);
}while(Max<1);
int T[Max];
most_present_number(Max,T);
}
the following proposed code:
cleanly compiles
performs the desired functionality
only includes header files those contents are actually used
and now the proposed code:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
a typical run of the code:
How much long the array will be?
4
Insert the numbers
1
Insert the numbers
2
Insert the numbers
3
Insert the numbers
2
This is the most present number 2 it is repeated 2 times

How can I pass values from one function to another?

I have created a program that takes in input "n" numbers that the user chooses and then prints the most repeated one, but I have a problem with passing the values between the functions so it gives me 0 as a result. How can I solve it?
void most_present_number(int array[]);
int read_numbers(int array[]);
int main() {
int array[400];
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
read_numbers(array);
int i = 0;
int Max = 0;
int Current_number = vettore[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
while (i < Max) {
if (array[i] == Current_number) {
Current_number_counter++;
i++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
i++;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
You have Max = 0 in most_present_number(), so the while loop stops immediately.
read_numbers() returns Max, so you can use this to initialize Max in most_present_number().
void most_present_number(int array[], int Max);
int read_numbers(int array[]);
int main() {
int array[400];
int size;
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
int Max = read_numbers(array);
int i;
int Current_number = array[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
for (i = 0; i < Max; i++) {
if (array[i] == Current_number) {
Current_number_counter++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
Note also that your algorithm assumes that all the equal numbers will be together in the array. If they can be mixed up, you need a very different design. You need another array where you keep the counts of each number. Then at the end you find the entry in this array with the highest count.

Print all the permutations using recursion

I coded as below to print all the permutations of three number :1,2,3.
But the output is:
1,1,1
1,1,2
1,1,3
1,2,1
1,2,2
1,2,3
The code is as follows:
#include<stdio.h>
#include<conio.h>
void perm(int);
int a[10],l=2;
int main()
{
int k;
k=0;
perm(k);
getch();
return 0;
}
void perm(int k)
{
int i;
for(a[k]=1;a[k]<=3;a[k]++)
{
if(k==2)
{
for(i=0;i<3;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
else
{
k++;
perm(k);
}
}
}
Please give the correct code.
Why do you increment k? k should not change for a given call to perm().
Also it's a bit too bad to be stuck with 3 permutations, you can easily generalize this way:
#include<stdio.h>
#include<conio.h>
static void perm(int, int);
static void all_perm(int);
int a[10];
int main()
{
all_perm(3);
getch();
return 0;
}
void all_perm(int n)
{
perm(0, n);
}
void perm(int k, int n)
{
if (k == n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(a[k]=1; a[k] <= n; a[k]++)
{
perm(k + 1, n);
}
}
}
Edit: Well, what you name permutations are not permutations.
The logic that I have used is to some extent similar to yours.
I have included the entire code to make it clear.
#include <stdio.h>
void recn(int*,int,int);
void print_arr(int*,int);
void main()
{
int arr[3] = {1,2,3};
recn(arr,3,0);
}
void print_arr(int *arr, int n){
int i;
for(i = 0,printf("\n"); i < n; printf("%d",arr[i++]));
}
void recn(int *arr, int n, int l) {
int i, j, f, k, xx = 0;
static int tst[15], a[14]={0};
if (l == n) {
for (i = 0; i < n; i++) {
tst[i] = arr[a[i]];
}
print_arr(tst,n);
return;
}
for (i = 0; i < n; i++) {
f = 0;
for (j = 0; j < l; j++)
if (a[j] == i)
f = 1;
if (!f) {
a[l] = i;
recn(arr, n, l + 1);
}
}
}

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

Why does this program crashes on execution?

I am trying to run this code on Dev C++ but it keeps on crashing after the user inputs the two numbers. The program takes input m and n from user two numbers and then returns the output as the solution of the function A which is:
A(m,n) = A(m,n-1)+ A(m-1, n) , if m,n >0
A(m,n) = m-n if m or n <0
Can anybody please tell me why is it happening?
#include<stdio.h>
#include<stdlib.h>
int main() {
int num1=0;
int num2=0;
int rows=0;
int columns=0;
int i,j,**array;
printf("Enter two non-negative integer numbers \n");
scanf("%d %d",&num1,&num2);
//create 2d-Array
rows=num1+1;
columns=num2+1;
array=malloc(rows * sizeof(int *));
for(i=0;i<rows;i++)
{
array[i]=malloc(columns*sizeof(int));
}
//Fill data in array
computeArray(array,rows,columns);
// Display contents of array
for (i = 0; i < rows; i++ )
{
for(j= 0; j < columns; j++ )
{
printf("array[%d][%d] = %d\n", i,j, array[i][j] );
}
}
getch();
return 0;
}
int computeArray (int **array, int rows, int columns) {
int i,j;
for(i=0; i<rows;i++)
{
for(j=0;j<columns;j++)
{
array[i][j]=computeFunction(array,i,j);
}
}
return **array;
}
int computeFunction(int **array, int i, int j) {
int value=0;
if((i<0)||(j <0))
{
value = i-j;
printf("%d",value);
return value;
}
else
{
value = (array[i][j-1] + array[i-1][j]);
printf("%d",value);
return value;
}
return value;
}
When program's behavior is undefined, anything could happen. You should declare a prototype for the function computeArray and computeFunction before main:
int computeArray (int **array, int rows, int columns);
int computeFunction(int **array, int i, int j);
and change
if((i<0)||(j <0)) {...}
in computeFunction to
if((i<=0) || (j <= 0)){...}
&& instead of || may help.
The code fails at the
value = (array[i][j-1] + array[i-1][j]);
line, when j==0.
Debuggers tend to be very useful for spotting simple mistakes. Use them.

Resources