C program to print next five prime numbers - c

I have a program that prints five prime numbers within a user input range(m,n).
My problem is i want to print numbers greater than m. and print only next five numbers. I don't want to use upper limit.
How can I do it so?
#include <stdio.h>
#include <conio.h>
int main()
{
int m,n,i,j,k,flag;
printf("\nEnter The Lower Limit: ");
scanf("%d",&m);
printf("\nEnter The Upper Limit: ");
scanf("%d",&n);
printf("\nPrime Numbers Between %d & %d Are:\n",m,n);
for(i=m ; i<=n ; i++)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
printf("%3d \n",i);
}
}

Why not simply count the number of primes you have printed?
int count = 0;
:
for(i=m ; (i<=n) && (count<5) ; i++)
:
if(flag)
{
printf("%3d \n",i);
count++;
}
PS, using longer names than single characters will help make your program more understandable.

Whether you like it or not, you have a higher limit which is set by the data type you use -> int, in your case. Imagine you want the first 5 prime numbers larger than (MAXINT- 10)...you get the point, but let's assume you are not concerned about corner-cases.
int count=0;
int i=m;
int k;
while (count<5)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
{
printf("%3d \n",i);
count++;
}
i++;
}

Related

how to sort prime factors into array and display in specific way using c code

Hi im trying to create a code using c language where you enter in a number it checks for its prime factors and then prints these factors out.
only there is a specific way that i am required to print these numbers
The prime factors of 56 are:
{2,7}
this is the required output and i can only get to this:
The prime factors of 56 are:
2,7,
ive been suggested to store the outputs into an array and then manipulate the array to delete the comma at the end and add the {} but im not sure how to do this.
Here is my code for reference:
#include<stdio.h> int main(){
int i, j, number, p;
printf("Enter a Number to find Prime Factors:");
scanf("%d", &number);
if(number < 2)
{
printf("Invalid input\n");
}else {
printf("The prime factors of %d are:\n ", number);
for(i=2; i<=number; i++)
{
/* Check 'i' for factor of num */
if(number%i==0)
{
/* Check 'i' for Prime */
p=1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
p = 0;
break;
}
}
/* If 'i' is Prime number and factor of num */
if(p==1)
{
printf("%d,", i);
}
}
}
First and foremost, you should indent your code consistently so that it is easy to read. In the following, I show you one way of doing what you want without using any intermediary array variables. The key points are that you need to distinguish the first number that you will output from the others, and you need to add curly braces unconditionally at the begin and the end.
#include<stdio.h>
int main() {
int i, j, number, p;
int first_factor = 1; /* Are we printing the first factor? */
printf("Enter a Number to find Prime Factors:");
scanf("%d", &number);
if(number < 2)
{
printf("Invalid input\n");
} else {
/* I removed the line break in the printf below */
printf("The prime factors of %d are: ", number);
putchar('{'); /* Inconditionnaly prints '{' at the beginning of the answer */
for(i=2; i<=number; i++)
{
/* Check 'i' for factor of num */
if(number%i==0)
{
/* Check 'i' for Prime */
p=1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
p = 0;
break;
}
}
/* If 'i' is Prime number and factor of num */
if(p==1)
{
if (first_factor == 1)
{
/* It is the first factor, prints without a comma */
printf("%d", i);
first_factor = 0; /* The other factors are not the first one */
} else {
printf(",%d", i);
}
}
}
}
puts("}"); /* Inconditionnaly prints a '}' followed by a line-break */
}
}

(Lotto 6/49 simulation) Calculate how many draws will take place until a user-entered combination hits the jackpot

I have my code and data for my Lotto 6/49 simulation. I just don't know how to create a simple mathematical expression to calculate how many draws will take place until the user hits the jackpot
Code:
#include <stdio.h>
#include <stdlib.h> //need this library to use srand and rand without warnings
#include <time.h> //need this library to use time function
int main(void) {
int i, j, k , temp, count1=0;
unsigned int count2=1;
int select[6];
int jackpot[6];
printf("Welcome to Lotto 6/49! The 6 numbers you choose must match the 6 randomly chosen numbers in the lottery in order to win the jackpot. Select your 6 numbers!\n\n");
for(i=0; i<6; i++)
{
printf("Enter a number between 1-49: ");
scanf("%d", &select[i]);
}
srand(time(NULL));
for(;;){
for(i=0; i!= 6 ; ) //This loop produces the winning number combination
{
temp= (rand() %49)+1; //produce a random number between 1 and 49
for( k =0 ; k<i && (temp!=jackpot[k]) ; k++); //make sure we do not have duplicate
if (k==i){ //only write to the array if we do not have a duplicate
jackpot[i] = temp;
i++; //only increment the loop if we write a value to the array
}
}
printf("\nThe winning numbers are: "); //displays the winning numbers
for(i=0;i<6;i++)
printf("\t%d\t",jackpot[i]);
printf("\n");
// This loop will check the user array with the jackpot array to determine if it is a winning combination
for(i=0; i<6 && i ==count1; i++){ //if we do not match the first element, we skip the rest
for(j=0; j<6 ; j++)
{
if(select[i]==jackpot[j]){
count1+=1; // only increment if we match
}
}
}
if(count1==6){ //all 6 numbers matched
printf("\n\nYou win the JACKPOT! Congratulations! \nIt took you %d draws to win", count2);
break;
}
else{
count2+=1;
count1=0; //reset match count
}
}
return 0;
}
Data (number range limited to 8 numbers for easier recording, eg temp= (rand() %8)+1 instead of temp= (rand() %49)+1 # line 19.)
Count1 = user input matched
Count2 = # of draws
↑
I have no clue how to calculate this

segmentation fault (core dumped) gcc ubuntu

I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem?

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem??
#include <stdio.h>
int main(void)
{
int number;
char flag = 0;
printf("Please enter the number:");
scanf("%d",&number);
for (int i = 1; i <= number; i++) {
if (number %i == 0) {
flag = 1;
break;
}
}
if (number==1)
printf("%d neither prime nor not prime", number);
if (flag==1)
printf("%d is not prime",number);
else
printf("%d is prime",number);
return 0;
}
Take a look at the loop condition here:
for(int i=1;i<=number;i++)
With <=, i goes up to number. So the last check will be if(number%number==0), which is always true: your program says that 5 is not a prime number because 5 divided by 5 has remainder 0. The same applies to dividing the number by 1 (which also results in no remainder), so this check should start at 2. This line should be:
for(int i=2;i<number;i++)
Typically i only has to go up to sqrt(number) because no two numbers bigger than the root of number multiplied will result in number.
Also, if the number entered is 1, you get two of the three possible outputs instead of just the first one. To fix this, put an else before the if (flag == 1).
edit to work successfully
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
for(int i=2;i<number;i++)
{
if(number%i==0)
{
flag=1;
break;
}
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
else if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}
Just check reminder either it is 0 or 1, if you divide any number in the end it will give you either 0 or 1 in reminder.If reminder is 1 so number is prime or 0 number is not prime.
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
if((number>2)&&(number%2==0))
{
flag=1;
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}

Can't seem to find why my Prime Number Program doesn't work properly after reprompt for number

Im having trouble with my prime number section part of my program. When I compile and run my program, the prime numbers print out fine for the first number, but when reprompted for another number. It usually prints out either no numbers or it cuts off the prime numbers from a certain point down. Im new to coding and this forum so I'm sorry about any formatting issues in my post.
#include <stdio.h>
#include <math.h>
int main(void)
{
int number, n=1;
long factorial=1;
int a=1, b=0,c;
int q=2, r=2, w=0;
int prime, count;
printf("Enter a Number:\n");
scanf("\n%d", &number);
while(number!=1000)
{
if(number<1||number>1000)
printf("Input is Invalid\n");
else
{
if(number==1000)
printf("Goodbye\n");
if(number<15)
{
factorial=number;
n=number-1;
while(n>=1)
{
factorial=factorial*n;
n--;
}
printf("The Factorial of %d is: %ld\n", number, factorial);
}
c=a+b;
printf("Fibonacci Sequence up to %d\n ", number);
while(c<number&&a+b<number)
{
c=a+b;
a=b;
b=c;
n++;
printf("%d\t", c);
count=n;
if(count%10==0)
printf("\n");
}
printf("\nTotal:%d\n", n);
printf("\nPrime numbers up to %d:\n", number);
while(q<=number)
{
prime=0;
for(r=2;r<q;++r)
{
if(q%r==0)
{
prime=1;
}
}
if(prime==0)
{
printf("%d\t", r);
w++;
}
q++;
}
count=r;
if(count%10==0)
printf("\n");
printf("\nTotal:%d\n", w);
}
printf("\nEnter a Number:\n");
scanf("\n%d", &number);
a=1;
b=0;
}
return(0);
}
You are never resetting your variable q, which is the control variable for your prime number loop. It is best practice to create all of your variables which need to be reset inside your loop. Either reset q at the beginning/end of your loop, or create q at the beginning of your outer loop and set it to 2.
Like this:
while(number != 1000) {
int q = 2;
//other assignments below which need to be reset
...
//all other code below
...
}

Resources