Hello all, I am having a great degree of difficulty incorporating two functions
ID: 3940451 • Letter: H
Question
Hello all, I am having a great degree of difficulty incorporating two functions into my Assingment for Computer science, a function that splits the entered integer into seperate digits (if possible) and a function that checks to see if the entered number is a prime number or not, and then precedes to list the prime numbers between 1 and the entered integer. Here is my code so far. (Written in Codeblocks)
*/
#include
using namespace std;
void personAge(int ageInYears);
void currencyCalculation(int amountCents);
void hailstoneSequence(int num);
// void primeNumbers(int n);
//int countDigitsInInteger(int n)
//{
// int count =0;
// while(n>0)
// {
// count++;
// n=n/10;
// }
// return count;
//}
int main()
{
int integerForProgram;
cout << "A program that accepts an integer from the keyboard." << endl
<< "The program then calls the following functions :" << endl << endl
<< "1. Currency Calculation: The function computes and displays the number of quarters," << endl
<< " dimes, nickels, and pennies." << endl
<< "2. Person's Age: The function calculates and displays the following information:" << endl
<< " :- Number of months, days, hours, minutes, and seconds for the person." << endl
<< "3. Number Separation: Function separates and displays the numbers into its" << endl
<< " individual digits. The function displays each digit of the integer in English." << endl
<< "4. Prime Numbers: Function that indicates whether or not the integer is prime. The" << endl
<< " function also displays all prime numbers between one and that number." << endl
<< "5. Hailstone Sequence: Given any positive integer n, the hailstone sequence" << endl
<< " starting at n is obtained as follows." << endl
<< " You write a sequence of numbers, one after another. Start by writing n." << endl
<< " If n is even, then the next number is n/2. If n is odd, then the next" << endl
<< " number is 3n+1. Continue in this way until you write the number 1." << endl << endl
<< "Enter a Positive Integer > 0 -----> ";
cin >> integerForProgram;
if (integerForProgram < 0)
{
cout << endl << "Integer must be > 0" << endl << "Re-run the app & use a positive number > 0" << endl;
}
else
{
cout << endl;
currencyCalculation(integerForProgram);
cout << endl;
personAge(integerForProgram);
cout << endl;
hailstoneSequence(integerForProgram);
cout << endl;
//primeNumbers(integerForProgram);
cout << endl;
//
}
cout << endl << "Implemented by Brenden Ibrahim" << endl
<< "November 11th, 2016" << endl;
return 0;
}
void currencyCalculation(int amountCents)
{
int QUARTER = 25;
int DIME = 10;
int NICKEL = 5;
int numQuarters = 0;
int numDimes = 0;
int numNickels = 0;
int numPennies = 0;
cout << "Currency Calculation" << endl << endl
<< amountCents << " Cents is equivalent to:" << endl << endl;
while(amountCents >= QUARTER)
{
amountCents -= QUARTER;
numQuarters++;
}
while(amountCents >= DIME)
{
amountCents -= DIME;
numDimes++;
}
while(amountCents >= NICKEL)
{
amountCents -= NICKEL;
numNickels++;
}
numPennies = amountCents;
cout << numQuarters << " Quarters" << endl
<< numDimes << " Dimes" << endl
<< numNickels << " Nickels" << endl
<< numPennies << " Pennies" << endl;
cout << endl; //Formatting
}
void personAge(int ageInYears)
{
//Months
int monthsAge = ageInYears * 12;
//Days
int daysAge = ageInYears * 365;
//Hours
long int hoursAge = daysAge * 24;
//Minutes
long long int minutesAge = hoursAge * 60;
//Seconds
long long int secondsAge = minutesAge * 60;
cout << "Age Calculation" << endl << endl
<< "Assuming that you are " << ageInYears << " old, then" << endl << endl
<< "You are " << monthsAge << " months old" << endl
<< "You are " << daysAge << " days old" << endl
<< "You are " << hoursAge << " hours old" << endl
<< "You are " << minutesAge << " minutes old" << endl
<< "You are " << secondsAge << " seconds old" << endl;
cout << endl; //Formatting
}
void numberSeperation()
{
}
/*
void primeNumbers ()
{
int n;
for (int i=2; i {
bool prime=true;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
prime=false;
break;
}
}
if(prime) cout << i << " ";
}
}
*/
void hailstoneSequence (int num)
{
cout << "The sequence of hailstone starting at " << num << " is" << endl;
while (num != 1)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num << " ";
}
else
{
num = 3 * num + 1;
cout << num << " ";
}
}
cout << endl; //Formatting
}
Explanation / Answer
#include<iostream>
#include<conio.h>
using namespace std;
int isPrime(int);
int main(){
int num=2,j,prime;
int range;
cout<<"Enter Range for Prime Number:";
cin>>range;
range = isPrime(range);// calling isPrime function
if(range !=0){
cout<<"Given number is Prime ";
while(num<=range){
prime=1;// initially Prime count is 1
for(j=2;j<num;j++){
if(num%j==0){
prime=0;
}
}
if(prime){
cout<<num<<endl;
}
num++;
}
}
getch();
return 0;
}
int isPrime(int number)
{
int i;
for (i=2; i<number; i++)
{
if (number % i == 0)
{
return 0;
}
}
return number;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.