Question

any help! my program not working , there is an error but cannot solve it

i did this program after editing a previous program  and i tried to follow the following ;

1. Instead of printing the prime numbers from 2 to n, your program will print the first n prime numbers.

2. It will be an error if n is less than 1.

Exampl:

$ ./a 1
2

3 #include<stdio.h> 4 int p; // p is the global variable. 5 int prime(int i) // function check the prime number. 6 { int c; f

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h> int prime(int i) { int c; for (c 2; c < i; c++ ) if (i%c return 0; } / if we have reached here, it means c

int main() int n, i 2; int p 0; // to keep count of prime numbers found so far printf(\nEnter the number of prime numbers re

Code

#include <stdio.h>

int prime(int i)
{
   int c;
   for (c = 2; c < i; c++)
   {
       if (i%c == 0)
           return 0;
   }

   // if we have reached here, it means c==i and i is prime number
   return 1;
}

int main()
{
   int n, i = 2;
   int p = 0; // to keep count of prime numbers found so far
   printf("\nEnter the number of prime numbers required : ");
   scanf("%d", &n);
   if (n < 1)
   {
       printf("Error");
       return 0;
   }
  
   printf("\n\nFirst %d prime numbers are : ", n);

   // We will keep checking for each value of i starting from 3
   // whether it's prime or not.
   // If it's prime we will increase prime count
   // if prime count is reached n, we will stop
   while(i)
   {
       // initially i = 2
       if (prime(i))
       {
           printf("\n%d ", i);
           p++;
       }
       if (p == n)
           break;
       i++;
   }
   printf("\n");
   return 0;
}

Test Output

Enter the number of prime numbers required 8 First 8 prime numbers are 2 5 7 11 13 17 19 m n

Add a comment
Know the answer?
Add Answer to:
any help! my program not working , there is an error but cannot solve it i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • **how can I make my program break if 0 is entered by user as last number...

    **how can I make my program break if 0 is entered by user as last number not always 10 numbers output should be enter 10 numbers:1 2 0 numbers entered by user are: 1 2 Largest number: 2 **arrays can ONLY be used in the main function, other than the main function pointers should be used #include #define N 10 void max(int a[], int n, int *max); int main (void) { int a [N], i , big; printf("enter %d numbers:",N);...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Help! i can't get the correct output for this question: Write a program that will read...

    Help! i can't get the correct output for this question: Write a program that will read in an integer from the user and test to see whether or not it is prime. You are to determine if each integer read is prime or not, and output the result to the screen as shown below. If a number is prime, your output should read: 101 = 1 x 101 101: PRIME Number For a number that turns out not to be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT