answersLogoWhite

0

How do you make a diamond using for loop in C plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

Here is the code to do it:

#include<stdio.h> main() { int n, c, k, space = 1; //Here we ask for the number of rows would be : printf("Enter number of rows\n"); scanf("%d",&n); space = n - 1; //This is the first half of the diamond for ( k = 1 ; k <= n ; k++ ) { for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= 2*k-1 ; c++) printf("*"); printf("\n"); } space = 1; //Here is the second half of the diamond for ( k = 1 ; k <= n - 1 ; k++ ) { for ( c = 1 ; c <= space; c++) printf(" "); space++; for ( c = 1 ; c <= 2*(n-k)-1 ; c++ ) printf("*"); printf("\n"); } return 0; }

Hope that helped :)

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make a diamond using for loop in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you alternate the input numbers using for loop in c plus plus?

Input a variable.


How do you convert meters to centimeters in c plus plus using the for loop construct?

It is unnecessary to use a for loop to convert meters to centimeters. Just multiply by 0.01.


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


How do you make C plus plus program to draw a straight line using for loop method?

void line(int length) { for(int i=0; i&lt;length; ++i) std::cout&lt;&lt;'_'; std::cout&lt;&lt;std::endl; }


How do you display stars in c plus plus using the if else structure and a for loop and you use void main?

It depends on what program you design really


C plus plus what do you do if im stuck in an infinite loop?

There are three ways out of a loop.1. Satisfy the loop ending condition2. Execute a break statement3. Terminate the programPerhaps you are not changing the value of the variable that is used in the loop ending condition. Perhaps you are using a variable, such as an unsigned int, decrementing it, and expecting it to go negative. Suggest you run the program in a debuger and step through the loop.


How do you make a c plus plus program using while loop to print a sum of digits?

Place the digits in an array (or vector) and use the loop to sum each digit to a running total initialised to zero. The following function demonstrates the loop: int sum_vector(std::vector&lt;int&gt;&amp; v) { int total=0; int i=0; while( i&lt;v.size() ) total+=v[i]; return total; }


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


What is CPU usage small programm c plus plus?

Example: int main (void) { LOOP: goto LOOP; }


How do you write a program to make asterisk isosceles triangle using for loop in c plus plus?

* ** *** **** simply use dis... { int x,y; for(x=1;x&lt;=4;x++) { for(y=1;y&lt;=x;y++) printf("*"); printf("\n"); } }


Does 'for loop' works in C plus plus?

In C++, a for loop is structured as follows: for( int index = 0; index &lt; 10; ++i ) { //do something }