Followers

Wednesday, March 29, 2023

Program To Calculate The Factorial Of Number Entered By the User

Program:

#include <iostream>  
using namespace std;  
int main() {  
  int n, fact = 1;  
  cout << "Enter a positive integer: ";  
  cin >> n;  
  if(n < 0) {  
    cout << "Error: Please enter a positive integer" << endl;  
    return 1;  
  }  
  for(int i = 1; i <= n; ++i) {  
    fact *= i;  
  }  
  cout << "Factorial of " << n << " is " << fact << endl;  
  return 0;  
}  

Output:








Working Of The Program:

Here is how this program works:
1. The code prompts the user to enter a positive integer and reads it from the standard input using the cin statement.
2. An integer variable fact is initialized with a value of 1, which will store the entered number's factorial.
3. The code checks whether the entered number is less than zero. If it is, an error message is printed to the console and the program terminates with a return code of 1. This is done to ensure that the entered number is positive, as the factorial is defined only for positive integers.
4. A for loop is used to compute the factorial of the entered number. The loop variable i is initialized to 1 and iterates until i is greater than the entered number n.
5. In each iteration of the loop, the current value of fact is multiplied by the value of i, and the result is stored back in fact. This updates the value of fact to be the product of all the integers from 1 to n.
6. After the loop completes, the final value of fact contains the factorial of the entered number, which is printed to the console using the cout statement.
7. The program terminates with a return code of 0, indicating successful execution.

Monday, March 27, 2023

Program To Check If The Number Is Odd Or Not

Program:

 #include <iostream>    
 using namespace std;    
 int main() {  
      int num;  
      cout<<"Enter the number to check: ";  
      cin>>num;  
      if(num%2 != 0){  
           cout<<"The number "<<num<<" is odd!"<<endl;  
      }else{  
           cout<<"The number "<<num<<" is not odd!"<<endl;  
      }  
      return 0;    
 }   
Output:
 In Case Of Odd Number:

In Case Of Non-Odd Number:

Working Of The Program:

1. First, I declare the variable num.
2. Then, I prompt the user to enter a number to check whether it is odd or not using cout and read it in using cin.
3. I check whether the number is odd or not using an if-else statement.
4. If the number is not divisible by 2, i.e., if the remainder of the division operation num%2 is not equal to 0, then the number is odd. In that case, I print out a message saying "The number num is odd!" using cout.
5. If the number is divisible by 2, i.e., if the remainder of the division operation num%2 is equal to 0, then the number is not odd. In that case, I print out a message saying "The number num is not odd!" using cout.
6. Finally, I return 0 to indicate that the program has completed successfully.

Program To Check If The Number Is Even Or Not

Program:

 #include <iostream>    
 using namespace std;    
 int main() {  
      int num;  
      cout<<"Enter the number to check: ";  
      cin>>num;  
      if(num%2 == 0){  
           cout<<"The number "<<num<<" is even!"<<endl;  
      }else{  
           cout<<"The number "<<num<<" is not even!"<<endl;  
      }  
      return 0;    
 }   
Output:
In Case Of Even Number:

In Case Of Non-Even Number:

Working Of The Program:

1. Then, I prompt the user to enter a number to check whether it is even or not using cout. 2. I read in the user input using cin and store it in the variable num. 3. I check if the number is even using an if-else statement and the modulo operator %. 4. If the remainder of num divided by 2 is 0, then num is even. 5. In that case, I print out a message using cout that says "The number [num] is even!", where [num] is replaced with the value of num. 6. If the remainder of num divided by 2 is not 0, then num is odd. 7. In that case, I print out a message using cout that says "The number [num] is not even!", where [num] is replaced with the value of num. 8. Finally, I return 0 to indicate that the program has completed successfully.

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...