Printing a triangle with 3 different symbols in C
I've been sent a exercise where I have to print to the console (in C) a triangle using three different symbols in the following way:
The program must ask how many rows the triangle will have.
The program will print the triangle in the following way:
1
From outside to inside, the first triangle will be formed of *, the one inside it will be -, the one inside again will be $ and then the other way around...
This is my actual code:
#include <stdio.h>
int num = 0; //Var to store the user input.
int blank; //Var to store number of blank spaces.
int main()
printf("Number of rows? ");
scanf("%d", &num);
printf("n");
blank = num - 1;
//For loop to print each row of the triangle.
for(int k = 1; k <= num; k++)
//For loop to print each blank space.
for(int j = 1; j <= blank; j++)
printf(" ");
blank--; //Decrease number of blank spaces.
//For loop to print each symbol of the triangle.
for(int r = 1; r <= 2 * k - 1; r++)
if(r % 2 == 0)
printf("-");
else if(r % 3 == 0)
printf("$");
else
printf("*");
printf("n");
blank = 1;
return 0;
And this is what it's printing:2
Can anyone give me a hand?
Thanks in advanced for the help!!
c
add a comment |
I've been sent a exercise where I have to print to the console (in C) a triangle using three different symbols in the following way:
The program must ask how many rows the triangle will have.
The program will print the triangle in the following way:
1
From outside to inside, the first triangle will be formed of *, the one inside it will be -, the one inside again will be $ and then the other way around...
This is my actual code:
#include <stdio.h>
int num = 0; //Var to store the user input.
int blank; //Var to store number of blank spaces.
int main()
printf("Number of rows? ");
scanf("%d", &num);
printf("n");
blank = num - 1;
//For loop to print each row of the triangle.
for(int k = 1; k <= num; k++)
//For loop to print each blank space.
for(int j = 1; j <= blank; j++)
printf(" ");
blank--; //Decrease number of blank spaces.
//For loop to print each symbol of the triangle.
for(int r = 1; r <= 2 * k - 1; r++)
if(r % 2 == 0)
printf("-");
else if(r % 3 == 0)
printf("$");
else
printf("*");
printf("n");
blank = 1;
return 0;
And this is what it's printing:2
Can anyone give me a hand?
Thanks in advanced for the help!!
c
5
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50
add a comment |
I've been sent a exercise where I have to print to the console (in C) a triangle using three different symbols in the following way:
The program must ask how many rows the triangle will have.
The program will print the triangle in the following way:
1
From outside to inside, the first triangle will be formed of *, the one inside it will be -, the one inside again will be $ and then the other way around...
This is my actual code:
#include <stdio.h>
int num = 0; //Var to store the user input.
int blank; //Var to store number of blank spaces.
int main()
printf("Number of rows? ");
scanf("%d", &num);
printf("n");
blank = num - 1;
//For loop to print each row of the triangle.
for(int k = 1; k <= num; k++)
//For loop to print each blank space.
for(int j = 1; j <= blank; j++)
printf(" ");
blank--; //Decrease number of blank spaces.
//For loop to print each symbol of the triangle.
for(int r = 1; r <= 2 * k - 1; r++)
if(r % 2 == 0)
printf("-");
else if(r % 3 == 0)
printf("$");
else
printf("*");
printf("n");
blank = 1;
return 0;
And this is what it's printing:2
Can anyone give me a hand?
Thanks in advanced for the help!!
c
I've been sent a exercise where I have to print to the console (in C) a triangle using three different symbols in the following way:
The program must ask how many rows the triangle will have.
The program will print the triangle in the following way:
1
From outside to inside, the first triangle will be formed of *, the one inside it will be -, the one inside again will be $ and then the other way around...
This is my actual code:
#include <stdio.h>
int num = 0; //Var to store the user input.
int blank; //Var to store number of blank spaces.
int main()
printf("Number of rows? ");
scanf("%d", &num);
printf("n");
blank = num - 1;
//For loop to print each row of the triangle.
for(int k = 1; k <= num; k++)
//For loop to print each blank space.
for(int j = 1; j <= blank; j++)
printf(" ");
blank--; //Decrease number of blank spaces.
//For loop to print each symbol of the triangle.
for(int r = 1; r <= 2 * k - 1; r++)
if(r % 2 == 0)
printf("-");
else if(r % 3 == 0)
printf("$");
else
printf("*");
printf("n");
blank = 1;
return 0;
And this is what it's printing:2
Can anyone give me a hand?
Thanks in advanced for the help!!
c
c
edited Nov 14 '18 at 13:48
LMF
asked Nov 14 '18 at 12:56
LMFLMF
35
35
5
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50
add a comment |
5
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50
5
5
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50
add a comment |
1 Answer
1
active
oldest
votes
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300780%2fprinting-a-triangle-with-3-different-symbols-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.
add a comment |
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.
add a comment |
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.
Try working out the pattern on paper first.
For example, the dollar sign starts two rows below the top of the outermost triangle. All subsequent dollar signs are each one column before or one column after the preceding dollar sign. In this way each character of a new line is remotely dependent on the very first character.
Once you understand the underlying pattern, you can start programming the loop.
answered Nov 14 '18 at 13:20
SzilanSzilan
2816
2816
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300780%2fprinting-a-triangle-with-3-different-symbols-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Please show us what you tried and how it failed. Please read How to Ask page first!!
– Sourav Ghosh
Nov 14 '18 at 12:59
Here.......where?
– Sourav Ghosh
Nov 14 '18 at 13:43
Sorry Sourav, I just posted the code and the result...
– LMF
Nov 14 '18 at 15:50