Sunday, July 30, 2017

C++ program to read and write a word into a file.

C++ File Read Write Example

Following C++ program will read and write a word into a given file.

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

int main () {   
   char word[20];
   //Open a data file to write a word.
   ofstream fout;
   fout.open("data.txt");

   cout << "Writing a word to data file...\n";
   cout << "Enter your word: ";
   cin.getline(word, 20);

   fout << word; // write data
   fout.close(); // close file

   ifstream fin;
   fin.open("data.txt"); // open file to read

   cout << "\nReading a word from a file...\n";
   fin >> word;
   cout << word;    //write data to screen   
   fin.close();     //close file
   return 0;
}

Output of Program

Writing a word to data file...
Enter your word: hello

Reading a word from a file...
hello

Note: Loops can be used to read/write multiple words into a file.



Saturday, July 29, 2017

C++ Inheritance Example

Example of C++ Inheritance (Base class: Shape, Derived class: Square)


A class can be derived from other class. Derived class also inherits data and functions from base class. Consider a base class Shape and its derived class Square


Following C++ program demonstrate how Square class is derived from base class Shape.


#include <iostream>
using namespace std;

class Shape {
  protected:
     int height;

  public:
     void setHeight(int h){
        height = h;
     }
};

// Derived class from Shape

class Square: public Shape {
  public:
     int findArea() {
        return (height * height);
     }
};

int main(void) {
  Square Sqr;

  Sqr.setHeight(5);

  // Print the area of the Square
  cout << "Area of Square:" << Sqr.findArea();

  return 0;
}

Output of program

Area of Square: 25


* * * * *

Tuesday, July 25, 2017

C++ program with a Class

Demonstration of C++ program with a "Student" Class

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

class student
{
  int rollno;
  float percentage;
  char name[10];

  public:
      void getdata(int a, float b, char* n)
      {
  rollno = a;
  percentage = b;
  strcpy(name,n);
      }
      void putdata()
      {
  cout << "Roll no = " << rollno << "\n";
  cout << "Percentage = " << percentage << "\n";
  cout << "Name = " << name << "\n\n";
      }
};

int main()
{
  student s1, s2;

  s1.getdata(101,80.85,"ABC");
  s1.putdata();

  s1.getdata(102,95,"XYZ");
  s1.putdata();

  return 0;
}

Output of program

Roll no = 101
Percentage = 80.85
Name = ABC

Roll no = 102
Percentage = 95
Name = XYZ

Sunday, July 23, 2017

C++ program to access Data Members of a class.

This C++ program will demonstrate how to access the Data Members of a class.

#include <iostream>
using namespace std;

class Box {
   public:
      double length;   
      double breadth;  
      double height;   
};

int main( ) {
   Box MyBox;        
   double volume; //for storing volumne of box

   // MyBox initialization
   MyBox.height = 2; 
   MyBox.length = 3; 
   MyBox.breadth = 5;
   
   /* Note: public data members of objects of a class 
   can be accessed using direct member access operator "." 
   */

   // volume of MyBox
   volume = MyBox.height * MyBox.length * MyBox.breadth;
   cout << "Volume of MyBox = " << volume <<endl;

   return 0;
}

Output of program

Volume of MyBox : 30

C++ program to transpose matrix.

This C++ program will transpose given 3X3 matrix.

#include<iostream>
using namespace std;

int main()
{
 int arr[3][3], arrTrans[3][3];
 int i,j;
 //Logic for reading matrix from user...
 cout << "Enter 3 x 3 matrix:\n\n";
 for(i=0;i<3;i++)
   for(j=0;j<3;j++)
   {
    cout << "Enter element arr["<< i << "]["<< j <<"]:";
    cin >> arr[i][j];
   }  

 cout << "\nThe Original matrix is: \n";
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
     cout << "\t" << arr[i][j];
  cout << endl;
 }

 cout << endl;
//Logic for transposing matrix...
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     arrTrans[j][i] = arr[i][j];
 }
 //Logic for printing Transposed matrix...
 printf("The transpose of the matrix is: \n");
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     cout << "\t" << arrTrans[i][j];
  cout << endl;
 }
 return 0;
}

Output of program

Enter 3 x 3 matrix:

Enter element arr[0][0]:1
Enter element arr[0][1]:2
Enter element arr[0][2]:3
Enter element arr[1][0]:4
Enter element arr[1][1]:5
Enter element arr[1][2]:6
Enter element arr[2][0]:7
Enter element arr[2][1]:8
Enter element arr[2][2]:9

The Original matrix is:
        1       2       3
        4       5       6
        7       8       9

The transpose of the matrix is:
        1       4       7
        2       5       8
        3       6       9

C++ program to swap two given numbers.

This C++ program will swap the values of two given number using third temporary variable.

#include <iostream>
using namespace std;

int main()
{
    int num1, num2, temp;

    cout << "Enter number1:";
    cin >> num1;

    cout << "Enter number2:";
    cin >> num2;

    cout << "Numbers before swapping:";
    cout << "Number1 = " << num1 << ", Number2 = " << num2;

    temp = num1;
    num1 = num2;
    num2 = temp;

    cout << "\nNumbers after swapping:";
    cout << "Number1 = " << num1 << ", Number2 = " << num2;

    return 0;
}

Output of program

Enter number1:10
Enter number2:20
Numbers before swapping:Number1 = 10, Number2 = 20
Numbers after swapping:Number1 = 20, Number2 = 10



C++ program to swap two given numbers without use of temporary variable.

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

    cout << "Enter number1:";
    cin >> num1;

    cout << "Enter number2:";
    cin >> num2;

    cout << "Numbers before swapping:";
    cout << "Number1 = " << num1 << ", Number2 = " << num2;

    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;

    cout << "\nNumbers after swapping: ";
    cout << "Number1 = " << num1 << ", Number2 = " << num2;

    return 0;
}

Output of program:

Enter number1:10
Enter number2:20
Numbers before swapping:Number1 = 10, Number2 = 20
Numbers after swapping :Number1 = 20, Number2 = 10


Friday, July 21, 2017

Convert given number into word

C++ Program to convert given number (between 0 to 9) into word using switch statement.


#include<iostream>
using namespace std;

int main ()
{
 int number;

 cout << "Enter your number:";
 cin >> number;
 switch(number)
 {
  case 1 : cout << "One";
  break;
  case 2 : cout << "Two";
           break;
  case 3 : cout << "Three";
           break;
  case 4 : cout << "Four";
           break;
  case 5 : cout << "Five";
           break;
  case 6 : cout << "Six";
           break;
  case 7 : cout << "Seven";
           break;
  case 8 : cout << "Eight";
           break;
  case 9 : cout << "Nine";
           break;
  case 0 : cout << "Zero";
           break;
  default :printf("Invalid number\n" );
 }
 return 0;
}

Output of the program

Enter your number:5
Five

if-else statement in C++

C++ program to check given number is positive or not using if..else statement.


#include<iostream>
using namespace std;
int main()
{
  int number;

  cout << "Enter number:";
  cin >> number;

  if(number > 0)
      cout << "Number is positive.";
  else
      cout << "Number is not positive.";
  return 0;
}

Output of the program

Enter number:11
Number is positive.

C++ Program to find bigger number.

C++ Program to find bigger number. Program will demonstrate use of if ... statement.

#include<iostream>
using namespace std;
int main()
{
    int num1, num2;

    cout << "Enter number1:";
    cin >> num1;

  cout << "Enter number2:";
    cin >> num2;

    if(num1 > num2)
    {
        cout << "Number1 is big.";
    }
    if(num2 > num1)
    {
        cout << "Number2 is big.";
    }
    if(num1 == num2)
    {
        cout << "Number1 and Number2 are equal.";
    }
    return 0;
}

Output of program

Enter number1:5
Enter number2:10
Number2 is big.


* * * * *

Saturday, July 15, 2017

ઈમેલ મોકલવાની રીત

ઈમેલ - ઇલેક્ટ્રોનિક મેલ (ઇમેલ) એ લોકો વચ્ચે સંદેશાની આપ-લે કરવા માટેની સગવડ (એપ્લિકેશન) છે. ઈમેલના ઉપયોગથી તમે વિશ્વમાં ગમે તે સ્થળે સંદેશો મોકલી શકો છો.


ઈમેલ મોકલવાની રીત
  • સૌથી પહેલા તમારૂ એક ઈમેલ એડ્રેસ બનાવવું પડશે . Gmail અને Yahoo  વેબસાઈટ પર તમારું ઈમેલ એડ્રેસ મફતમાં બનાવી શકો છો.
  • ઈમેલ એપ્લિકેશન ચાલુ કરી તમારું લોગીન કરો. નવો મેસેજ મોકલવા માટે કંપોઝ (COMPOSE) બટન પર ક્લિક કરો.
  • જેને ઈમેલ મોકલવો હોય તેનું ઈમેલ એડ્રેસ TO ની સામેના ખાનમાં લખો. તમારા ઈમેલનો વિષય SUBJECT ની સામેના ખાનામાં લખો. તમારા ઈમેલનો મેસેજ SUBJECT નીચેના ખાનામાં લખો. 
  • મેસેજ લખાયી જાય પછી સેન્ડ (SEND) બટન પર ક્લિક કરો.



Figure: Sample Email Screen of GMAIL

  • ઉપરના ચિત્રમાં બતાવ્યા પ્રમાણે, સૌથી પહેલા COMPOSE પર ક્લિક કરો. 
  • abc@gmail.com  એ જેને ઇમેલ  મોકલવો છે તેનું એડ્રેસ છે. 
  • ઈમેલનો મેસેજ: Hello  Students ............જે SUBJECT નીચેના ખાનામાં લખેલો છે.
  • બધું લખાઈ જાય પછી SEND બટન પર ક્લિક કરો.


ઈન્ટરનેટને લગતા અગત્યના શબ્દોની માહિતી

ઈન્ટરનેટને લગતા અગત્યના શબ્દોની માહિતી નીચે આપી છે:

ઈન્ટરનેટ - ઇન્ટરનેટ વિવિધ કમ્પ્યુટરો અને ડિજિટલ ઉપકરણોનું નેટવર્ક છે. ઇન્ટરનેટ દ્વારા અલગ અલગ કોમ્પ્યુટરો અને ઉપકરણો (જેવા કે મોબાઈલ) એક બીજા સાથે માહિતીની આપ લે કરી શકે છે.

વેબસાઈટ  - એ એક સંબંધિત વેબ પાનાઓનો સમૂહ છે જે ઈન્ટરનેટ પર એક નામથી  (website name) ઓળખાય છે. yahoo.com, google.com, amazon.com, flipkart.com એ જાણીતી વેબસાઈટના નામ છે. 

બ્રાઉઝર - વેબસાઈટ જોવા માટેનો કોમ્પ્યુટર પ્રોગ્રામ છે. ગૂગલ ક્રોમ (Chrome), ઈન્ટરનેટ એક્સપ્લોરર (Internet Explorer) અને મોઝિલા ફાયરફોક્સ(Mozilla Firefox) એ જાણીતા બ્રાઉઝર છે.

એચટીએમએલ(HTML) - HTML એટલે હાયપરટેક્સ્ટ માર્કઅપ લેંગ્વેજ; જે વેબ સાઈટને બનાવવા માટેની એક કોમ્પ્યુટરની ભાષા છે.

IP એડ્રેસ - IP એટલે ઈન્ટરનેટ પ્રોટોકોલ એડ્રેસ. તેનો ઉપયોગ ઇન્ટરનેટ પર કોમ્પ્યુટરને ઓળખવા માટે થાય છે. તેને કોમ્પ્યુટરનું સરનામું પણ કહી શકાય. 192.168.1.1 એક IP એડ્રેસનું ઉદાહરણ છે.

અપલોડ - તમારા કોમ્પ્યુટરથી ડેટાને બીજા કોમ્પ્યુટર પર મોકલવાની ક્રિયાને અપલોડ કહે છે.

ડાઉનલોડ - અન્ય કોમ્પ્યુટરથી ડેટાને આપણા કોમ્પ્યુટર પર લાવવાની ક્રિયાને ડાઉનલોડ કહે છે.

ઈમેલ - ઇલેક્ટ્રોનિક મેલ (ઇમેલ) એ લોકો વચ્ચે સંદેશાની આપ-લે કરવા માટેની સગવડ (એપ્લિકેશન) છે. ઈમેલના ઉપયોગથી તમે વિશ્વમાં ગમે તે સ્થળે સંદેશો મોકલી શકો છો.


Wednesday, July 12, 2017

do-while loop in C++

C++ do-while Loop: Syntax and Examples


The do-while loop is a looping statement in C++ that executes the loop body at least once, even if the condition is false. This is because the condition is checked after executing the loop body.

Syntax

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

Key Points
  • The semicolon (;) after while(condition) is mandatory.
  • Loop body executes minimum one time
  • Condition is checked after execution
  • Useful when at least one execution is required (menus, input validation)

Example 1: Print Numbers from 1 to 5

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    do {
        cout << i << " ";
        i++;
    } while (i <= 5);
    return 0;
}

Output:
1 2 3 4 5

Example 2: Sum of First 3 Numbers

#include <iostream>
using namespace std;

int main() {
    int i = 1, sum = 0;
    do {
        sum = sum + i;
        i++;
    } while (i <= 3);

    cout << "Sum = " << sum;
    return 0;
}

Output:
Sum = 6

Example 3: C++ program to print Z to A using do..while loop.

#include<iostream>
using namespace std;
int main()
{
  char ch='Z';
  do
  {
    cout << ch;
  ch = ch - 1;
  }
  while(ch >= 'A');
  return 0;
}

Output:

ZYXWVUTSRQPONMLKJIHGFEDCBA


* * * * *

Wednesday, July 5, 2017

C++ While Loop Syntax and Examples

C++ While Loop Syntax and Examples

The while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop in C++ is used to repeatedly execute a block of code as long as a given condition is true.

Basic Syntax

The loop checks the condition before executing the block of code. If the condition is false initially, the code inside the loop will never run.

while (condition) {
    // Code to be executed while condition is true
    // Remember to update variables to eventually make the condition false
}
  • The condition must be a boolean expression
  • The loop continues until the condition becomes false

Example 1: C++ program to print 1 to 10 numbers using while loop.

#include<iostream>
using namespace std;
int main()
{
  int i=1;
  while(i<=10)
  {
    cout << i << " ";
    i++;
  }
  return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10


Example 2: Display first 5 even numbers

#include <iostream>
using namespace std;

int main() {
    int num = 2, count = 1;

    while (count <= 5) {
        cout << num << " ";
        num += 2;
        count++;
    }
    return 0;
}

Output:
2 4 6 8 10

Example 3: Sum of numbers until user enters 0

#include <iostream>
using namespace std;

int main() {
    int num, sum = 0;

    cout << "Enter numbers (0 to stop): ";
    cin >> num;

    while (num != 0) {
        sum += num;
        cin >> num;
    }

    cout << "Sum = " << sum;
    return 0;
}


* * * * *

for loop in C++

C++ for Loop: Syntax and Example 


The for loop in C++ is used to repeat a block of code a fixed number of times. It is especially useful when the number of iterations is known in advance. 

Syntax 

for (initialization; condition; increment/decrement)
{
    // statements to be executed repeatedly
}

Key points 
  • Initialization: Declares and initializes the loop control variable (runs once). 
  • Condition: Checked before each iteration. If true, the loop continues. 
  • Increment/Decrement: Updates the loop variable after each iteration. 
  • All three parts of the for loop are optional, but the semicolons (;) are required.

Program 1: C++ program to print 1 to 10 numbers using for loop.

#include<iostream>
using namespace std;
int main()
{
  int i;
  for(i=1; i<=10; i++)
  {
    cout << i << " ";
  }
  return 0;
}

// Output of the program

1 2 3 4 5 6 7 8 9 10


Program 2: Print even numbers from 1 to 20

#include <iostream>
using namespace std;

int main() {
    for(int i=2; i<=20; i=i+2) {
        cout << i << " ";
    }
    return 0;
}

// Output of the program

2 4 6 8 10 12 14 16 18 20

Program 3: Find sum of first N natural numbers

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter a number: ";
    cin >> n;

    for(int i = 1; i <= n; i++) {
        sum += i;
    }

    cout << "Sum = " << sum;
    return 0;
}

/* Output
Enter a number: 5
Sum = 15
*/


* * * * *

Saturday, July 1, 2017

Use of various Data Types in C++ program.

This program shows use various data types in C++.

#include<iostream>
using namespace std;

int main()
{
   int rollno;
   char name[20];
   float age;
   char grade;
                     
   cout << "Enter your roll no.:";
   cin >> rollno;
                     
   cout << "Enter your name:";
   cin >> name;
         
   cout << "Enter your age:";
   cin >> age;
         
   cout << "Enter your grade:";
   cin >> grade;
           
   //Logic for Printing output....
   cout << "========================\n";
   cout << "Your Roll No is " << rollno << "\n";
   cout << "Your Name is " << name << "\n";
   cout << "Your Age is " << age << "\n";
   cout << "Your Grade is " << grade << "\n";
   cout << "========================\n";
   return 0;
}

Output of program

Enter your roll no.:101
Enter your name:ABC
Enter your age:25
Enter your grade:A
========================
Your Roll No is 101
Your Name is ABC
Your Age is 25
Your Grade is A
========================


C++ program to read roll number and marks

This C++ program shows how to declare and use int variables. Program will read roll number and marks from user and display it on screen.

#include<iostream>
using namespace std;
int main()
{
 int marks, rollno;

 cout << "Enter your Roll No:";
 cin >> rollno;

 cout << "Enter your Marks:";
 cin >> marks;

 cout << "\nYour Roll No. is " << rollno << "\n";
 cout << "Your marks are " << marks;
 return 0;
}

Output of program

Enter your Roll No:101
Enter your Marks:90

Your Roll No. is 101
Your marks are 90