samedi 29 octobre 2022

My code produce random number without using rand()

I expected as output positive divisors of 76 but when I run, the output is 2 and a random integer value. When I put 1 instead of 2 for i in the iz_num function it prints 1 and 2.

#include <stdio.h>
#include "stdbool.h"

int sqrt1(int n)
{
    for(int i = 1; true; i++)
    {
        if ( i*i >n)
        {
            return i-1;
        }
    }
}

bool isPrime1(int a) {
    if (a !=2 && a%2==0)
        return false;

    int sq = sqrt1(a);
    for (int d = 3; d<= sq; d+=2)
    {
        if ( a%d==0)
            return false;
    }
    return true;
}

void iz_num(int arr[], int num){
    int k=0;
    if (isPrime1(num))
        return;
    for(int i = 2; i <= num / 2 ; ++i,++k){

        if (num % i == 0){
            arr[k]=i;

        }
    }
    int size = sizeof (arr) / sizeof (size);
    for (int j = 0; j < size;++j){
        printf("%d ", arr[j]);

    }
}

int main()
{
    int arr[] = {};
    iz_num(arr,76);
}

Next I would find just prime divisors of 76.




Aucun commentaire:

Enregistrer un commentaire