C Programming Tutorial 75 - Counting Prime Numbers Down From Input (Counting Prime Numbers Part 4)

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


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

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

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

thank you very much for this video
very informative and well explained
thank you so much

mohitsilori
Автор

If anyone is interested, here is my program from the last couple of videos. It identifies if the number is prime and writes it down in terms of its prime factors.

#include <stdio.h>

int main()
{
int input, i;
int sum = 0;

printf("Please enter the desired value: ");
scanf("%d", &input);

printf("%d = ", input);

int newValue = input;

if(newValue > 1)
{
for(i = 2; i <= input; i++)
{
while(newValue % i == 0)
{
sum++;
newValue = newValue / i;

if(newValue > 1)
{
* ", i);
}
else
{
i);
}


}
}

printf("\nThe amount of factors of this number is: %d\n", sum);

if(sum == 1)
{
printf("Therefore %d is a prime number.\n", input);
}
else
{
printf("Therefore %d is not a prime number.\n", input);
}
}
else
{
printf("Please enter a value higher than 1 shithead.\n");
}

return 0;
}

codex
Автор

great course but this video really lost me, you skipped the explanation of a lot

HamedAdefuwa
Автор

How do I show how many number are prime, for example between 2 and 10 there are 4 prime numbers.

vicentecaballero
Автор

Hey, we need to know about functions! If we don't, we won't be able to...function 😎

PunmasterSTP
Автор

So when I write my function isPrime if it is located below the main and I try to compile it wont compile and if I move it above the main it works fine, any idea why?

CaptainQ
Автор

My code:

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

bool testPrime(int input); //function prototype

int main(void)
{
int primeNumRange;

printf("You wanna see the primes from: ");
if(scanf("%d", &primeNumRange) == false) /*it checks if the input is an int (scanf function returns 0 if the input is different of int)*/
{
printf("\n<<<Invalid Entry>>>\n\n");
return 0;
}

if(primeNumRange < 2)
{
printf("\n<<There isn't prime numbers from 1 to minus infinite>>\n\n");
return 0;
}

printf("\nPrime numbers from %d to 0:\n\n", primeNumRange);

for(int i = primeNumRange; i > 1; i--)
{
bool isPrime = testPrime(i);

if( isPrime && (i > 2) )
{
printf("%d, ", i);
}
else if (i == 2) //To put a dot "." at the end, and not a comma ", "
{
printf("%d.", i);
}
}

printf("\n\n");

return 0;
}

bool testPrime(int input)
{
for(int i = sqrt(input); i > 1; i--)
{
if ((input % i) == 0)
{
return false;
}
}

return true;
}

tiagodmota
Автор

Please give me the code of all Armstrong number 1 to n

AniketRana-ho
Автор

I just wanted to ask one thing that when ever I write 1. It says that 1 is a prime number.. which is not.. so how to fix this issue? Thanks for uploading this.. Please answer it..

aritora