Some C++ programs

Posted on 51 Comments

Update:
As requested by Sajith Karingat (comment #29), I worked on a Phone Billing System in C++, as per his requirements. You can download it here.

I have entered my final year in my B.Tech course, and with it, it’s placement time. Companies for the computer science stream (my stream) will start visiting my college beginning from 11th August. The first to come will be TCS (Tata Consultancy Services) which is considered as a good IT firm in India with global fame.

As part of my prep for the placements, obviously, I am revising my concepts (technical ones). Today, as it was raining heavily in the college, we sat in our central canteen, waiting for the rain to stop. During that time, we held a heated discussion on C++ programs that were most likely to be asked in interviews for placements.

Amongst those programs, one was to swap the values of two variables “a” and “b” without using the “temp” variable. Another interested one we discussed was that would generate a pattern like:

1
2 3
4 5 6
7 8 9 10
11 12 13 14
…… and so on.

When I got back home, I decided to have a go on these programs. And so, here are my answers to above problems.

Swapping of “a” and “b” without “temp”

//This program runs only in Turbo C++
#include<iostream.h>
#include<conio.h>
 
void main()
{
	int a,b;
	clrscr();
	cout<<"Enter 'a': ";
	cin>>a;
	cout<<"\nEnter 'b': ";
	cin>>b;
	a = a + b;
	b = a - b;
	a = a - b;
	cout<<"\n\nThe SWAPED numbers are: "<<endl;
        cout<<"a = "<<a;
        cout<<"\n b = "<<b;
        getch();
}

Pattern generation

//pattern.cpp
//Author: Anurag Bhandari
//Used to display the type of pattern asked in TCS interview
//To run it, I recommend using gcc (on Linux) or Dev-C++ on Windows
#include<iostream>
 
/* Each and every entity (classes, objects, functions) in C++
libs (like iostream) are defined within the "std" namespace */
using namespace std;
 
int main()
{
    int r,c; //we declare the row and column variables
    int count = 1; //the "count" variable goes on incrementing each time an element is printed
    for(r=1;r<=10;r++) //loop for rows
    {
                      for(c=1;c<=r;c++) //loop for columns
                      {
                                       cout<<" "; //we print the elements
                                       count = count + 1; //we increment the counter
                      }
                      cout<<"\n"; //this is a new line after a row
    }
   /* use system("PAUSE") here which is the equivalent of getch(), except that it's a bit advanced*/
    return(0); //main() HAS to return something to be compatible with modern compilers (gcc)
}

51 thoughts on “Some C++ programs

  1. sir… i wants to write c++ program on resturant managment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.