Question

In: Computer Science

C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2....

C++ Code

Using all for loops

1. Print 5 lines with 1 asterisk per line

2. Print 5 asterisk on 1 line. Endl at the end of the line.

3. Ask for a positive # and print that many asterik on the next line.

4. Using 2 for loops one inside of another that only prints 1 asterik, print a hill shaped triangle:

Ex( Input a number a: 4

*

**

***

****

5. Change the for statements to print the triangle upside down.

Solutions

Expert Solution

1.

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
    for(int i=0;i<5;i++) // for loop to iterate 5 times
    cout<<"*"<<endl;  //print one asterik mark per line 
    return 0;
}

Output:-

2.

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
    for(int i=0;i<5;i++) // for loop to iterate 5 times
    cout<<"*";  //print five asterik mark per line 
    return 0;
}

Output:-

3.

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cout<<"Input a number"<<endl;
    cin>>n; //Ask for positive integer
    for(int i=0;i<n;i++) // for loop to iterate n times
    cout<<"*";  //print n asterik mark per line 
    return 0;
}

Output:-

4.

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cout<<"Input a number"<<endl;
    cin>>n; //Ask for positive integer
    for(int i=0;i<n;i++) // for loop to iterate n times
    {   for(int j=0;j<=i;j++) // second for loop to print pattern in hill shaped triangle
            cout<<"*";  
        cout<<endl;
    }
    return 0;
}

Output:-

5.

Code:-

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cout<<"Input a number"<<endl;
    cin>>n; //Ask for positive integer
    for(int i=0;i<n;i++) // for loop to iterate n times
    {   for(int j=n;j>i;j--) // second for loop to print the triangle upside down
            cout<<"*";  
        cout<<endl;
    }
    return 0;
}

Output:-


Related Solutions

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".
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
Using C++ code, write a program named q5.cpp to print the minimum of the sums x...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x + y^3 and x^3 + y, where x and y are input by a user via the keyboard.
The sequence 2, 4, 1, 3, 5 has three inversions (2,1), (4,1), (4,3). Using C++ Code...
The sequence 2, 4, 1, 3, 5 has three inversions (2,1), (4,1), (4,3). Using C++ Code an O(nlog(n)) algorithm to count the number of inversions. Use the Merge sort Algorithm, and use the template below: (Use the file below that to test) countInv.cpp ------------------------------------------------------------------------------------------------------------------------------- #include <vector> #include <algorithm> using namespace std; int mergeInv(vector<int>& nums, vector<int>& left, vector<int>& right) { // Your code here } int countInv(vector<int>&nums) { // Your code here } CountInv_test.cpp ------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <vector> using namespace...
The sequence 2, 4, 1, 3, 5 has three inversions (2,1), (4,1), (4,3). Using C++ Code...
The sequence 2, 4, 1, 3, 5 has three inversions (2,1), (4,1), (4,3). Using C++ Code an O(nlog(n)) algorithm to count the number of inversions. Use the Merge sort Algorithm, and use the template below: (Use the file below that to test) countInv.cpp ------------------------------------------------------------------------------------------------------------------------------- #include <vector> #include <algorithm> using namespace std; int mergeInv(vector<int>& nums, vector<int>& left, vector<int>& right) { // Your code here } int countInv(vector<int>&nums) { // Your code here } CountInv_test.cpp ------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <vector> using namespace...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT ALREADY BEEN POSTED, please use code from the assignment Copy-paste will be reported Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers...
Hw questions 1. Write a single C++ statement that will:      Print the string “C++” using the...
Hw questions 1. Write a single C++ statement that will:      Print the string “C++” using the variable:       string s = “Introduction to C++”. 2. Change the letter ‘c’ to ‘C in the following string:    Reprint the string with “C”  given the code and using the string: string greeting = “Introduction to c++” 3. Write the C++ statement or statements that will:    Print the answer for regular division of the real numbers a / b + c and print the answer for integer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT