C Programming Tutorial 72 - Counting Prime Numbers (Part 1)

preview_player
Показать описание


~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
Рекомендации по теме
Комментарии
Автор

Hey man. So I gave it a try before watching the rest of the videos for this mini-program and here is what I came up with. It may not be perfect and clean but hey, it does what the boss asked for! Looking forward to seeing your solution now :)


/* Create a program that takes input from a user and output all prime numbers between 0 and the user input. At the end, output how many prime numbers there are */

#include <stdio.h>

int main(void) {

int i, input, k, sum, notPrime, isPrime;

printf("This program will calculate which numbers are prime between 0 and your choice and output the total number of prime numbers in this range.\n\n");

printf("Enter a number: ");
scanf("%d", &input);

for(i = input; i > 1; i--) {

for(k = i - 1; k > 1; k--) {
sum = i % k;

if(sum == 0) {
//printf("%d is not prime\n", i);
break;
}

}

if (sum != 0) {
printf("\n%d is a prime number\n", i);
isPrime++;
}

}

printf("\nThere is a total of %d prime numbers between %d and 2 inclusive", isPrime, input);

return 0;
}

LimeWitness
Автор

i can't believe i understood this at last ! thank you !

bassemhodhod
Автор

Counting prime numbers really calms me down

lebaguette
Автор

Prime numbers? More like prime-time, because this should be shown to more people!

PunmasterSTP
Автор

Came here because i am doin a course on udemy and can copy the code word for word and i can kinda put 2 and 2 together but i need more details before i move on this is irking me thought a prime number was supposed to come out 0 when using modulus but only 2 times as prime numbers have only two factors but when i look at codes that work the fact that the modulus answer is 0 is a bad thing and i just don't get it. So let's see if this video can explain it to me cause i just can't copy code and move on.

thefluffles
Автор

Hello sir iam no. 1 fun of your channel sir

balladictz
Автор

0 and 1 are and are not prime according to different definitions.

Yeaaaah. The history of math is annoying.

jadoaesra
Автор

I tried to do it myself. I think it works?

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

int main()
{
/*
Take an integer input.
Print all prime numbers from 1 to input.
Print the amount of prime numbers from 1 to input.
To compile:
gcc primeCounter.c -o primeCounter -lm
*/
int input;

printf("Welcome to the Prime Counter!\nEnter a number for the prime counter\n: ");
scanf("%d", &input);

int i;
int k;
bool isComposite;
int number_of_primes = 0;

for(i=2;i<=input;i++)
{
//Check if input is prime.
//Use modulus?

for(k=2;k<=sqrt(i);k++)
{
if(i%k == 0)
{
isComposite = true;
k += i;
}else{
isComposite = false;
}
}

if(isComposite == false)
{
printf("%d\n", i);
number_of_primes += 1;
}
}
printf("There are %d prime numbers between 0 and %d.\n", number_of_primes, input);
return 0;
}

nik-jzvk
welcome to shbcf.ru