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++)
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ####12 ###123 ##1234 #12345
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...
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT