Notice: Undefined offset: 15989876 in /var/www/html/qa-external/qa-external-users.php on line 744
Factorail for a non integer number - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Factorail for a non integer number

+15 votes
asked Sep 26, 2018 by anonymous
Print a non negative number less than 7 and get the factorial for the non negative number

4 Answers

0 votes
answered Jun 30, 2025 by Jerry Jeremiah (2,040 points)

The problem with this question is the factorial of 0:

Non-negative number: 0   Factorial: 1

The rest are easy to understand.

Number: 1   Factorial: 1
Number: 2   Factorial: 2
Number: 3   Factorial: 6
Number: 4   Factorial: 24
Number: 5   Factorial: 120
Number: 6   Factorial: 720
Number: 7   Factorial: 5040

So, if you need code you can just print the results (without calculating anything).

https://onlinegdb.com/P9R0ojB1Q

#include <stdio.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("No number provided");
        return 1;
    }

    int input=atoi(argv[1]);

    if(input < 0) {
        printf("Number less than 0");
        return 2;
    }

    if(input > 7) {
        printf("Number larger than 7");
        return 3;
    }
    
    switch(input) {
        case 0:  printf("Non-negative integer: 0 Factorial: 1");    break;
        case 1:  printf("Non-negative integer: 1 Factorial: 1");    break;
        case 2:  printf("Non-negative integer: 2 Factorial: 2");    break;
        case 3:  printf("Non-negative integer: 3 Factorial: 6");    break;
        case 4:  printf("Non-negative integer: 4 Factorial: 24");   break;
        case 5:  printf("Non-negative integer: 5 Factorial: 120");  break;
        case 6:  printf("Non-negative integer: 6 Factorial: 720");  break;
        case 7:  printf("Non-negative integer: 7 Factorial: 5040"); break;
    }

    return 0;
}

+1 vote
answered Jul 2, 2025 by ALAN (160 points)
import math

# Get input from user
num = int(input("Enter a non-negative number less than 7: "))

# Validate the input
if 0 <= num < 7:
    # Calculate factorial
    factorial = math.factorial(num)
    print(f"The factorial of {num} is {factorial}")
else:
    print("Invalid input. Please enter a non-negative number less than 7.")
+1 vote
answered Jul 7, 2025 by Gundubommala Praveen 22131936101 (160 points)

import java.util.Scanner;

public class Main 

{

    public static void main(String[] args) 

{

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a non-negative number less than 7 ");

        int num = sc.nextInt();

        if (num < 0 || num >= 7) 

{

            System.out.println("Invalid input! Number must be between 0 and 6");

        } else 

{

            int factorial = 1;

            for (int i = 1; i <= num; i++) 

{

                factorial *= i;

            }

            System.out.println("Factorial of " + num + " is" + factorial);

        }

    }

}

+1 vote
answered Jul 7, 2025 by (160 points)
https://onlinegdb.com/uWJ9GnzIC

Is it solving the asked query?
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...