در زیر کدهای مورد نیاز برای حل این مسئله در چهار زبان برنامه نویسی مختلف را می بینید:
C++:
#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
int product = 1;
for(int i = 1; i <= n; i++) {
product *= i;
}
cout << "Product of first " << n << " numbers is: " << product << endl;
return 0;
}
Python:
n = int(input("Enter a number: "))
product = 1
for i in range(1, n+1):
product *= i
print("Product of first", n, "numbers is:", product)
C#:
using System;
class Program {
static void Main() {
Console.Write("Enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());
int product = 1;
for(int i = 1; i <= n; i++) {
product *= i;
}
Console.WriteLine("Product of first " + n + " numbers is: " + product);
}
}
Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
int product = 1;
for(int i = 1; i <= n; i++) {
product *= i;
}
System.out.println("Product of first " + n + " numbers is: " + product);
}
}