Use C to find prime numbers within 100

Updated on educate 2024-03-27
4 answers
  1. Anonymous users2024-02-07

    Gives a function that determines whether a number is prime or not.

    bool judegenum(int num)if(num == 1)

    return true;

    for(int i=2; ireturn false;

    return true;

    The main function is given.

    int main()

    for(int i=1; i<=100;i++)if(judgenum(i) == true).

    printf("%d", i);

  2. Anonymous users2024-02-06

    I haven't learned c, but I wrote one in c++, you refer to the idea (only write the key parts) for(i=0; i<100;i++)

    for(j=2;jif(i%j==0)

    flag=1;If a factor is found, let flag=1

  3. Anonymous users2024-02-05

    The first 2 c, mine is c

    public static void main()bool isv = new byte[101];

    for(int i = 0;i < 101; i++)isv[i] = true;

    for(int i = 3; i < 101;i++)if(isv[i]) for(int j = 2; i * j <= 100; j++)isv[i * j] = false;

    for(int i = 0;i < 101; i++)"", isv[i]);

  4. Anonymous users2024-02-04

    If you want to use c to calculate the sum of all prime numbers between 1 and 100, you must first know what a prime number is?

    Whether a number is prime or notA "prime number" is a natural number that cannot be divisible by any integer except 1 and itself. Like 1, 2, 3, 5, 7 are all prime numbers.

    Once you understand the concept of prime numbers, then the program is easy to write. To know if a number x is prime, then all the integers between 1 x need to be divided by x, and if there is such a number (except 1 and x) that is divisible by x (with a remainder of 0), then x is not prime, otherwise x is prime. This is a general solution.

    Then it's up to you to optimize it.

    Actually, we don't need to check all integers between 1 x. Just check the number between 1 [root number x] and you're good to go. As for why, it can be understood from the perspective of multiplication, we are doing division, in fact, the reverse is multiplication.

    Let's take the case of determining whether 15 is prime or not. When we divide by 3, 3*5=15. So, while judging 3, we also judge another number 5.

    So we just need to check the root number x, and when the root number x is checked, the root number x will also be checked after the root number x.

    **(c#private static void printprimes(int n)

    if (j > k)}}

    printprimes(100);All prime numbers from 1 to 100 can be printed.

    Note the for loop.

Related questions
4 answers2024-03-27

#include

#include >>>More

6 answers2024-03-27

#include

using namespace std; >>>More

5 answers2024-03-27

Consider putting these 100 numbers in an array first, taking a random position one at a time (1-100 for the first time, 1-99 for the second time,..).Swapping the number of that position with the number of the last position will do the trick. >>>More

5 answers2024-03-27

bool f=0;

for(int i=1000;i>=1;i--) Start with the largest number of 1000 and try to the smaller number. >>>More

3 answers2024-03-27

The base and high area of the simple input triangle are presented as follows: >>>More