1.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++)
2.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loopto compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total. Do NOT modify n. (c++)
3.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...whileloop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++)
4.
Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do.
Declare any variables that you need.(c++)
5.
Write a program that will predict the size of a population of
organisms. The program should ask the user for the starting number
of organisms, their average daily population increase (as a
percentage, expressed as a fraction in decimal form: for example
0.052 would mean a 5.2% increase each day), and the number of days
they will multiply. A loop should display the size of the
population for each day.
Input Validation.Do not accept a number less than 2 for the starting size of the population. If the user fails to satisfy this print a line with this message "The starting number of organisms must be at least 2.", display the prompt again and try to read the value. Similarly, do not accept a negative number for average daily population increase, using the message "The average daily population increase must be a positive value." and retrying. Finally, do not accept a number less than 1 for the number of days they will multiply and use the message "The number of days must be at least 1."(c++)
Thanks for the question, Please post 1 question at a time.
I have solved the first 4, Please post question 5 in a separate post.
As per Chegg policy, I am suppose to answer only the first 4 questions.
Here is the code in C++, do comment for any further help.
thank you !
===================================================================
#include<iostream>
#include<fstream> // for wriitng data to file
using namespace std;
int main(){
int n=4;
int k;
int total;
// Problem 1
total=0;
for(k=1;k<=n;k++){
total+= k*k*k;
}
cout<<"Problem 1 total = "<<total<<endl;
//Problem 2
k=1;
total=0;
while(k<=4){
total+=k*k*k;
k+=1;
}
cout<<"Problem 2 total = "<<total<<endl;
//Probem 3
k=1;
total=0;
do{
total+=k*k*k;
k+=1;
}while(k<=4);
cout<<"Problem 3 total = "<<total<<endl;
// Problem 4
char filename[]="outdata.txt";
char username[51];
int age;
cout<<"Enter user name: ";
cin.getline(username,50,'\n');
cout<<"Enter age: ";
cin>>age;
ofstream outfile(filename);
if(outfile.is_open()){
outfile<<username<<", "<<age<<endl;
outfile.close();
cout<<"File updated successully\n";
}else{
cout<<"Unable to write data to file "<<filename<<endl;
}
}
===================================================================
1.Given an int variable n that has been initialized to a positive value and, in addition,...
CODE MUST BE IN C 1. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have alreadybeen declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and...
java : Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
anslate Assume that number is an int variable that has already been initialized. Write a statement that prints out the value of number in a field of 10 positions (right-justified within that field). Hint: Assume #include <iomanip> is included in the program. В І ЦА HTML Editor I 5 7 7 7 7 x x = = 1:12pt Paragraf O words 18 ctv A
5.11: Population Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), and the number of days they will multiply. A loop should display the size of the population for each day. Input Validation.Do not accept a number less than...
Given an int variable k that has already been declared, use a while loop to print a single line consisting of 97 asterisks. Use no variables other than k. PLEASE ANSWER IN C
Given an int variable n that has already been declared, write some code in C that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.
public static List sumOfDistinctCubes(int n) Determine whether the given positive integer n can be expressed as a sum of cubes of positive integers greater than zero so that all these integers are distinct. For example, the integer n = 1456 can be expressed as sum 11 3 + 5 3 . This method should return the list of these distinct integers as a list [11, 5] with the elements given in descending order. If n cannot be broken into a...
Assume you have already declared and initialized a variable named number as seen here: number = 1 This list contains 10 potential assignment statements that will give the variable named number a value of 4. Select all the LEGAL ways to do this. Assume that these other variables also exist and have been initialized as seen here: value = 4 count = 2 Group of answer choices number = count + number * 2 4 = number number == count...
python
Canvas 10 pts Question 26 Assume a variable score that has already been initialized, write one if/elif/else statement that will: • Output the message "Test was not taken if the score variable is equal to 0. • Output the error message "Error: Score is not valid" if the value stored in the score variable is outside the range of 0-100 (i.e., not in between 0 and 100; less than 0 or greater than 100). . If the value stored...
Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. For example, a population might begin with two organisms, have an average daily increase of 50 percent, and will be allowed to multiply for seven days. The program should use a loop to display the size of the population for each...