Followers

Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

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 Calculate The Sum Of All Elements Of An Array

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







Working Of The Program:
The working of the program is as follows:
1. The code defines a C++ program that calculates the sum of 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 sum and initializes it to 0.
4. The program then enters a for loop that iterates over each element of the array, using loop counter variable i.
5. Inside the loop, the program adds the current element of the array arr[i] to the sum variable.
6. After the loop completes, the program outputs the sum of the array to the console using cout.
7. The output message includes the phrase "The sum of the array", followed by the array elements in square brackets and the calculated sum.
8. 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...