Thursday, May 21, 2020

Function Overloading in C++


  • Program :

#include <iostream>
using namespace std;
int c(int x, int y)
{
  return x + y;
}

double d(double x, double y)
{
  return x + y;
}

int main()
{
  cout <<"\n----------------FUNCTION OVERLOADING----------------\n";
  int a = c(8, 5);
  double b = d(4.3, 6.26);
  cout << "Int: " << a << "\n";
  cout << "Double: " << b;
  return 0;
}


Pass By Reference using functions in C++


  • Program :

#include <iostream>
using namespace std;
void swapNums(int &x, int &y)
{
  int z = x;
  x = y;
  y = z;
}


int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Pass By Reference----------------\n";
  int a = 10, b = 5;
  cout << "Before swap: " << "\n";
  cout << a <<" and "<< b << "\n";

  swapNums(a, b);

  cout << "After swap: " << "\n";
  cout << a <<" and "<< b << "\n";

  return 0;
}


Return Values using functions in C++


  • Program :

#include <iostream>
using namespace std;
int myFunction(int x) {
  return 10 + x;
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Return Values----------------\n";
  cout << myFunction(8);
  return 0;
}


Multiple Parameters using functions in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
void myFunction(string fname, int age) {
  cout << fname << " Venkat" << age << " years old. \n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Multiple Parameters----------------\n";
  myFunction("Monisha", 3);
  myFunction("Janani", 14);
  myFunction("Ramya", 30);
  return 0;
}


Default Parameters using functions in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
void myFunction(string country = "Norway") {
  cout << country << "\n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Default Parameters----------------\n";
  myFunction("Sweden");
  myFunction("India");
  myFunction();
  myFunction("USA");
  return 0;
}

Parameters and Arguments using function in C++

  • Syntax :

     void functionName(parameter1parameter2parameter3
        {
      // code to be executed        }

  • Program :

#include <iostream>
using namespace std;
void myFunction(string name) {
  cout << name << " Venkat\n";
}

int main(){
  cout <<"\n----------------FUNCTION PARAMETERS----------------\n";
  cout <<"\n----------------Parameters and Arguments----------------\n";
  myFunction("Monisha");
  myFunction("Janani");
  myFunction("Ramya");
  return 0;
}


Call a function & function declaration,definition in C++


  • Syntax :

     void myFunction() 
        { // declaration
      // the body of the function (definition)        }
  • Program :

#include <iostream>
using namespace std;
// Function declaration
void myFunction();
int main(){
  cout <<"\n----------------FUNCTIONS----------------\n";
  cout <<"\n----------------Function Declaration & Definition,Create & Call a Function----------------\n";
    myFunction();  // call the function
  return 0;
}

// Function definition
void myFunction() {
  cout << "C++ Programming";
}


Modify Pointer Value in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
int main(){
  cout <<"\n----------------POINTERS----------------\n";
  cout <<"\n----------------Modify Pointer Value----------------\n";
   string food = "Pizza";
  string* ptr = &food;

  // Output the value of food
  cout << food << "\n";

  // Output the memory address of food
  cout << &food << "\n";

  // Access the memory address of food and output its value
  cout << *ptr << "\n";
 
  // Change the value of the pointer
  *ptr = "burger";
 
  // Output the new value of the pointer
  cout << *ptr << "\n";
 
  // Output the new value of the food variable
  cout << food << "\n";
  return 0;
}

Dereferencing pointer in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
int main(){
  cout <<"\n----------------POINTERS----------------\n";
  cout <<"\n----------------Dereferencing----------------\n";
   string dish = "Briyani";  // Variable declaration
  string* ptr = &dish;    // Pointer declaration
  // Reference:
  cout << ptr << "\n";
  // Dereference:
  cout << *ptr << "\n";
  return 0;
}


Creating pointers in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
int main(){
  cout <<"\n----------------POINTERS----------------\n";
  cout <<"\n----------------Creating Pointers----------------\n";
  string snacks = "Pani puri";
  cout << snacks<< "\n";
  cout << &snacks << "\n";
  return 0;
}

References in C++

  • Program :
#include <iostream>
#include <string>
using namespace std;
int main(){
  cout <<"\n----------------REFERENCES----------------\n";
  cout <<"\n----------------Creating References----------------\n";
  string food = "Pizza";
  string &meal = food;
  cout << food << "\n";
  cout << meal << "\n";
 
  cout <<"\n----------------Memory Address----------------\n";
  string dish = "Briyani";
  cout << &dish;
  return 0;
}

Arrays in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;
int main(){
   cout <<"\n----------------ARRAYS----------------\n";
   cout <<"\n----------------Access the Elements of an Array----------------\n";
   string fruits[4] = {"Apple", "Orange", "Bannana", "Grapes"};
   cout << fruits[3];
 
   cout <<"\n----------------Change an Array Element----------------\n";
   string juice[4] = {"Mazza", "Bovanto", "Mango", "Fanta"};
   juice[1] = "Slice";
   cout << juice[1];
 
   cout <<"\n----------------Loop Through an Array----------------\n";
   string subject[4] = {"Tamil", "English", "Maths", "Science"};
  for(int i = 0; i < 4; i++)
  {
    cout << subject[i] << "\n";
  }
 
  cout <<"\n----------------Add Element----------------\n";
  string district[5] = {"Coimbatore", "Chennai", "Erode"};
  district[3] = "Bangalore";
  district[4] = "Madurai";
  for(int i = 0; i < 5; i++)
  {
  cout << district[i] << "\n";
  }
 
  cout <<"\n----------------Omit Elements on Declaration----------------\n";
  string Phone[5];
  Phone[0] = "Vivo";
  Phone[1] = "Samsung";
  Phone[2] = "Iphone";
  Phone[3] = "Lava";
  Phone[4] = "Nokia";
  for(int i = 0; i < 5; i++)
  {
    cout << Phone[i] << "\n";
  }
  return 0;
}


Continue statement in C++


  • Program :

#include <iostream>
using namespace std;
int main(){
    cout <<"\n----------------CONTINUE STATEMENT----------------\n";
   for (int i = 0; i < 10; i++) {
    if (i == 4) {
      continue;
    }
    cout << i << "\n";
  } 
  return 0;
}

Break statement in C++


  • Program :

#include <iostream>
using namespace std;
int main(){
    cout <<"\n----------------BREAK STATEMENT----------------\n";
   for (int i = 0; i < 10; i++) {
    if (i == 4) {
      break;
    }
    cout << i << "\n";
  }
  return 0;
}


For loop in C++


  • Syntax :

     for (initialization; condition; increment(or)decrement
        {
      // code block to be executed
        }

  • Program :

#include <iostream>
using namespace std;
int main(){
    cout <<"\n----------------FOR LOOP----------------\n";
   for (int i = 0; i <= 10; i = i + 2) {
    cout << i << "\n";
  }
  return 0;
}


Do-while loop in C++


  • Syntax :

       do 
       {
       // code block to be executed       }
       while (condition);

  • Program :

#include <iostream>
using namespace std;
int main(){
    cout <<"\n----------------DO-WHILE LOOP----------------\n";
   int i = 0;
  do {
    cout << i << "\n";
    i++;
  }
  while (i < 5);
  return 0;
}


While loop in C++


  • Syntax :

      while (condition
       {
      // code block to be executed       }

  • Program :

#include <iostream>
using namespace std;
int main(){
    cout <<"\n----------------WHILE LOOP----------------\n";
    int i = 0;
  while (i < 5) {
    cout << i << "\n";
    i++;
  }
  return 0;
}

Switch statement in C++


  • Syntax :

     switch(expression
        {
     case x:
     // code block
     break;
     case y:
     // code block
     break;
     default:
     // code block
        }
  • Program :
#include <iostream>
using namespace std;
int main(){
   int a,b,n;
   cout <<"\n----------------SWITCH STATEMENT----------------\n";
   cout<<"\nThe given options are : \n1.Add \n2.sub \n3.multiply \n4.divide";
   cout<<"\nEnter your choice : ";
   cin>>n;
   cout<<"\nEnter A value : ";
   cin>>a;
   cout<<"\nEnter B value : ";
   cin>>b;
   switch(n) {
      case 1:
        cout<<"\n "<<a<<" + "<<b<<" = "<<a+b;
        break;
      case 2:
         cout<<"\n "<<a<<" - "<<b<<" = "<<a-b;
         break;
      case 3:
         cout<<"\n "<<a<<" * "<<b<<" = "<<a*b;
         break;
      case 4:
         cout<<"\n "<<a<<" / "<<b<<" = "<<a/b;
         break;
      default:
        cout<<"\nError";
        break;
   }
   return 0;
}

Wednesday, May 20, 2020

Short hand if-else statement in C++


  • Syntax :

     variable = (condition) ? expressionTrue : expressionFalse;

  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------SHORT HAND IF-ELSE STATEMENT----------------\n";
  int a;
  cout<<"\nEnter your age :";
  cin>>a;
  string c = (a < 18) ? "Minor" : "Major";
  cout << c;
  return 0;
}


Else-if statement in C++


  • Syntax :


     if (condition1
        {
       // block of code to be executed if condition1 is true
        } 
      else if (condition2
        {
      // block of code to be executed if the condition1 is false and           condition2 is true
        } 
      else 
        {
     // block of code to be executed if the condition1 is false and     condition2 is false
        }


  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------ELSE-IF STATEMENT----------------\n";
  int a;
  cout<<"\nEnter your age :";
  cin>>a;
  if (a <= 18) {
    cout <<"\nYou are not eligible for applying for the job";
  }
  else if (a <= 45) {
    cout <<"\nYou are eligible for applying for the job";
  }
  else {
    cout << "\nYou are retired from the job";
  }
  return 0;
}

Else statement in C++

  • Syntax :


      if (condition
         {
         // block of code to be executed if the condition is true
         } 
        else 
         {
        // block of code to be executed if the condition is false
         }

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------ELSE STATEMENT----------------\n";
  int a;
  cout<<"\nEnter your age :";
  cin>>a;
  if (a >= 18) {
    cout <<"\nMajor";
  }
  else  {
    cout <<"\nMinor";
  }
  return 0;
}


If statement in C++


  • Syntax :

    
      if (condition)
        {
        // block of code to be executed if the condition is true
        }


  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------IF STATEMENT----------------\n";
  int a;
  cout<<"\nEnter your age :";
  cin>>a;
  if (a >= 18) {
    cout << "Major";
  } 
  return 0;
}


Boolean expressions in C++


  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------BOOLEAN EXPRESSIONS----------------\n";
  int x = 15;
  int y = 10;
  cout <<"\n"<<x<<" > "<<y<<" is "<<(x > y);
  cout <<"\n"<<x<<" == "<<y<<" is "<<(x == y);
  return 0;
}


Math functions in C++


  • Program :

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  cout <<"\n----------------MATH FUNCTIONS----------------\n";
  cout <<"\nThe max of two numbers is "<< max(10, 100);
  cout <<"\nThe min of two numbers is "<< min(5, 20);
  cout <<"\nThe sqrt is "<<sqrt (64);
  cout <<"\nThe round is "<< round(2.6);
  cout <<"\nThe log is "<< log(2) ;
  return 0;
}


String operations in C++


  • Program :

#include <iostream>
#include <string>
using namespace std;

int main() {
  cout <<"\n----------------STRING OPERATIONS----------------\n";
  string a = "C++ ";
  string b = "Programming";
  string c = a.append(b);
  string d = "Class";
  string e = "Cow";
  e[0] = 'W';
  string name;
  cout <<"\nString Concatenation is "<<c;
  cout <<"\nThe length of the string is " << b.length();
  cout <<"\nThe accessed characters in a string is " << d[0];
  cout <<"\nThe changed string character is " << e;
  cout <<"\n----------------USER INPUT STRINGS----------------\n";
  cout <<"\nEnter your full name: ";
  cin >> name;
  cout <<"\nYour name is : " << name;
  return 0;
}


Logical operators in C++

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------LOGICAL OPERATORS----------------\n";
  int x = 8;
  cout <<"\n("<<x<<" > 3 && "<<x<<" < 10 ) is "<<(x > 3 && x < 10); // returns true (1)
  cout <<"\n("<<x<<" > 3 || "<<x<<" < 10 ) is "<<(x > 3 || x < 4); // returns true (1)
  cout <<"\n (!("<<x<<" > 3 && "<<x<<" < 10 )) is "<<(!(x > 3 && x < 10)); // returns false (0)
  return 0;
}


Comparision operators in C++

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------COMPARISON OPERATORS----------------\n";
  int x = 10;
  int y = 13;
  cout <<"\n"<<x<<" == "<<y<< " is "<<(x == y); // returns 0 (false)
  cout <<"\n"<<x<<" != "<<y<< " is "<<(x != y); // returns 1 (true)
  cout <<"\n"<<x<<" > "<<y<< " is "<<(x > y); // returns 1 (true)
  cout <<"\n"<<x<<" < "<<y<< " is "<<(x < y); // returns 0 (false)
  cout <<"\n"<<x<<" >= "<<y<<" is "<<(x >= y); // returns 1 (true)
  cout <<"\n"<<x<<" <= "<<y<<" is "<<(x <= y); // returns 0 (false)
  return 0;
}

Assignment operators in C++


  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------ASSIGNMENT OPERATORS----------------\n";
  int a = 5;
  int b = 10;
  b += 3;
   int c = 15;
  c -= 3;
  int d = 20;
  d *= 3;
   double e = 25;
  e /= 3;
  int f = 30;
  f %= 3;
  int g = 35;
  g &= 3;
  int h = 40;
  h |= 3;
  int i = 45;
  i ^= 3;
   int j = 50;
  j >>= 3;
   int k = 55;
  k <<= 3;
  cout <<"\n a = "<< a;
  cout <<"\n a += "<< b;
  cout <<"\n a -= "<< c;
  cout <<"\n a *= "<< d;
  cout <<"\n a /= "<< e;
  cout <<"\n a %= "<< f;
  cout <<"\n a &= "<< g;
  cout <<"\n a |= "<< h;
  cout <<"\n a ^= "<< i;
  cout <<"\n a >>= "<< j;
  cout <<"\n a <<= "<< k;
  return 0;
}


Arithmetic operators in C++

  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------ARITHMETIC OPERATORS----------------\n";
  int x = 10;
  int y = 5;
  int a = 6;
  ++a;
  int b = 6;
  --b;
  cout <<"\n----------------ADDITION----------------\n";
  cout << x + y;
  cout <<"\n----------------SUBTRACTION----------------\n";
  cout << x - y;
  cout <<"\n----------------MULITIPLICATION----------------\n";
  cout << x * y;
  cout <<"\n----------------DIVISION----------------\n";
  cout << x / y;
  cout <<"\n----------------MODULUS----------------\n";
  cout << x % y;
  cout <<"\n----------------INCREMENT----------------\n";
  cout << a;
  cout <<"\n----------------DECREMENT----------------\n";
  cout << b;
  return 0;
}


Data types in C++

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------DATA TYPES----------------\n";
  // Creating variables
  int a = 5;             
  float b = 5.99;   
  double c = 9.98; 
  char d = 'D';       
  bool e = true;     
  string f = "Hello"; 
 
  // Print variable values
  cout << "int: " << a << "\n";
  cout << "float: " << b << "\n";
  cout << "double: " << c << "\n";
  cout << "char: " << d << "\n";
  cout << "bool: " << e << "\n";
  cout << "string: " << f << "\n";

  return 0;
}


Print user input in C++

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------PRINT USER INPUT----------------\n";
  int x, y,sum;
  cout << "A value : ";
  cin >> x;
  cout << "B value : ";
  cin >> y;
  sum = x + y;
  cout << "Sum is: " << sum;
  return 0;
}


Declaring variable using constant datatype in C++

  • Syntax :


     type variable = value;

  • Program :

#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------DECLARING VARIABLE----------------\n";
  //The value is constant it can not be changed
  const int minutesPerHour = 60;
  const float PI = 3.14;
  cout << minutesPerHour << "\n";
  cout << PI;
  return 0;
}


Declaring variables in C++

  • Syntax :


     type variable = value;

  • Program :
#include <iostream>
using namespace std;

int main() {
  cout <<"\n----------------DECLARING VARIABLE----------------\n";
  int x = 5, y = 6, z = 50; 
  cout << x + y + z;
  return 0;
}

Print statement by omitting namespace in C++

  • Program :
#include <iostream>
#include <string>
using namespace std;

int main() {
  cout <<"\n----------------OMITTING NAMESPACE----------------\n";
  std::string a = "Hello";
  std::cout << a;
  return 0;
}


Print statement in C++


  • Program :

#include <iostream>
using namespace std;
int main()
{
  cout << "\n----------------PRINT STATEMENT----------------";
  cout << "\nHello World!";
  return 0;
}


Find GCD of numbers in C

  • Program :
#include <stdio.h>
int main()
{
    int n1, n2, i, gcd;
    printf("\n----------------GCD OF NUMBERS----------------");
    printf("\nEnter two integers: ");
    scanf("%d %d", &n1, &n2);

    for(i=1; i <= n1 && i <= n2; ++i)
    {
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }

    printf("\nG.C.D of %d and %d is %d", n1, n2, gcd);

    return 0;
}


Find the string is palindrome or not in C

  • Program :
#include <stdio.h>
#include <string.h>
void isPalindrome(char str[])
{
 
    int l = 0;
    int h = strlen(str) - 1; 
    while (h > l)
    {
        if (str[l++] != str[h--])
        {
            printf("\n%s is Not Palindrome", str);
            return;
        }
    }
    printf("\n%s is palindrome", str);
}
int main()
{
    printf("\n----------------PALINDROME OR NOT----------------");
    isPalindrome("madam");
    isPalindrome("moni");
    isPalindrome("wow");
    return 0;
}


Transpose of matrix in C


  • Program :

#include <stdio.h>
int main()
{
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("\n----------------TRANSPOSE OF MATRIX----------------"); 
    printf("\nEnter rows and columns: ");
    scanf("%d %d", &r, &c);

   
    printf("\nEnter matrix elements");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("\nEnter element A %d%d: ", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }

 
    printf("\nEntered matrix: \n");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("%d  ", a[i][j]);
            if (j == c - 1)
                printf("\n");
        }

 
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            transpose[j][i] = a[i][j];
        }

   
    printf("\nTranspose of the matrix:\n");
    for (i = 0; i < c; ++i)
        for (j = 0; j < r; ++j) {
            printf("%d  ", transpose[i][j]);
            if (j == r - 1)
                printf("\n");
        }
    return 0;
}


Find prime number or not in C


  • Program :

#include <stdio.h>
int main()
{
    int n, i, flag = 0;
    printf("\n----------------PRIME OR NOT----------------"); 
    printf("\nEnter a positive integer: ");
    scanf("%d", &n);

    for (i = 2; i <= n / 2; ++i) {

        if (n % i == 0) {
            flag = 1;
            break;
        }
    }

    if (n == 1) {
        printf("\n1 is neither prime nor composite.");
    }
    else {
        if (flag == 0)
            printf("\n%d is a prime number.", n);
        else
            printf("\n%d is not a prime number.", n);
    }

    return 0;
}


Factorial of numbers in C


  • Program :

#include <stdio.h>
int main()
{
    int n, i;
    unsigned long long fact = 1;
    printf("\n----------------FACTORIAL OF NUMBERS----------------"); 
    printf("\nEnter an integer: ");
    scanf("%d", &n);

    if(n<0)
        printf("\nError! Factorial of a negative number doesn't exist.");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("\nFactorial of %d = %llu", n, fact);
    }

    return 0;
}


Fibonacci series in C

  • Program :
#include <stdio.h>
int main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm;
    printf("\n----------------FIBONACCI SERIES----------------"); 
    printf("\nEnter the number of terms: ");
    scanf("%d", &n);
    printf("\nFibonacci Series: ");

    for (i = 1; i <= n; ++i)
{
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
}

    return 0;
}


Print star in C

  • Program :
#include <stdio.h>
int main()
{
  int row, c, n;
  printf("\n----------------PRINT STAR----------------"); 
  printf("\nEnter the number of rows in pyramid of stars to print:");
  scanf("%d", &n);

  for(row=1;row<=n;row++) 
  {
    for(c=1;c<=n-row;c++) 
      printf(" ");

    for (c=1;c<=2*row-1;c++)
      printf("*");

    printf("\n");
  }

  return 0;
}


Find even or odd number in C

  • Program :
#include <stdio.h>
int main()
{
 
    int num;
    printf("\n----------------EVEN NO OR NOT----------------"); 
    printf("\nEnter an integer number: ");
    scanf("%d",&num);
    if(num%2==0)
        printf("\n%d is an EVEN number.",num);
    else
        printf("\n%d is an ODD number.",num);

    return 0;
}


Find leap year or not in C

  • Program :
#include <stdio.h>
int main()
{
    int year;
    printf("\n----------------LEAP YEAR OR NOT----------------"); 
    printf("Enter a year: ");
    scanf("%d", &year);
    if(year%4==0)
{
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
}     
    return 0;
}

Multiplication table in C

  • Program :
#include <stdio.h>
int main()
{
    int number, i = 1;
    printf("\n----------------MULTIPLICATION TABLE----------------"); 
    printf("\n Enter the Number:");
    scanf("%d", &number);
    printf("\nMultiplication table of %d:\n ", number);
    printf("--------------------------\n");
    while (i <= 10)
    {
        printf(" \n%d x %d = %d \n ", number, i, number * i);
        i++;
    }
    return 0;
}


Reversing the numbers in C



  • Program :

#include<stdio.h>
int main()
{
int n,a,r=0;
printf("\n----------------REVERSED NUMBER----------------"); 
printf("\nEnter any number to be reversed :");
scanf("%d",&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf("\nreverse : %d",r);
return 10;
}


Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...