I'm having some problems with a program I'm writing here.
It's supposed to print the next 10 prime numbers of given a value -- of course, it's not working. My logic is at fault here.
For example, if the program reads the number:
2
it's supposed to print:
3, 5, 7, 11, 13, 17, 19, 23, 29, 31
Code:
#include <stdio.h>
int main() {
int n, i, count, primenumber = 1; // primenumber is a flag
printf("Insert a number:\n");
scanf("%d", &n);
for (count = 0, n++; count < 10; n++, count++ ) {
for (i = 2; i < n; i++) {
if (n % i == 0) {
primenumber = 0;
break;
}
}
if (primenumber)
printf("%d\n", n);
}
return 0;
}
I'd be very grateful if someone were to solve this problem.
Thanks.
==UPDATE==
I did it!
The crucial change was with the primenumber flag. I inserted it inside the while, always set to 1. If set outside the while, the flag never resets and further tests won't occur -- depending on the number you give when prompted.
Here's the updated, functional code:
#include <stdio.h>
int main() {
int n, i, count = 0, primenumber;
printf("Insert a number: ");
scanf("%d", &n);
n++; // we do not want to print the prompted number
while (count < 10) {
primenumber = 1; // primenumber is set as flag
for (i = 2; i < n / 2; i++) {
if (n % i == 0) {
primenumber = 0;
break;
}
}
if (primenumber) {
printf("%d\n", n);
count++; // increment count only when prime
}
n++;
}
return 0;
}
I decided not to delete this post. I'm sure someone, someday, will find it useful.
Split your code a little arrange a bit more and present it
int isPrime ( int n )
{
if (n <= 1) return 0; // zero and one are not prime
unsigned int i;
for (i=2; i*i<=n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main() {
int n, count; // primenumber is a flag
printf("Insert a number:\n");
scanf("%d", &n);
count = 0 ;
n++ ;
for ( ; count < 10; n++ ) {
if (isPrime(n)) { // if its Prime, print and increase count
printf("%d\n", n);
count ++;
}
// Check next number until we get all our numbers
}
return 0; // All Done
}
This is your modified working code.
In each step you should re initializing primenumber=1 otherwise it gives only one number. Also count should be increases when you find a prime otherwise not.
#include <stdio.h>
int main() {
int n, i, count, primenumber = 1; // primenumber is a flag
printf("Insert a number:\n");
scanf("%d", &n);
for (count = 0, n++; count < 10; n++ ) {
primenumber = 1;
for (i = 2; i < n; i++) {
if (n % i == 0) {
primenumber = 0;
break;
}
}
if (primenumber){
count++;
printf("%d\n", n);
}
}
return 0;
}
Here is a solution which will reduce the processing time, generally used for larger values of n:
#include <stdio.h>
#include <math.h>
int main() {
int n, i, count= 1, primenumber = 1, root; // primenumber is a flag
printf("Insert a number:\n");
scanf("%d", &n);
n++;
while(count!=11)
{
root= sqrt(n);//You only need to check for range 2 to square root of number. This is the key ingredient of the code to reduce time complexity
primenumber = 1;
for(i=2; i<= root; i++)
{
if(n%i==0)//As soon as it finds a number which perfectly divides it, break from loop checking n's nature
{
primenumber = 0;
break;
}
}
if(primenumber==1)
{
printf("%d \t", n);
count++;
}
n++;
}
}
Related
#include <stdio.h>
#include <math.h>
#define MAX_SIZE 10000
int main(void)
{
int a[MAX_SIZE];
int N;
int L; /* the current size of the list */
/* read in the upper limit. Keep reading until
a valid number between 3 and the maximum that
can be handled by the array is entered */
double b[10000];
int j, i;
L = 0;
printf("Enter the upper limit:\n");
do {
scanf("%d", &N);
} while (N<3 || N>MAX_SIZE+2);
int prime;
for (j = 1; j < N; j++)
{
prime = 1;
for (i = 2; i < j; i++)
{
if (j % i == 0)
{
prime = 0;
break;
}
}
if (prime)
{
a[i] = j;
L++;
}
}
/* write out the result - DO NOT CHANGE THIS */
for(i=0;i<L;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Program needs to take an integer, calculate primes below that integer, print that list of primes.
I think my problem is related to the loops.
The program is calculating the primes but listing 0 if the number previously there isnt prime eg a[4] is now printing as 0
Any help is appreciated.
thanks.
Is this what you were trying to implement?
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
int primes[MAX_SIZE];
int primes_found = 0;
int limit = 0;
while (limit < 3)
{
printf("Enter the upper limit:\n");
scanf("%d", &limit);
}
for (int candidate = 2; candidate <= limit && primes_found < MAX_SIZE; candidate++)
{
int divisor = 2;
int is_prime = 1;
while(is_prime && divisor < candidate)
is_prime = candidate % divisor++ != 0;
if (is_prime)
primes[primes_found++] = candidate;
}
for (int i = 0 ; i < primes_found ; i++)
printf("%d ", primes[i]);
printf("\n");
return 0;
}
I am new to programming, I am trying to write a program that lets the user input numbers ranging from 0 to 1000, and the maximum number the user can input is 100. The numbers in the array don't have to be in order, and the program ends when the user inputs a negative number. After that, the program should determine which number occurs the most and the frequency of that occurrence.
I have written a similar code but not for this type of problem the code below showcases what I mean by similar code and any help would be appreciated
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char again;
do {
srand(time(0));
int myNumbers[10];
int i, n, findnum, time, num;
n = 10;
for (i = 0; i < n; i++) {
myNumbers[i] = rand() % 10 + 1;
}
for (i = 0; i < n; i++) {
printf("elements %d\n", myNumbers[i]);
}
printf("Enter number to find Occurrence: ");
scanf("%d", &findnum);
time = 0;
for (i = 0; i < n; i++) {
if (myNumbers[i]==findnum)
time++;
}
if (findnum>0) {
printf("Occurrence of %d is: %d times\n",findnum,time);
} else {
printf("The number %d is not present in the array\n",num);
}
do {
printf("Shall we play again (y/n)?: ");
while(getchar()!='\n');
scanf("%c", &again);
}
while(again !='y' && again !='n');
}
while(again =='y');
}
You will need a second array to count the frequencies. Worst case, the user entered unique numbers, so the second array should be as large as myNumbers. The array will hold two values: the number, and its count:
int myNumbers[10];
int myCount [10][2] = {0};
int n= 10;
You remember the first entry of myCount that is available:
int m= 0;
You cycle over all numbers:
for (i = 0; i < n; i++){
For each number, you check if it is already in the myCount and if yes, increment the count and then exit the loop:
for (j = 0; j < m; j++){
if (myCount[j][0] == myNumbers[i]){
myCount[j][1]++;
break;
}
}
If the number was not found, you add it:
if (j == m) {
myCount[m][0] = myNumbers[i];
myCount[m][1] = 1;
m++;
}
}
Now you can search the array for the number with the highest count.
Integrated the code is:
int myNumbers[10];
int myCount [10][2] = {0};
int n= 10;
int m= 0;
/* now fist read the input */
for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
if (myCount[j][0] == myNumbers[i]){
myCount[j][1]++;
break;
}
}
if (j == m) {
myCount[m][0] = myNumbers[i];
myCount[m][1] = 1;
m++;
}
}
To do: search the array for the number with the highest count.
This is the code, this code is not working. For loop needs to be used for this program. I need help to make this work.
Program prints same statement for different numbers. Kindly debug this and help me to understand the concept.
#include<stdio.h>
int main()
{
int n,i,sum=0,sum1=0,rem;
printf("enter values\n");
scanf("%d",&n);
for(i=n;i<=n;)
{
rem=n%10;
if(rem%2 == 0)
{
sum=sum+rem;
}
else
{
sum1=sum1+rem;
}
n=n/10;
}
if(sum==sum1)
printf("I will win the Card Game");
else
printf("I will not win the Card Game");
return 0;
}
#include<stdio.h>
int main()
{
int n,i,sum=0,sum1=0,rem;
printf("enter values\n");
scanf("%d",&n);
for(i = 0; i < n;)
{
rem=n%10;
if(rem%2 == 0)
{
sum=sum+rem;
}
else
{
sum1=sum1+rem;
}
n=n/10;
}
if(sum==sum1)
printf("I will win the Card Game\n");
else
printf("I will not win the Card Game\n");
return 0;
}
Try this:
Use Array to get multiple values and compare them like below.
#include<stdio.h>
int main()
{
int n,*arr,i,sum=0,sum1=0;
printf("how many numbers\n");
scanf("%d",&n);
printf("enter values");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]%2 == 0)
sum=sum+1;
else
sum1=sum1+1;
}
if(sum==sum1)
printf("I will win the Card Game");
else
printf("I will not win the Card Game");
return 0;
}
Calculate the length of user's number, and you will know how much numbers you got with this loop:
temp = n;
length = 0;
while (temp > 0) {
length++;
temp /= 10;
}
Then insert this code before your main loop, and in your main 'for' loop head you can use full 'for' syntax:
for(i = 0; i < length; i++)
Put all together:
int main()
{
int n, i, sum = 0, sum1 = 0, rem, length, temp;
printf("enter values\n");
scanf("%d",&n);
temp = n; // Temp variable to calculate digits' count
length = 0; // Digits' counter
while (temp > 0) { // Loop to calculate digits' count
length++; // increase counter
temp /= 10; // remove the unity digit, untill the number equal to 0.
}
for(i = 0; i < length; i++) // go through all digits
{
rem = n % 10;
if(rem % 2 == 0)
{
sum = sum + rem;
}
else
{
sum1 = sum1 + rem;
}
n = n / 10;
}
if(sum == sum1)
printf("I will win the Card Game, sum: %d", sum);
else
printf("I will not win the Card Game, sum: %d, sum1: %d", sum, sum1);
return 0;
}
I have written some code to ask the user for n, then print the prime numbers up to n. However when I use it, i.e with 10, it only prints the non-prime numbers
/* Asks for the amount of prime numbers you would like to print, then prints them */
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
check = 0;
for (j = 2; j < i ; j++) {
if (i % j == 0) {
check = 1;
if (check == 1) {
printf("%d\n", i);
}
}
}
}
return 0;
}
How many prime numbers would you like to print? 10
4
6
6
8
8
9
10
10
I've tried everything but I think I am missing something really trivial!
This is how it should be:
for (i = 2; i <= n; i++)
{
check = 0;
for (j = 2; j < i ; j++)
{
if (i % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
printf("%d\n", i);
}
}
Also, in the inner loop you don't have to divide the number till j < i. You don't have to go beyond i/2.
As Weather Vane said, the mod operator % returns 0 if i is exactly divisible by j and if this is true then the number is not prime. Your conditional statement is backwards.
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++)
{
check = 0;
for (j = 2; j < i ; j++)
{
if (i % j == 0)
{
check = 1;
break;
}
}
if (check == 0)
{
printf("%d\n", i);
}
}
return 0;
}
How many prime numbers would you like to print? 10
2
3
5
7
Several problems.
First, when you set check = 1, that means that i divides evenly, so n is not prime, so you shouldn't print it. You should be printing the number when check == 0.
Second, you're printing each time through the inner loop. You should test check at the end of the loop, to ensure that none of the numbers divided it.
As an improvement, there's no need to keep checking once you find one number that divides evenly. So you can break out of the inner loop as soon as you set check = 1.
#include <stdio.h>
int main(void)
{
int n, i, j, check;
printf("How many prime numbers would you like to print? ");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
check = 0;
for (j = 2; j < i ; j++) {
if (i % j == 0) {
check = 1;
break;
}
}
if (check == 0) {
printf("%d\n", i);
}
}
return 0;
}
try looking at this code
#include <stdio.h>
int IsPrime(int num)
{
int i = 2;
for (i = 2; i < num; i++) if (num % i == 0) return 0;
return 1;
}
int main(void)
{
int n, i;
char *nStr = (char*)malloc(10);
printf("How many prime numbers would you like to print? ");
fgets(nStr, 9, stdin);
n = atoi(nStr);
for (i = 1; i <= n; i++) if (IsPrime(i)) printf("%d\n", i);
getchar();
return 0;
}
and about your code, you should print the number only if check remains 0.
This application will receive a number n. After receiving this number, the program has to show the n-th prime in the list of primes. For example, if the user enters 3, the program is supposed to display 5, because 5 is the third prime starting at 2. I know that something is wrong with my code but I don't know where the problem is and how I can fix it.
#include <stdio.h>
int main() {
int n, i, flag, prime;
int counter = 1;
scanf("%d", &n);
if (n == 1) prime = 2;
else
do{
prime = 3;
for (i = 2; i < prime; i++) {
flag = 1;
if (prime % i == 0) {
flag = 0;
}
}
if (flag == 1)
counter++;
prime++;
} while (counter != n);
if (counter == n)
printf("%d\n", prime);
return 0;
}
Fix sample of remains of your policy like this :
#include<stdio.h>
int main(void){
int n, i, flag, prime;
int counter = 1;
scanf("%d", &n);
if (n == 1)
prime = 2;
else {
prime = 1;
do{
prime += 2;
flag = 1;
for (i = 3; i < prime; i+=2){
if (prime % i == 0) {
flag = 0;
break;
}
}
if(flag == 1)
counter++;
} while (counter != n);
}
printf("%d\n", prime);
return 0;
}
You are resetting flag to 1 every time through the loop, so 'flag' will only tell you if prime is divisible by "prime-1", which of course it never is.