Question

In: Computer Science

1.Divisors flow chart using modulo operator (%) to print out all the divisors of a user...

1.Divisors

flow chart using modulo operator (%) to print out all the divisors of a user entered number. Your program should prompt the user to enter a positive number or 0 to end. Using a loop variable that starts at 1, your program should print out all the divisors of the entered number plus the number of printed divisors and their sum.

For example:
This program identifies and displays divisors of a given number.
Developed as an IPC144 project.

Enter a positive number. Enter 0 to end: 15
1 3 5 15
4 divisors
sum of divisors: 24

Enter a positive number. Enter 0 to end: 36
1 2 3 4 6 9 12 18 36
9 divisors
sum of divisors: 91

Enter a positive number. Enter 0 to end: 43
1 43
2 divisors
sum of divisors: 44
Enter a positive number. Enter 0 to end: 0
Thanks and bye!

  1. Divisor counter

Using what you did in part 1,to develop a function called
unsigned divisorCount (unsingned num)

That gets an integer number as an input parameter and returns the number of divisors of that number. For example, if you send 15 to the function, it will return 4 as 15 has 4 divisors: 1, 3, 5, 15
The function will return 2 for 17 because 17 only has two divisors


3.Prime number

Using the function you developed in part 2, write a
int isPrime(unsigned num)

That gets a number as a paremeter and returns 1 if the number is prome and returns 0 if the number is not prome.

Use this function to write a code that gets a number from the user and prints all the prime numbers smaller and equal to that number

A sample run of your code should look like:

Please enter a positive integer number (0 to end): 12 Primes before 12 are: 2 3 5 7 11

5 Prime numbers smaller than 12

Please enter a positive integer number (0 to end): 7 Factors of 7 are: 2 3 5 7

4 prime numbers smaller than 7

Please enter a positive integer number (0 to end): 0

Thanks and have a good day!

Hint: This question is divide to three-part each of the parts is individual and the answer for each of the part will need to link together with a flowchart and c code

Solutions

Expert Solution

Flow chart to find divisors :

  • read n
  • if n is 0, end the program
  • set i = 1, sum = 0, count = 1
  • loop until i<=n
    • if n%1 ==0, print i, add i to sum, increment count
    • increment i
  • print sum and count

C function to find divisors count:

unsigned int divisorCount(unsigned int n){
int count = 0;
for(undigned int i = 1; i<=n; i++){
if(n%i==0)
count += 1;
}
return count
}

function for prime number check:

int isPrime(unsigned int n){
if(divisorCount(n)>2)
return 0;
return 1;
}

complete program that prints prime number less than n:

  • get a number n from user.
  • set count = 0
  • loop for i = 2 to count
    • if isprime(i) then print i and increment count
  • print count

#include <stdio.h>

unsigned int divisorCount(unsigned int n){
int count = 0;
for(unsigned int i = 1; i<=n; i++){
if(n%i==0)
count += 1;
}
return count;
}

int isPrime(unsigned int n){
if(divisorCount(n)>2)
return 0;
return 1;
}

int main(){
unsigned int n;
int count;
printf("Please enter a positive integer number(0 to end: ");
scanf("%u",&n);
while(n!=0){
count = 0;
printf("Prime number before %u are", n);
for(int i =2;i<=n;i++){
if(isPrime(i)){
printf("%d ", i);
count++;
}
}
printf("\n%d prime numbers smaller than %u\n", count, n);
printf("Please enter a positive integer number(0 to end: ");
scanf("%u",&n);
}
}

output:


Related Solutions

Read a number N and print all its divisors.
Read a number N and print all its divisors.
Write a program that will print out “W” using a character that a user provides. use...
Write a program that will print out “W” using a character that a user provides. use for loop statement. in java please
using Emulator Write an Assembly program that will produce all divisors for a 1-digit decimal number....
using Emulator Write an Assembly program that will produce all divisors for a 1-digit decimal number. For example, if the number is 6, then the outputs will be 1,2,3,6 which are the divisors of 6. show me the output
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write in javaScript: User input 5 words in one input, and print out the longest word.
Write in javaScript: User input 5 words in one input, and print out the longest word.
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345...
Using Eclipse (Pyramid) Print out the following pyramid using nested loop. 1 12 123 1234 12345 1) the source code (.java file), and 2) the screenshot of running results of each question.
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or...
Objectives: Write a program which reads User Input using Scanner Print formatted output using printf or DecimalFormat Practice programming simple mathematical calculations Instructions: Samwise Gamgee has finally decided to submit his expense report for all of his adventures regarding his travels to Mordor. Part of those expenses are his stay at the Prancing Pony Inn located in Bree. You are to write a simple Java program which will generate an Invoice for his stay at the Inn. Your program should...
Describe flow chart for a cola industry. Elaborate all steps involved in the preparation of a...
Describe flow chart for a cola industry. Elaborate all steps involved in the preparation of a soda industry.
Let p= 11 and 13. (a) Determine all the squares modulo p in (Z/pZ)∗. (b) Using...
Let p= 11 and 13. (a) Determine all the squares modulo p in (Z/pZ)∗. (b) Using this determine the value of the Legendre symbol(a/p)for all a∈(Z/pZ)∗. (c) For all a∈(Z/pZ)∗, compute a^((p−1)/2) and confirm that a^((p−1)/2)=(a/p).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT