Prime Number Program in C | C Language Tutorial

preview_player
Показать описание
Prime Number Program in C Programming
C Language Tutorial Videos

? Visit Our Website for Classroom Training:
? For Online Training:
--------------------------

? About NareshIT:

"Naresh IT is having 20+ years of experience in software training industry and the best Software Training Institute for online training, classroom training, weekend training, corporate training of Hadoop, Salesforce, AWS, DevOps, Spark, Data Science, Python, Tableau, RPA ,Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in USA,Hyderabad, Chennai and Vijayawada,Bangalore India which provides online training across all the locations

--------------------------

? Our Online Training Features:
1.Training with Real-Time Experts
2.Industry Specific Scenario’s
3.Flexible Timings
4.Soft Copy of Material
5. Share Videos of each and every session.

--------------------------

+1404-232-9879 or India: +918179191999

** Check The Below Links**
? Follow us on Linkedin:
Рекомендации по теме
Комментарии
Автор

I need to say that this is the third tutorial I have watched to do this program and this is the only one that worked. Thank you!

devermis
Автор

This video was very helpful, thank you for uploading it. For anyone interested, I tried writing the code out and it seems to be working perfectly fine. I will paste it down below so that it may assist others trying to write this program as well, I had put an extra semicolon on my first attempt that is why it wasn't working. I prefer this method because it is very easy to understand the logic, I've seen other methods where you have to use the loop from when i starts from 2 and ends at n/2 or something similar to that, and I find it easier to think about it this way.


#include <stdio.h>

int main()
{
int n, i, count = 0;
printf("Please enter a positive integer:");
scanf("%d", &n);

for(i = 1; i <= n; i++)
{
if((n % i) == 0)
{
count++;
}
}
if(count == 1)
printf("1 is neither a prime nor composite number.");
else
if(count == 2)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}

Vyantri
Автор

I can't thank you enough! This logic of two factors was great. My teacher didn't tell me.
Thank you! Love from Pakistan

huzaifaimran
Автор

I was struggling so much to understand and write code to find prime number . And watched so many videos and read so many websites. But this video helped me much . Thank you so much for creating this video with very good explanation with logic. I easily understood by watching only one time .
May Allah bless you.

shadhil
Автор

Here is the complete code:
#include<stdio.h>
main()
{
int i=1, n, count=0;
printf("enter the value of n: ");
scanf("%d", &n);
{
for(i=1;i<=n;i++)
{
if(n%i==0)
{ count++;
}
}
if(count==2)
printf("number %d is prime number", n);
else
printf("%d number is not prime", n);
}
}

goyaldeekshant
Автор

Thanks a lot, I'm studying computer engineering I mostly tend to prepare by learning online I rarely get such videos which consume less time and clear all the concepts. Thanks a lot

laartisto
Автор

Sir I received my offer letter today on TCS ninja being a civil engineer ... thanks for your effort

poojasreedas
Автор

if you start from 2, you don't need the count variable. If you do this, follow the condition n%i == 0 with a condition n==i. If n==i, then the number is prime, and if n!=i, then i = the root of n.

wake-digital
Автор

The best logic ever on youtube. Thank you, Sir!

soumyadeepchatterjee
Автор

We could also check the number is prime using a flag & break:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
int n, i, flag=0;

printf("Enter the number: ");
scanf("%d", &n);

for (i=2; i<n; i++)
{
if(n%i == 0)
{
flag=1;
break;
}
}
if(flag==0)
printf("Prime");
else
printf("Not prime");

return EXIT_SUCCESS;
}

thomasphilip
Автор

This program is alternatively ok but I have two queries regarding this,
1. what about the negative number?
2. unnecessary iteration will be done which may affect the efficiency of the program (suppose I input 10, after the division of 2 I have known that this is not prime but the loop will iterate till 10 unnecessarily)

MohdRashid-hicp
Автор

You are simply the best!!! keep up the great work!! I recommend everyone who want to learn programming!

danielnadar
Автор

Thank u so much for explaining so calmly ... I really admire the way u have explained so detailly ... ❤️❤️

rohinisubba
Автор

To test whether a number is prime or not is to divide it successively by all numbers from 2 to n/2, not n. If the remainder of any of these divisions is zero, the number is not a prime. If none of the remainders results in 0, it’s a prime number. Hope it was helpful.

SandipBhattacharya
Автор

I was having problem in logic of prime no after watching your video my problem has solved thanku so much sir for explaining it in very simple form

GauravKumar-cfmo
Автор

Sir u was doing one mistake in this program that is (i<=n)(sir every number has two factorials for example ( we are checking 5 is prime or not 5 has two factorials that is 5 is divisible by 1 and 5 so every number is equal to count =2)
So we change (I<n)

arjunsumanth
Автор

Excellent teaching sir. I have understood the sequence execution. It's great opportunity for computer students to learn c programming language through this channel. Thank you very very much. 🙏🙏🙏

vidya
Автор

सर किती सोप्प करून सांगता सगळं एकदा पहिल्यानंतर सहज समजत 👌👌

govind_rajewar
Автор

Thanks a lot! This is so far the best explanation I could find online!

hmlpsouvenirs
Автор

Best channel to understand programs....
thank you so much sir👍👍

rajeshkanade