Followers

Monday, April 24, 2023

Program To Find The Largest Number Amongst Three Numners

Program:
 #include <iostream>  
 using namespace std;  
 int main() {  
      int a, b, c, lg_num;  
      cout<<"Enter first number: ";  
      cin>>a;  
      cout<<"Enter second number: ";  
      cin>>b;  
      cout<<"Enter third number: ";  
      cin>>c;   
      if(a>b && a>c ){  
           lg_num = a;  
      }else if(b>a && b>c ){  
           lg_num = b;  
      }else if(c>a && c>b ){  
           lg_num = c;  
      }  
      cout<<"Largest Number: "<<lg_num<<endl;  
      return 0;  
 }  
Output: 












Working Of The Program:

1. First, I declare the variables a, b, c, and lg_num.
2. Then, I prompt the user to enter the first number using cout and read it in using cin.
3. Next, I prompt the user to enter the second number and read it in using cin.
4. Finally, I prompt the user to enter the third number and read it in using cin.
5. I compare a, b, and c using if-else statements to find the largest number among them.
6. If a is greater than b and c, then I set lg_num to a.
7. If b is greater than a and c, then I set lg_num to b.
8. If c is greater than a and b, then I set lg_num to c.
9. I print out the value of lg_num using cout.
10. Finally, I return 0 to indicate that the program has completed successfully.

Thursday, April 20, 2023

Program To Find The Exponent Of Number

Program:
 #include <iostream>  
 using namespace std;  
 int main(){  
      int base, exp, result = 1;  
      cout<<"Enter the base: ";  
      cin>>base;  
      cout<<"Enter the exponent: ";  
      cin>>exp;  
      for(int i=1; i<=exp;i++){  
           result = result * base;  
      }  
      cout<<base<<" raised to the power "<<exp<<" is "<<result<<endl;  
      return 0;  
 }  
Output: 








Working Of The Program:

Here's how this program works:

1. The program starts by including the standard input/output library, iostream, and using the std namespace.

2. The main function is declared, which returns an integer value and takes no arguments.
3. Three integer variables are declared: base, exp, and result. The result variable is initialized to 1.
4. The program prompts the user to enter the base and exponent values.
5. The user inputs the base and exponent values using the cin (console input) command.
6. A for loop is initiated with a loop counter variable, i, starting at 1 and going up to exp.
7. Within the loop, the result variable is updated by multiplying it with the base value.
After the loop completes, the program outputs the result of the computation using cout (console output).
8. The program ends by returning the integer value 0 to the operating system.

Wednesday, April 19, 2023

Program To Print The Cube Of Number Entered By The User

Program:

 #include <iostream>  
 using namespace std;  
 int main(){  
      int num;  
      cout<<"Enter the number to calculate cube: ";  
      cin>>num;  
      cout<<"The cube of number "<<num<<" is "<<num * num * num<<endl;  
      return 0;  
 }  

Output:
 







Working Of The Program:
Here is the working of the program:
1. The first line includes the iostream library, which is necessary for input/output operations.
2. The second line declares that we are using the standard namespace (std) in our program.
3. The main() function is the starting point of the program, which is executed when the program is run.
4. Inside the main() function, an integer variable called num is declared.
5. The next line prints the message "Enter the number to calculate cube: " to the console.
6. The user is prompted to input a value for the variable num using the cin (console input) statement.
7. The next line calculates the cube of the input number using the formula num * num * num, and prints the result to the console using cout (console output).
8. The program then returns 0, which indicates successful completion of the program.

Program To Print The Square Of A Number Entered By The User

Program:
#include <iostream>  
using namespace std;  
int main(){  
     int num;  
     cout<<"Enter the number to calculate square: ";  
     cin>>num;  
     cout<<"The square of number "<<num<<" is "<<num * num<<endl;  
     return 0;  
}  
Output: 







Working Of The Program:
Here is the working of the program:
1. This is a C++ program that calculates the square of a number entered by the user.
2. The program starts by including the iostream library, which provides input and output operations functionality.
3. The program then declares a variable called "num" of type "int", which will be used to store the number entered by the user.
4. The program outputs a prompt asking the user to enter the number to be squared using the "cout" statement.
5. The user's input is read in using the "cin" statement and stored in the "num" variable.
6. The program then calculates the square of the number by multiplying "num" by itself and outputs the result to the user using the "cout" statement.
7. The program returns 0 to indicate successful execution and terminates.

Saturday, April 15, 2023

Program To Print A Triangle Pattern

 Program:

 #include <iostream>    
 using namespace std;    
 int main() {    
  cout<<"Program To Print A Triangle"<<endl;  
  cout<<"*"<<endl;  
  cout<<"**"<<endl;  
  cout<<"***"<<endl;  
  cout<<"****"<<endl;  
  cout<<"*****"<<endl;  
  cout<<"******"<<endl;    
  return 0;    
 }  
Output:












Working Of The Program:
1. This is a C++ program written to print a triangle using asterisks (*) on the console.
2. The first line of the code includes the iostream header file, which is needed for input/output operations.
3. The 'using namespace std' statement is used to avoid writing the 'std::' prefix before every standard library function call.
4. The main() function is the entry point of the program, which contains the code to be executed.
5. The first cout statement prints a string on the console, which indicates the purpose of the program.
6. The next six cout statements print the triangle using asterisks in a pattern of increasing the number of asterisks in each row.
7. Finally, the 'return 0;' statement is used to exit the program with a status of 0, indicating successful execution.

Friday, April 14, 2023

Program To Create A Square Pattern

Program:
 #include <iostream>    
  using namespace std;    
  int main() {    
  cout<<"Program To Form A Square"<<endl<<endl;   
  cout<<"* * * * * *"<<endl;   
  cout<<"*         *"<<endl;    
  cout<<"*         *"<<endl;   
  cout<<"*         *"<<endl;   
  cout<<"*         *"<<endl;   
  cout<<"* * * * * *"<<endl;   
  return 0;    
 }  
Output:














Working Of The Program:
1. The code starts with the inclusion of the "iostream" header file, which is necessary to use the standard input/output stream objects like "cout" and "cin" in C++.
2. The "using namespace std" statement is used to avoid typing "std::" before standard library objects like "cout" and "endl".
3. The "main" function is the starting point of the program, and it's where the execution begins.
4. The first line inside the "main" function prints a string message to the console using the "cout" object.
5. The next six lines print a square shape made of asterisks and spaces to the console. The first and last lines contain six asterisks separated by spaces, forming the top and bottom sides of the square. The middle four lines contain an asterisk followed by four spaces and another asterisk, forming the left and right sides of the square.
6. Finally, the "return 0" statement is used to indicate a successful termination of the program.

Tuesday, April 11, 2023

Program To Calculate The Sum of Positive Integers Up To An Upper Limit

Program:
 #include <iostream>    
 using namespace std;    
 int main() {    
  int upper_Limit = 0 , sum = 0;  
  cout<<"The highest positive integer you want to add upto: ";  
  cin>>upper_Limit;  
  for(int i=1; i<=upper_Limit; i++){  
      sum = sum + i;  
  }  
  cout<<"The sum of positive integer from 1-"<<upper_Limit<<" is "<<sum<<endl; 
  return 0;   
 }   
Output: 






Working Of The Program:
The program works as follows:
1. The program begins by including the iostream library, which contains the necessary functions for input/output operations.
3. The 'using namespace std' line declares that the code will use the standard namespace, which contains the standard C++ library functions.
4. The main function is the entry point of the program.
5. The program initializes two integer variables, 'upper_Limit' and 'sum', to 0.
6. The program prompts the user to enter the highest positive integer they want to add up to, and stores the value in the 'upper_Limit' variable using the 'cin' function.
7. A 'for' loop is used to iterate from 1 to the user input 'upper_Limit'.
8. In each iteration of the loop, the value of 'i' is added to the 'sum' variable.
9. Once the loop has finished iterating, the program outputs the sum of positive integers from 1 to the user input 'upper_Limit'.
10. The program ends with the 'return 0' statement, indicating that the program has been completed successfully.

Monday, April 10, 2023

Program To Find The Minimum Number In An Array

 

Program:

 #include <iostream>    
  using namespace std;    
  int main() {    
   int arr[5] = {1, 2, 3, 4, 5}, min_num=arr[0];    
   for(int i=0; i<5;i++){   
       if(arr[i]>min_num){   
         min_num=arr[i];   
       }   
    }   
    cout<<"The minimum number in the array [1, 2, 3, 4, 5] is "<<min_num<<endl;   
   return 0;   
  }   
Output:

Working Of The Program:

The program works as follows:
1. This code is a C++ program that finds the minimum element in an array of integers.
2. The program initializes an array arr of size 5 with the values {1, 2, 3, 4, 5}.
3. The program also initializes a variable min_num to the first element of the array, which is 1.
4. The program then enters a for loop that iterates over each element of the array, using the loop counter variable i.
5. Inside the loop, the program checks if the current element of the array arr[i] is less than the current value of min_num.
6. If the current element is less than min_num, the program updates the value of min_num to be the current element arr[i].
7. After the loop completes, the program outputs the minimum element of the array to the console using cout.
8. The output message includes the phrase "The minimum number in the array", followed by the array elements in square brackets and the calculated minimum value.
9. Finally, the program terminates with a return value of 0.

Program To Find The Maximum Number In An Array

Program:
  #include <iostream>   
  using namespace std;   
  int main() {   
   int arr[5] = {1, 2, 3, 4, 5}, max=arr[0];    
   for(int i=0; i<5;i++){  
           if(arr[i]>max){  
                max=arr[i];  
           }  
      }  
      cout<<"\n\tThe maximum number in the array [1, 2, 3, 4, 5] is "<<max<<endl;  
    return 0;  
 }  
Output:





Working Of The Program:
The program works as follows:
1. The code defines a C++ program that finds the maximum element in an array of integers.
2. The program begins by declaring an integer array arr of size 5, and initializes its elements to the values {1, 2, 3, 4, 5}.
3. The program also declares an integer variable max and initializes it to the first element of the array arr[0].
4. The program then enters a for loop that iterates over each element of the array, using the loop counter variable i.
5. Inside the loop, the program checks if the current element of the array arr[i] is greater than the current value of max.
6. If the current element is greater than max, the program updates the value of max to be the current element arr[i].
7. After the loop completes, the program outputs the maximum element of the array to the console using cout.
8. The output message includes the phrase "The maximum number in the array", followed by the array elements in square brackets and the calculated maximum value.
9. Finally, the program terminates.

Program to Find the Length of a String

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