Solved

Making a Right Angle with C Program...

Posted on 2007-12-06
10
2,577 Views
Last Modified: 2012-06-22
Hi friends !

I want to get this output of a 90 degree tringle by using 'C' Language only. I am beginner in  programming. Please see the desired output:

*
**
***
****
*****
******
*******
********

(The lines occupied by the triangle will depend on an input 'n' provided by the user.

And in the above picture, the size of the triangle depends on the height of the triange. I want user to input a number (n), which will determine, now many lines the trinagle will cover on the screen (Height).

I wrote this code...

#include<stdio.h>
#include<conio.h>

void main()

{

int i, j, n;

clrscr();

printf("Please enter the number:");
scanf("%d", &n);

i=1;
j=1;

for (i=1; i<=n; i++)
{
  for (j=1; j<=i; j++)
  {
  printf("*\t\n");
  }
}

getch();

}


When I compile and run it, it gives no error but it gives output like this...

for input n = 2, output is

*
*
*

for input n = 3, output is

*
*
*
*
*

likewise for input n = 5, 15 lines are occupied.

Please help me in getting my desired output.

Thanks
Hemant
0
Comment
Question by:JatinHemant
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
ID: 20420081
Hi JatinHemant,

In your code, the indexes i and j represent the height and width of the triangle.

For the width, you want to print the corresponding number of asterisks.  Thats the simple loop:

  for (j = 1; j < width; ++j)
    printf ("*");

You want to print the new line character only AFTER printing the entire line.  There's a couple of smaller issues, but solve that and the rest should fall into place.  Try this for the loops:

for (i=1; i<=n; i++)
{
  for (j=1; j<=i; j++)
  {
  printf("*");
  }
  printf ("\n");
}


That'll get you closer and let you work on the next little oddity.


Good Luck,
Kent
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20420106
Try :

  for (j=1; j<=i; j++)
  {
  printf("*");
  }
  printf("\n");

instead of :

  for (j=1; j<=i; j++)
  {
  printf("*\t\n");
  }
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20420111
Oops, sorry Kdo
0
 
LVL 45

Expert Comment

by:Kdo
ID: 20420148
Hi Infinity08,

There's a twist.  Usually it's me that has to learn to "type faster".   :)


Kent
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20420155
Got distracted while writing the reply heh ;)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:rstaveley
ID: 20421651
Refer to http://csusap.csu.edu.au/~sbuckley/maths/funpage/jokes1.htm#Longer for clarification on Pythagoras's theorem.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20424734
After looking at your question history I concluded this was not a 'homework' type question, therefore I've provided a C++/STL solution example. If STL is not what you need you should be able to modify it a non-STL solution without too much effort: -
#include <iostream>

#include <string>
 

void main()
 

{

	std::cout << "Please enter the number: ";

	int n = 0;
 

	std::cin >> n;
 

	for (int i = 1 ; i <= n; ++i)

	{

		for (int j = 1 ; j <= i; ++j)

		{

			std::cout << "*";

		}

		std::cout << std::endl;

	}

}
 

Please enter the number: 30

*

**

***

****

*****

******

*******

********

*********

**********

***********

************

*************

**************

***************

****************

*****************

******************

*******************

********************

*********************

**********************

***********************

************************

*************************

**************************

***************************

****************************

*****************************

******************************

Open in new window

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 20426219
My advice with respect to loops it to avoid [1,numberOfValues] and prefer [0,numberOfValues).

If you are not familiar with range notation, square brackets mean "inclusive" and round brackets mean "exclusive".

i.e. Prefer

  for (int i = 0 ; i < n; ++i)

  ...and avoid...

  for (int i = 1 ; i <= n; ++i)

It may seem like an unnecessary change, but you'll find that adopting the convention [0,numberOfValues), gets you ready for the joys of standard library algorithms of you go the way of C++.

Incidentally, question http:/Q_22981835.html needs to be closed on this. It is EE convention only to have one question open on a subject at a time.
0
 

Author Closing Comment

by:JatinHemant
ID: 31413163
Thanks to all of you. I accept all the answer but sincerely here, Kdo deserves the points.

Regards,

Hemant
0
 

Author Comment

by:JatinHemant
ID: 20426392
Thanks rstaveley !

I read your suggestion after accepting the solution. But, you gave me very important concept. Got it.

Regards,

Hemant
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now