Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

whats wrong with this?

0 votes
asked Jul 8, 2020 by Sãthwìk Ñïmmãlâ (120 points)
#include <stdio.h>

int main()
{   
    int i;
    int a[]={1,2,3,4,5,6,7,8};
    for(i=0;a[i]/i==1;a[i]/1==1,i++);
    printf("prime numbers are:");
    return 0;
}

2 Answers

0 votes
answered Jul 12, 2020 by Peter Minarik (86,040 points)
edited Jul 12, 2020 by Peter Minarik

What is a prime number?

First, you have to understand what prime numbers are: https://en.wikipedia.org/wiki/Prime_number

  • "is a natural number greater than 1 that is not a product of two smaller natural numbers"
  • "a different but equivalent definition of the primes: they are the numbers with exactly two positive divisors, 1 and the number itself."

So knowing this, you can try to write your code again. ;)

Problems with the code

From programming perspective, the code looks funny. You have an array called a on which you would like to iterate through and check if the elements are primes or not. A typical loop would do the body as many times as many items are in the array.

Suggested solution

I would start off with something like this:

#include <stdio.h>

#define NUMBER_COUNT    10

int isPrime(int number)
{
    // TODO: Implement isPrime(): return TRUE if `number` can be divided by 1 and itself only.
    return 0;
}

void main()
{
    int numbers[NUMBER_COUNT] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    printf("Prime numbers: ");
    for (int i = 0; i < NUMBER_COUNT; i++)
    {
        if (isPrime(numbers[i]))
            printf("%d, ", numbers[i]);
    }
}

Now you can write the body of the isPrime() method and test if your code works correctly. -- Of course, I have written my version of it, but I don't want to ruin the fun for you, so go ahead, and give it a try. ;)

When you're done with it, you can try to optimize it further.

Also, feel free to post your solution if you want some one to have a quick review of it.

Good luck!

0 votes
answered Jul 22, 2020 by Dabons22 (250 points)

the parameters of your looping conditions  for(i=0;a[i]/i==1;a[i]/1==1,i++);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...