Skip to main content

Posts

Showing posts with the label break

Calculating the sum of diagonal elements and then finding the difference in C++

                            #add of diagonal of 2d array                           #include <iostream> using namespace std; #include <math.h> int main() {     int arr[3][3] = {1, 4, 2, 5, 2, 8, 2, 5, 9};     int sum1 = 0 , sum2 = 0;      for (int i = 0; i < 3; i++) {         sum1 += arr[i][i];          sum2 += arr[i][2 - i];      }     int d = abs(sum1 - sum2);     cout << "Sum of diagonal1: " << sum1 << endl;     cout << "Sum of diagonal2: " << sum2 << endl;     cout << "Diff between the sums: " << d << endl;     return 0; }

HOW TO PASS ARRAY IN FUNCTION

       SUM OF ALL ELEMENTS OF ARRAY   #include <iostream> using namespace std;   int abc(int brr[], int n)    //abc is the name of function {               int sum=0;               for(int i=0; i<n; i++)               {                              sum+=brr[i];               }               return sum; }   int main() { int n = 10; int arr[n]= {1,2,3,4,5,6,7,8,9,10};   cout<<"Sum is : "<<abc(arr,n)<<endl;        ...

Jump Statements in C – break, continue, goto, return

Jump Statement  makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the  loop   or  switch-case   instantly. It is also used to escape the execution of a section of the program. ·         Break ·         Continue ·         Goto ·         Return 1. Break Jump Statement A  break  statement is used to terminate the execution of the rest of the block where it is present and takes the control out of the block to the next statement. It is mostly used in loops and switch-case to bypass the rest of the statement and take the control to the end of the loop. The use of the  break  keyword in switch-case has been explained in the previous tutorial  Switch – Control Statement . Another point to be taken into consideration is that the  br...