Posts

Showing posts from June, 2024

functions and function prototyping in c++

  #include <iostream> using namespace std ; // int sum(int a, int b){ //     int c = a+b; //     return c; // } /**********FUNCTION PROTOTYPE************* */ int sum ( int a , int b ); //ab program run hojaega void greet ( void ); // void greet();---> acceptable hai //ye assurity deta hai ki sum function aage hai //int sum(int a, int b);--->acceptable //int sum(a, b);---> not acceptable //int sum(int, int);--->acceptable int main () {     //compiler main function se read krna start krta code ko.     /*************FUNCTION*************/     /*main is itself a function and yaha return zero ka mtlb hai ki     ye program successfully execute hogya ha and koi dikkat nhi aai hai*/         int k;     int s;     cout << "enter the value of k " << endl;     cin >> k;     cout << "enter the value of s " << endl; ...

structures, unions and enum in c++

  #include <iostream> using namespace std ; //structure is a user defined data type which stores value of other data types. //arrays ko use kr skte h sirf similar type ke data type ko store krne k liye typedef struct employee     {         /* data */         int employee_id; //4 bytes         char favchar; //1 byte         float salary; //4 bytes     } KS ;     union money     {         /* data */         int rice; //4         char cars; //1 //hum in teeno m se ek time pr sirf ek cheez use krenge         float homes; //4 //teeno mese koi ek set kr skte h. mtlb koi ek trh ka data type use krna chahein     };     //union memory share kradega inn teeno ke beech main     //union 4 (max) byte memory allocate krdega so that teeno ka kaam bn j...

arrays and pointers arithematic in c++

Image
  #include <iostream> using namespace std ; int main () {     /*###AN ARRAY IS A COLLECTION OF ITEMS OF SIMILAR TYPES STORES IN CONTIGUOS(CHIPAK CHIPAK KE) MEMORY LOCATION ###SOMETIMES A SIMPLE VARIABLE IS NOT ENOUGH TO HOLD ALL THE DATA ###FOR EXAMPLE hume 2500 students ke marks store krne h so 2500 variables bnana fesile nhi hoga ###to solve this problem we can define an array with size 2500 that can hold the marks of all students ###ginti 0 se start hogi ye c++ ka rule hai, 0,1,2... indices hote h*/     /********ARRAYS EXAMPLE********/     //int marks[4] = {5, 7, 57, 75}; //bracket m 4 likhna optional hai kyuki compiler khud hi figure out krelta hai     //cout<<marks[0]<<endl;     //cout<<marks[1]<<endl;     //cout<<marks[2]<<endl;     //cout<<marks[3]<<endl; //yaha humne marks naam ka ek array bnaya jo continuous data store kr raha h //another way to ma...

pointer variable and pointer to pointer variable in c++

  #include <iostream> using namespace std ; int main () {     //what is a pointer---> a data type which stores the address of other data types     //hmare device ki memory(ram) mein ek address assign kiya jata hai which is given by "&"     int a = 5 ;     int * b = & a;     // or     // int* b;     // b = &a;     cout << "the address of a is " << b << endl;     cout << "the address of a is " <<& a << endl;     //int*---> pointer (data type)     //&---> address of operator     //*---> dereference operator (value at)     cout << "the value at address of b is " <<* b << endl;     //pointer to pointer---> ek esa pointer jo pointer ke address ko store krta hai     int ** c = & b;     cout << "the address of b is " <...

break and continue in c++

  #include <iostream> using namespace std ; int main () {     for ( int i = 0 ; i < 5 ; i ++ )     {         // if (i==2){         //     break;         // }         if (i == 2 ){             continue ; //iska mtlb hai jese hi i ki value 2 hui usne kaha iss iteration(repetation)                       //ko rok do and next iteration pe jao mtlb neeche ab jo bi likha hai                       //use rok ke continue kro agli iteration pe                       //continue ke baad vala execute nahi hoga         }         cout << i << endl;         // if (i==2){         //   ...

for, while and do-while+infinite loops in c++

  #include <iostream> using namespace std ; int main () {     /*loops in c++:     there are 3 types of loops in c++:     1)For loop     2)while loop     3)Do-while loop*/         //For loop in c++:         // let me say i wanna print numbers upto 50 so i can do the following things     // #printing numbers just by cout     // cout<<1;     // cout<<2;     // cout<<3;     // and soo on       // #or     // int i=1;     // cout<<i;     // i++;     // cout<<i;     // i++;     // and soo on       // #or we can use loops for it, for very simplification     // for (int i = 0; i <= 50; i++)     // {     //     code     //     cout<<i<<endl;   ...

control structure: if else ladder and switch case

  #include <iostream> using namespace std ; int main () {     // selection control structure: if else-if... else ladder     int age;     cout << "Tell me your age" << endl;     cin >> age;     // if ((age < 18) && (age>0)) {     //     cout << "You are a child and you cannot come to my party" << endl;     // }     // else if (age == 18) {     //     cout << "You are a kid and you will get a kid pass to my party" << endl;     // }     // else if (age<1) {     //     cout<<"you are not yet born";     // }     // else if (age>100) {     //     cout<<"you are too old to come to the party";     // }     // else {     //     cout << "You are an adult and you can come to...

constants, manipulators and operator precedence in c++

  #include <iostream> #include <iomanip> using namespace std ; int main (){     // int a=5;     // cout<<"the value of a was "<<a<<endl;     // a=7;     // cout<<"the value of a is "<<a<<endl;     //here the value of a changed from 5 to 7     //constants in c++ (to not let change the value of a we use constants)         // const int a=5; The  const variable  cannot be left un-initialized at the time of the declaration.     // cout<<"the value of a was "<<a<<endl;     // a=7;     // cout<<"the value of a is "<<a<<endl;     //here it will show an error because a is a constant     //manipulators in c++     //manipulators vo operators hote h jo data display ki formatting m help krte h for ex:endl     int a = 5 , b = 57 , c = 5757 ;   ...

scope resolution operator, literals, reference variables, typecasting in c++

  #include <iostream> using namespace std ; int c = 15 ; //global variable int main (){     int a , b ; //local variables (preference is given to local variables)     cout << "enter the value of a: " << endl ;     cin >> a ;     cout << "enter the value of b: " << endl ;     cin >> b ;     int c = a + b ;     cout << "the sum is " << a + b << endl ;     cout << "the value of c is " << :: c << endl ; //this (::) is scope resolution operator, it is used to print a global variable inside the function     //LITERALS IN C++     //float x=5.7;     //long double y=5.7; //now 5.7 is a double by default by the compiler, we can use specific keywords to make a variable float double or long double     //cout<<"the value of x is "<<x<<endl<<"the value of y is "<...

header files and operators(syntax)

  #include <iostream> // iostream is a system header file which inhance the functionality of program // there are two types of header files // 1. system header files: it comes with the compiler // 2. user defined header file: it is written by the prgrammer #include "this.h" //this is user defined header file //#include "this.h"-->> this will produce an error if this.h is not present in the current directory //we can also use endl for jumping into another line but it should be red by the compiler using namespace std ; int main (){     cout << "operators in c++" << endl ;     cout << "following are the operators in c++" << endl ;     //arithematic operators     int a = 5 ;     int b = 7 ;     cout << "the value of a+b is " << a + b << endl ;     cout << "the value of a-b is " << a - b << endl ;     cout << "the value of a...

sum code

  #include <iostream> using namespace std ; int main (){     int a ;     int b ;     cout << "enter the value of a : \n " ;     // <<this is insertion operator, >>this is extraction operator     // cout is used for making an output, cin is used for extracting an input     cin >> a ;     cout << "enter the value of b : \n " ;     cin >> b ;     cout << "the sum is " << a + b ;         return 0 ; }

basics c++, void function, mindset of compiler

  #include <iostream> using namespace std ; // or use "std::" before cout /*this is a multiline comment*/ /*comment is not red by the compiler. number of spaced lines does not matter in c++*/ int a = 9 ; void krishna (){     int a = 8 ;     cout << a ; } int main (){     int a = 5 ;     a = 7 ;     int b = 8 ;     krishna ();     cout << a ;     char coding = 'd' ;     cout << coding ;     double pi = 3.14159 ;     cout << pi ;     bool kssk = false ;     cout << kssk ;     // long int k = 1234567890123456789;     // int s = 7;     // int ks = 5;     // float a = 1.22;     // // cout<<"the value of k is "<<k<<". the value of s is "<<s<<". the value of ks is "<<ks;     // cout<<"the value     // f k...

hello world code

Image
  #include <iostream> using namespace std ; // or use "std::" before cout_function /*this is a multiline comment*/ /*comment is not red by the compiler how many lines space does not matter in c++*/ int main () {     cout << "hello world" ;     return 0 ; }

basic sum code c++

  #include <iostream> using namespace std ; int main () {     #ifndef ONLINE_JUDGE         freopen ( "input.txt" , "r" , stdin );         freopen ( "output.txt" , "w" , stdout );     #endif             int a , b ;     cin >> a >> b ;     cout << a + b << " \n " ;     return 0 ; }