DonanımHaber Mini sürüm 2 Ocak 2026 tarihi itibariyle kullanımdan kalkacaktır. Bunun yerine daha hızlı bir deneyim için DH Android veya DH iOS uygulamalarını kullanabilirsiniz.
Arama butonu
Bu konudaki kullanıcılar: 1 misafir
5
Cevap
1326
Tıklama
0
Öne Çıkarma
java right triangle?
B
11 yıl (253 mesaj)
Teğmen
Konu Sahibi

benden istenen bu:

Write a program that prints a right triange out of numbers. After each time a number is printed, the number should be incremented by 1. The program should also print a space character after each number.

Your program should work as follows;

- Get a number from user, which is number of columns

- Check if the user input is valid (it should be greater than 1). If not print an error message "Invalid number of columns"

- If the number is valid, print a right triangle having inputted number of columns and as stated above.

Sample Execution 1:

Enter number of columns: -1

Invalid number of columns

Sample Execution 2:

Enter number of columns: 3

1

2 3

4 5 6

Sample Execution 3:

Enter number of columns: 5

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15


benim şu ana kadar yazdıklarım şunlar
quote:

public static void main(String[] args) {
Scanner tri = new Scanner(System.in);
System.out.print("Enter number of columns: ");
int columns = tri.nextInt();

while(0<=columns);

if(0>=columns);
System.out.print("Invalid number of columns");

else

devamına ne yazmam gerektiğini bulamıyorum.



S
11 yıl (222 mesaj)
Teğmen

ama hiç başlamamışsın ki. Sen neleri denediğini yaz, ben yazdığım kodu (c++ ile) sonra göndereyim.


Bu mesaja 1 cevap geldi.
S
11 yıl (222 mesaj)
Teğmen

nb kullanıcının girdiği sayı iken


int scrn;
scrn = 1;
for (int i = 1; i <= nb; i++)
{
for (int j = 1; j <= i; j++){
cout << scrn++;
cout << " ";
}
cout << endl;
}





< Bu mesaj bu kişi tarafından değiştirildi senGitBenGeliyorum -- 16 Kasım 2014; 17:12:33 >

L
11 yıl (388 mesaj)
Yüzbaşı

 
int ilk = 0, son = 1, satir = 5;

for (int i = 1; 0 < satir; i++)
{
System.out.printf("%d ", i);

if( (ilk + son) == i )
{
System.out.println();
son = (++ilk + son);
satir--;
}
}



< Bu ileti mini sürüm kullanılarak atıldı >
Bu mesaja 1 cevap geldi.
B
11 yıl (253 mesaj)
Teğmen
Konu Sahibi

quote:

import java.util.Scanner;


public class Prelab3 {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
int count1, count2, count3, count4;
System.out.print("Enter number of columns: ");
count1 = scan.nextInt();
count2 = 1;
count3 = 1;
count4 = 1;
if (count1 < 0)
System.out.println("Invalid number of columns");
else
while (count2 <= count1){
while (count3 <= count2){
System.out.print(count4 + " ");
count4++;
count3++;
}
count2++;
count3 = 1;
System.out.println("");

}
}

}

böyle girdiğim zaman sonuca ulaştım peki bu döngüyü tepeden azalan şekilde nasıl yapabilirim?



L
11 yıl (388 mesaj)
Yüzbaşı

ben yukardan azalanı bu şekil yaptım:

 
Scanner girdi = new Scanner(System.in);

System.out.printf("Kac Satir Olacak..: ");
int satir = girdi.nextInt();
for(int i = ( (satir % 2 != 0) ? ((satir * satir) - (satir * (satir / 2))) : ((satir * satir) - (satir * (satir / 2)) + (satir / 2)) ); 0 < i; i--)
{
if( i == ( ((satir % 2 != 0) ? ((satir * satir) - (satir * (satir / 2))) : ((satir * satir) - (satir * (satir / 2)) + (satir / 2))) - (satir - 1) ) )
{
System.out.printf("\n");
satir--;
}
}

Output:
Kac Satir olacak..: 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1


Algoritmam:
(altaki 5 ve 8 sayıları "satir" değişkeni:

Eğer sayı tek ise:
(5 x 5) - (5 * (5 / 2))

Eğer Sayı çift ise:
(8 x 8) - (8 * (8 / 2)) + (8 / 2)



< Bu ileti mini sürüm kullanılarak atıldı >

DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.