🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Factorial, denoted as n!, is a fundamental mathematical operation, representing the product of all positive integers less than or equal to n. For instance, the factorial of 5 (5!) is 5 x 4 x 3 x 2 x 1 = 120. Computing factorials is a popular task in programming, serving as a means to understand control structures, particularly loops. In this post, we'll discuss a C++ program that calculates the factorial of a number without using recursion.
2. Program Overview
The program's logic is straightforward:
1. Prompt the user to input a number.
2. Use a loop to multiply numbers in decreasing order until 1.
3. Display the calculated factorial to the user.
3. Code Program
#include <iostream>
using namespace std;
int main() {
int num;
unsigned long long factorial = 1; // To hold larger factorial values
// Getting user input
cout << "Enter a positive integer: ";
cin >> num;
// Factorial calculation
for(int i = 1; i <= num; ++i) {
factorial *= i; // Multiplying each number with the result
}
cout << "Factorial of " << num << " = " << factorial;
return 0; // Signify successful program termination
}
Output:
For an input of 5, the program would output: "Factorial of 5 = 120"
4. Step By Step Explanation
1. Headers and Namespace: We commence by including the I/O stream library and establishing the use of the standard namespace.
2. Main Function: This is where our program starts. Within, we declare our variables: num for holding user input and factorial initialized to 1 for computing the factorial. We use unsigned long long for factorial to manage larger factorial values.
3. User Input: The program uses cout to prompt for a number and cin to receive the entered number.
4. Factorial Logic: Utilizing a for loop, we multiply numbers in descending order till 1. The multiplication accumulates in the factorial variable.
5. Displaying the Result: The program then uses cout to display the calculated factorial.
6. Program Termination: The main function concludes and signals successful termination with a return value of 0.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment