The world is currently online!


Welcome to Emps-World!

Register now to gain access to all of our forum features. Once registered and logged in, you will be able to create topics, post replies, send private messages, manage your profile, chat with other players in the shoutbox and much more. Once you sign in, this message will disappear.



Pages: 1
0 Members and 1 Guest are viewing this topic.

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
C code.
« on: April 08, 2017, 08:30:43 am »
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)




Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.

Offline Kaarel 123

  • *
  • 661
  • Liked: 377 times
  • +0/-0
  • mak merica grate agen Ȃ̴̍͐͆̓̌̀̓̈̋̈̌̇̀͛̈́̉̈̒͒͘̕̚͝͝͠
    • View Profile
Re: C code.
« Reply #1 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.

Ȃ̴̛̛̰̤̘͉̍͐͆̓̌̀̓̈̋̈̌̇̀͛̈́̉̈̒͒̋̂́̍̽̈̔̉̑̐͋̐̾͛̎̄͛́̈́̅͐͋̃͋̿͂͂͛̋͛̆͊̐̋͗͒͊͋̈́͛͑͑̉̉͒͌͆̾̂̈́̋̆̍̓̊̄̑̈͒̒͆͑̋̿̄͌͒͛́̑̿̑͗̇͋̈́̅̎̍͘̕̚̚͘͘͘̚̚̚̕͝͝͠͝͝͝͝͝
̶̡̡̨̢̨̨̨̢̢̡̢̛̛̛̙̟̗͎͍̞̪̖͙͓͖̥̗̲̬̫̬̝͉͔͍̻̹̙͕̱̮̲̺̭͉͔̹̖̮̖͖̳͉̩͙̲͚̝̲̺̥̬̰̝͙̤̭͕̝̯̺͔͎̜̳͙̖̫͉͉̗͉̱̭̗̱̦̭̹̗͓̤̟̻̞͂͌̌́̀̌̾͊̅̾̊̅̈́̈́͂̆̔̊̑̂̽̔̌̀͗̈́̃͆͗̌́̀͋̈̌͗̐͌̃̌͒̏̆̊͆̓̊̀͒̑̈̀̈́͑̏̋͆̇̈́̽̍̽̃̾̔̈́̀̾̈́͂̌̈́̐̃̊̀̌̇̾͛̽̽́̌̓̅̉̓̎̊̄̇̓̈́̎́̿̍͂̑̌̂̀̔̏͛̆͒͛̃̌̋͘̕̚̚͘͘͜͜͝͝͝͝ͅͅ

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
Re: C code.
« Reply #2 on: April 08, 2017, 06:08:35 pm »
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.

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



Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.

Online Bluestorm

  • *
  • *
  • 186
  • Liked: 522 times
  • +1/-0
  • Adam.
    • View Profile
Re: C code.
« Reply #3 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.
The following users liked this post: Ameer

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
Re: C code.
« Reply #4 on: April 08, 2017, 07:58:38 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.

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.



Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.

Online Bluestorm

  • *
  • *
  • 186
  • Liked: 522 times
  • +1/-0
  • Adam.
    • View Profile
Re: C code.
« Reply #5 on: April 08, 2017, 08:26:47 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.

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.

Well you can use the loop in this function in the main() ;)

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
Re: C code.
« Reply #6 on: April 09, 2017, 07:40:27 am »
#include<stdio.h>
int main ()
{
    int num;
    int i;
    float sum;
    printf("Enter a number ");
    scanf("%d", &num);
    printf("prime numbers that it is divided by:\n");
    for (i = 1; i<=num; i++)
    {
        sum = (float)num / i;
        if (sum == (int)sum && i < sum)
        {
            printf("(%d,%.0f)\n", i,sum);
        }
    }
    return 0;
}




I couldn't actually find a way where I have to check if ( i ) is prime or not like using a loop inside another loop



Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.
Pages: 1