මාත challenge එක කරා හැබැයි python වලින් නම් නෙවෙයි 😊 import java.util.ArrayList; import java.util.Scanner; public class Main { public int inputNumber; public ArrayList list = new ArrayList(); public static void main(String[] args) { Main obj = new Main(); Scanner sc = new Scanner(System.in); System.out.print("Enter number : "); obj.inputNumber = sc.nextInt(); for (int i = 0; i < obj.inputNumber; i++) { int ans = obj.getTriangular(i); obj.list.add(ans); } System.out.print("Triangular numbers are : "); for (int i = 0; i < obj.list.size(); i++) { if ( i == (obj.list.size() - 1) ){ System.out.println( obj.list.get(i)); }else{ System.out.print( obj.list.get(i)+" , "); } } int size = obj.list.size() - 1; System.out.print("Enter number between 0 and "+size+" : "); int inputSize = sc.nextInt(); if ( size >= inputSize){ System.out.println("The Triangular number at index "+inputSize+" is " + obj.list.get(inputSize)); }else{ System.out.println("Invalid number"); } } public int getTriangular(int x) { return x * (x + 1) / 2; } }
mama me challenge eka hadawwe For loop ekakin triangularNum = 0 triangularNumOut = [] num = int(input("Enter a Number : ")) for i in range (num): triangularNum = triangularNum + i if triangularNum 0: triangularNumOut.append(triangularNum) else: break print(triangularNumOut) area = len(triangularNumOut) area = area - 1 print("Enter a number between 0 and " + str(area) + ": ")
Sample answer for fun :) entered_number = int(input("Enter a number: ")) answers = [((i * (i + 1)) // 2) for i in range(1,entered_number) if ((i * (i + 1)) // 2) < entered_number] #print the list print(f"Triangular numbers up to {entered_number} : {answers}") #print the answer for entered index prompt = int(input(f"Enter a number between 0 to {int(len(answers))-1}: ")) print(f"The Triangular number as index {str(prompt)} is: {answers[prompt]}")
SL Geek School එකෙන් කරන Live Bootcamp එකට join වෙලා මාත් එක්ක live code කරන ගමන් ප්රශ්න හැම දේටම live එකේ උත්තර දැනගන කැමතිද. පැය 2-3 කින් python වල beginner කෙනෙක් ඉදන් intermediate වෙනකන් යන්න ආසද ? මේ form එකෙන් bootcamp එක ගැන විස්තර දැනගන්න පුළුවන්. forms.gle/c3PxdQzDUfb5cehB6
niyamai math challenge eka kara. me challenge eka pattai
Niyamayi ❤
නියමයි ❤❤
මාත challenge එක කරා හැබැයි python වලින් නම් නෙවෙයි 😊
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public int inputNumber;
public ArrayList list = new ArrayList();
public static void main(String[] args) {
Main obj = new Main();
Scanner sc = new Scanner(System.in);
System.out.print("Enter number : ");
obj.inputNumber = sc.nextInt();
for (int i = 0; i < obj.inputNumber; i++) {
int ans = obj.getTriangular(i);
obj.list.add(ans);
}
System.out.print("Triangular numbers are : ");
for (int i = 0; i < obj.list.size(); i++) {
if ( i == (obj.list.size() - 1) ){
System.out.println( obj.list.get(i));
}else{
System.out.print( obj.list.get(i)+" , ");
}
}
int size = obj.list.size() - 1;
System.out.print("Enter number between 0 and "+size+" : ");
int inputSize = sc.nextInt();
if ( size >= inputSize){
System.out.println("The Triangular number at index "+inputSize+" is " + obj.list.get(inputSize));
}else{
System.out.println("Invalid number");
}
}
public int getTriangular(int x) {
return x * (x + 1) / 2;
}
}
Man hithuwa channel eka nawattala kiyala ecommerce eke videosuth dannako
mama me challenge eka hadawwe For loop ekakin
triangularNum = 0
triangularNumOut = []
num = int(input("Enter a Number : "))
for i in range (num):
triangularNum = triangularNum + i
if triangularNum 0:
triangularNumOut.append(triangularNum)
else:
break
print(triangularNumOut)
area = len(triangularNumOut)
area = area - 1
print("Enter a number between 0 and " + str(area) + ": ")
Sample answer for fun :)
entered_number = int(input("Enter a number: "))
answers = [((i * (i + 1)) // 2) for i in range(1,entered_number) if ((i * (i + 1)) // 2) < entered_number]
#print the list
print(f"Triangular numbers up to {entered_number} : {answers}")
#print the answer for entered index
prompt = int(input(f"Enter a number between 0 to {int(len(answers))-1}: "))
print(f"The Triangular number as index {str(prompt)} is: {answers[prompt]}")