Linux C: Double for loop for associative variables

Source: Internet
Author: User

To illustrate:

Like printing an inverted triangle.

* * * *

* * *

* *

*

The first loop is the number of rows, the second loop is the number of * printed per row, and as the number of rows changes, the number of printed * varies.

This is the double loop of associative variables. My approach is to first design the first layer variable I=4;i>=1;i--, This ensures that 4 rows of rows are printed correctly.

Then, design the second layer variable j=i,j>=1,j--, so that J can accompany the number of changes, and print a different number of stars. The third layer, tied to the second layer, is used for output spaces.

For example: The first line i=4,j=4, so print 4 * number, the second line i=3, then j=4, then print 3 * number, and so on, and so on, and finally printed in addition to the inverted star chart.

The complete procedure is as follows:

#include <stdio.h>
int main (void)
{
for (int i=4;i>=1;i--) {
for (int j=i;j>=1;j--) {
printf ("*");
}
printf ("\ n");
for (int k=i-1;k<4;k++) {
printf ("");
}
}
printf ("\ n");

return 0;
}
Pay attention to the judgment of the greater than and less than sign in the condition. In addition: If k<=4, then 2 spaces will be output in the second line, resulting in asymmetry.

Another example: Ten turn octal

/* Decimal to octal normal edition.
* Note the use of the two-tier for loop for this associative variable
*
*/
#include <stdio.h>
int main (void)
{
int number;
int jinzhi=8;
int weishu=5;
printf ("Enter a number (5wei):");
scanf ("%d", &number);
printf ("%o\n", number);
for (int i=weishu;i>=1;i--) {
int temp=number;
for (int j=i-1;j>=1;j--) {
Temp/=jinzhi;
}
Temp%=jinzhi;
printf ("%d", temp);

}
printf ("\ n");
return 0;
}

Linux C: Double for loop for associative variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.