while বা for লুপ এবং স্ট্রিং ফরম্যাটিং ব্যবহার করে যেকোনো সংখ্যার গুননার টেবিল প্রিন্ট করা যায়।
বেসিক লজিক
int n = 4; // যেই সংখ্যার টেবিল
int i = 1;
while (i <= 10)
{
Console.WriteLine($"{n} x {i} = {n * i}");
i++;
}
সম্পূর্ণ উদাহরণ
using System;
class MultiplicationTableDemo
{
public static void Main()
{
Console.Write("কোন সংখ্যার গুননার টেবিল চান? ");
if (int.TryParse(Console.ReadLine(), out int n))
{
Console.WriteLine($"\n{n} এর গুননার টেবিল (১ থেকে ১০):");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine($"{n} x {i,2} = {n * i,3}");
}
}
else
{
Console.WriteLine("অবৈধ ইনপুট!");
}
}
}
আউটপুট (n=4)
4 এর গুননার টেবিল (১ থেকে ১০):
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
উন্নত ফিচার
- ইনপুট ভ্যালিডেশন:
int.TryParseব্যবহার করুন। - ফরম্যাটিং:
{i,2}(রাইট-আলাইন),{n*i,3}কলাম অ্যালাইন করতে। - রেঞ্জ কাস্টমাইজ: শুরু এবং শেষ মান ইউজার ইনপুট নিন।
ফাংশন হিসেবে
public static void PrintMultiplicationTable(int number, int start = 1, int end = 10)
{
for (int i = start; i <= end; i++)
Console.WriteLine($"{number} x {i} = {number * i}");
}