Beecrowd 1161 Factorial Sum Solution

Beecrowd 1161 Factorial Sum Solution in C, C++, Java

Welcome to our comprehensive guide on URI Factorial Sum Solutions in C, C++, and Java. In this guide, we will provide you with an in-depth understanding of the factorial sum problem and its solutions in these three programming languages. We will also compare and contrast the solutions to help you choose the best option for your specific needs.

What is URI Factorial Sum Problem?

The URI Factorial Sum Problem is a mathematical problem that requires us to calculate the sum of factorials of a given number. The problem is stated as follows:

Given a number n, find the sum of factorials of all the numbers from 1 to n.

For example, if n is 3, then the sum of factorials would be 1! + 2! + 3! = 1 + 2 + 6 = 9.

Bee1161 Solutions in C:

To solve the URI Factorial Sum problem in C, we can use loops to iterate through each number from 1 to n and calculate the factorial sum. Here is the code snippet to solve the problem in C:

#include<stdio.h>

int main()
{
    int n, i, fact = 1, sum = 0;
    
    printf("Enter the value of n: ");
    scanf("%d", &n);
    
    for(i=1;i<=n;i++)
    {
        fact = fact * i;
        sum = sum + fact;
    }
    
    printf("Sum of factorials of 1 to %d is %d", n, sum);
    
    return 0;
}

Bee1161 Solutions in C++:

In C++, we can use a similar approach as C to solve the URI Factorial Sum problem. However, C++ provides us with some additional features such as the iostream library and the using namespace std; statement. Here is the code snippet to solve the problem in C++:

#include <iostream>
using namespace std;

int main() {

   long long int m,n,f=1,f1=1;
   while(cin>>m>>n){
   for(int i=1;i<=m;i++)
   {
       f=f*i;
   }
   for(int j=1;j<=n;j++)
   {
       f1=f1*j;
   }

   long long int sum=f+f1;
   cout<<sum<<endl;
   f=f1=1;

   }
    return 0;
    cout<<endl;
}

Bee1161 Solutions in Java:

In Java, we can use the BigInteger class to handle large factorials. Here is the code snippet to solve the URI Factorial Sum problem in Java:

import java.math.BigInteger;
import java.util.Scanner;

public class FactorialSum {

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        int n = input.nextInt();
        BigInteger fact = BigInteger.ONE, sum = BigInteger.ZERO;
        
        for(int i=1;i<=n;i++)
        {
            fact = fact.multiply(BigInteger.valueOf(i));
            sum = sum.add(fact);
        }
        
        System.out.println("Sum of factorials of 1 to "+n+" is "+sum);
    }

}

Comparison of Solutions:

All three solutions provide an efficient and effective way to solve the URI Factorial Sum problem. However, Java’s BigInteger the class provides us with the ability to handle very large factorials that are not possible with C and C++. C++ is slightly faster than C, but the difference is negligible for small inputs.

Conclusion:

In this guide, we provided you with a comprehensive understanding of the URI Factorial Sum problem and its solutions in C, C++, and Java. We hope this guide helps you choose the best solution for your specific needs.

Competitive programming | C++ | C | Computer programing |Competitive programming website

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
NEW TOXIC ROCKET CYCLE DECK in Clash Royale 2023 Best Deck in Clash Royale Best Deck for Arena 8 in Clash Royale Best Hog deck for Pro players Best Clash Royale Deck