Miscellaneous > Computers and Technology

C code.

(1/2) > >>

Ameer:
Hey, I am actually working on something that I didn't know how to solve yet
I'll appreciate it if someone can help me out

What the program is :


1 - You enter a number as an ( Int ) - Int x
2 - the number goes into a loop, what happens there is it gets divided by i ( from i = 1 until i = x ), and puts it in a new Integer lets call it ( Sum ), and prints like this ( i , Sum )  it if its only a Natural number
3 - it only prints it if ( I ) is a prime number.

for example if X = 70
it prints
(1,70)
(2,35)
(5,14)
(7,10)

If x = 60
(1,60)
(2,30)
(3,20)
(5,12)

so it doesn't print both
(4,15) and (6,10)

Kaarel 123:
So what you're having trouble with is especially the 3rd part?

If so then you should simply check if i isnt a prime number before printing the text.

Ameer:

--- Quote from: Kaarel 123 on April 08, 2017, 09:00:20 am ---So what you're having trouble with is especially the 3rd part?

If so then you should simply check if i isnt a prime number before printing the text.

--- End quote ---

Yes checking if ( i ) is prime number before printing it is the only problem ( I guess ) I am having atm

I tried to search at google then use the codes i found there, however didn't work at all

Bluestorm:
Use this function to check if a number is prime or not:

int is_prime(int num)
{
     if (num <= 1) return 0;
     if (num % 2 == 0 && num > 2) return 0;
     for(int i = 3; i < num / 2; i+= 2)
     {
         if (num % i == 0)
             return 0;
     }
     return 1;
}

If you looking for faster code use sqrt(num) instead of num/2 in the loop.

Ameer:

--- Quote from: Bluestorm on April 08, 2017, 06:49:56 pm ---Use this function to check if a number is prime or not:

int is_prime(int num)
{
     if (num <= 1) return 0;
     if (num % 2 == 0 && num > 2) return 0;
     for(int i = 3; i < num / 2; i+= 2)
     {
         if (num % i == 0)
             return 0;
     }
     return 1;
}

If you looking for faster code use sqrt(num) instead of num/2 in the loop.

--- End quote ---

Forgot to mention however in this assignment I am not allowed to use anything other than int main ()
:/

it would've been super easy with functions tho ;P

Thanks, adam.

Navigation

[0] Message Index

[#] Next page

There was an error while liking
Liking...
Go to full version