Question

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 prime, your output should read:

333 = 1 x 333
333 = 3 x 111
333 = 9 x 37
333: NOT A PRIME Number

After each test allow the user to exit or input the next number to be tested.

Example interaction:

PRIME NUMBER CHECKER

Enter Value: 101

101 = 1 x 101
101: PRIME Number

Would you like to enter another value? (1=Yes, 0=No)> 1

Enter Value: 333

333 = 1 x 333
333 = 3 x 111
333 = 9 x 37
333: NOT A PRIME Number

Would you like to enter another value? (1=Yes, 0=No)> 0

My code is below. It giving more output than it should. Like it's compound and printing the same over and over. Like This:

Input

1768374681

1

5001

0

Your output

1768374681 = 1 X 1768374681

1768374681 = 3 X 589458227

1768374681 = 157 X 11263533

1768374681 = 471 X 3754511

1768374681 = 3754511 X 471

1768374681 = 11263533 X 157

1768374681 = 589458227 X 3

1768374681:NOT A PRIME Number

5001 = 1 X 5001

5001 = 3 X 1667

5001 = 1667 X 3

5001:NOT A PRIME Number

Your output does not contain - Correct format!!

1768374681 = 1 x 1768374681

1768374681 = 3 x 589458227

1768374681 = 157 x 11263533

1768374681 = 471 x 3754511

1768374681: NOT A PRIME Number

#include <stdio.h>

int main()

{

int n, i,x,w;
scanf("%d",&n);

x=prime(n);

if(x==0){

for(i=1;i<n;i++){

if(n%i==0){

printf("%d = %d X %d\n",n,i,n/i);

}

}

printf("%d:NOT A PRIME Number\n",n);

}

else if(x==1){

printf("%d = %d X %d\n",n,1,n);

printf("%d: PRIME Number\n",n);

}

do{


scanf("%d",&w);

switch(w){

case 1:


scanf("%d",&n);

x=prime(n);

if(x==0){

for(i=1;i<n;i++){

if(n%i==0){

printf("%d = %d X %d\n",n,i,n/i);

}

}

printf("%d:NOT A PRIME Number\n",n);

}

else if(x==1){

printf("%d = %d X %d\n",n,1,n);

printf("%d: PRIME Number\n",n);

}

break;

}

}while(w!=0);

  

return 0;

}

int prime(int n){

int i,flag=0;

for(i=2; i<=n/2; ++i)

{

if(n%i==0)

{

flag=1;

break;

}

}

if (flag==0)

return 1;

else

return 0;

}

Thank you!!

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

Program:

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

//function prototype
int prime(int n);

//main function
int main()
{
int n, m, i, x, w;

do
{
printf("PRIME NUMBER CHECKER\n\n");

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

x=prime(n);

if(x==0){
m = sqrt(n);
for(i=1;i<=m;i++)
{
if(n%i==0){
printf("%d = %d X %d\n",n,i,n/i);
}
}
printf("%d:NOT A PRIME Number\n",n);
}
else
{
printf("%d = %d X %d\n",n,1,n);

printf("%d: PRIME Number\n",n);
}

printf("\nWould you like to enter another value? (1=Yes, 0=No): ");
scanf("%d", &w);

}while(w!=0);

return 0;

}

//function to check prime number
int prime(int n)
{

int i, m;
m = sqrt(n);

for(i=2; i<=m; ++i)
{
if(n%i==0) return 0;
}

return 1;
}

Output:

Add a comment
Know the answer?
Add Answer to:
Help! i can't get the correct output for this question: Write a program that will read...
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
  • 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 =...

  • 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...

  • 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...

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

    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...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • 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. But I need to split this program in three files 1. bill.h = contains the...

    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. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • C program Classwork_3.3: Correct the program below that will read two numbers: number of drinks and...

    C program Classwork_3.3: Correct the program below that will read two numbers: number of drinks and number of sandwiches, and display the total bill calculated as: Total bill = Number of Drinks X 5.50 + Number of Sandwiches X 10.00 Answer the questions below. C-Program with error: Write the correct program here: #include<stdio.h> int main (void) floats numberofDrinks, numberofSandwiches; float totalBilling: printf("Enter number of Drinks \n"); scanf("%d",&numberofdrinks); printf("Enter number of Sandwiches\n") scanf("%f",&numberofSandwiches); totalbilling = numberofDrinks *5.50 + numberofsandwiches/10.00; printf("Total bill...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

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