Last active
January 17, 2021 02:40
-
-
Save sparshs413/4f787f8d14d5b855f5d653dcf2ff42b8 to your computer and use it in GitHub Desktop.
Contains the Code snippets for Session on C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Cout | |
| // #include <iostream> | |
| // using namespace std; | |
| // int main() | |
| // { | |
| // char sample[] = "Electronics Club"; | |
| // cout << sample << " , IIT Kanpur"; | |
| // return 0; | |
| // } | |
| // Cin | |
| // #include <iostream> | |
| // using namespace std; | |
| // int main() | |
| // { | |
| // int age; | |
| // cout << "Enter your age:"; | |
| // cin >> age; | |
| // cout << "\nYour age is: " << age; | |
| // return 0; | |
| // } | |
| // numbers using ternary operator | |
| // #include <iostream> | |
| // using namespace std; | |
| // int main() | |
| // { | |
| // // variable declaration | |
| // int n1 = 5, n2 = 10, max; | |
| // // | |
| // // Largest among n1 and n2 | |
| // max = (n1 > n2) ? n1 : n2; | |
| // // Print the largest number | |
| // cout << "Largest number between " << n1 | |
| // << " and " << n2 | |
| // << " is " << max; | |
| // return 0; | |
| // } | |
| // Basic Function Example | |
| // #include <iostream> | |
| // using namespace std; | |
| // int max(int x, int y) | |
| // { | |
| // if (x > y) | |
| // return x; | |
| // else | |
| // return y; | |
| // } | |
| // int main() { | |
| // int a = 10, b = 20; | |
| // // Calling above function to find max of 'a' and 'b' | |
| // int m = max(a, b); | |
| // cout << "m is " << m; | |
| // return 0; | |
| // } | |
| // Check Prime Number - Functions | |
| // #include <iostream> | |
| // using namespace std; | |
| // bool checkPrimeNumber(int); | |
| // int main() { | |
| // int n; | |
| // cout << "Enter a positive integer: "; | |
| // cin >> n; | |
| // if (checkPrimeNumber(n)) | |
| // cout << n << " is a prime number."; | |
| // else | |
| // cout << n << " is not a prime number."; | |
| // return 0; | |
| // } | |
| // bool checkPrimeNumber(int n) { | |
| // bool isPrime = true; | |
| // // 0 and 1 are not prime numbers | |
| // if (n == 0 || n == 1) { | |
| // isPrime = false; | |
| // } | |
| // else { | |
| // for (int i = 2; i <= n / 2; ++i) { | |
| // if (n % i == 0) { | |
| // isPrime = false; | |
| // break; | |
| // } | |
| // } | |
| // } | |
| // return isPrime; | |
| // } | |
| // // Largest No. (Q1: Arrays) | |
| // #include<iostream> | |
| // using namespace std; | |
| // int main(){ | |
| // int size; | |
| // cin>>size; // input size of array | |
| // int arr[size]; // declaring array | |
| // int max=INT_MIN; | |
| // for(int i=0;i<size;i++){ // looping over complete array to find max no. | |
| // cin>>arr[i]; | |
| // if(max<arr[i]){ // if no. is greater than max then this no is our new maximum | |
| // max=arr[i]; | |
| // } | |
| // } | |
| // cout<<max<<endl; | |
| // } | |
| // // Largest Length (Q2: Arrays) | |
| // #include<iostream> | |
| // #include <string.h> | |
| // using namespace std; | |
| // int main(){ | |
| // int size; | |
| // cin>>size; // input size | |
| // string arr[size]; // declared a array | |
| // int max=0; // intialize some variable | |
| // int idx=0; | |
| // for(int i=0;i<size;i++){ | |
| // cin>>arr[i]; // input each string | |
| // if(max < arr[i].length()){ // same condition as before | |
| // max=arr[i].length(); | |
| // idx=i; | |
| // } | |
| // } | |
| // cout<<arr[idx]<<endl; | |
| // cout<<max<<endl; | |
| // } | |
| // // Rotate Array (Q3: Arrays) | |
| // #include<iostream> | |
| // #include <string.h> | |
| // using namespace std; | |
| // int main(){ | |
| // int size; | |
| // cin>>size; // take size of array as an input | |
| // int arr[size]; // declaring arr of given size | |
| // for(int i=0;i<size;i++){ // loop over each iteration and input array element accordingly | |
| // cin>>arr[(i+1)%size]; | |
| // } | |
| // for(int i=0;i<size;i++){ // print each element of array | |
| // cout<<arr[i]<<" "; | |
| // } | |
| // } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment