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. yhanku

  2. thank u sir your guidelines helped me but i want to prepare
    a program on garment shop management will u help

  3. Somya,

    Nice that my blog post helped you out. And I am afraid, due to time constraints I cannot offer you help in your personal project. But you could seek help from an appropriate place, like some programming forum? Say – http://www.daniweb.com

    Anyway, if you are thinking about making a garment management application, I think you should use a different language for it, other than C++, like Python, Java, C#, etc., as you’ll have better options then.

  4. sir ,
    i m a student of 12th
    i have got problem in one program,…..wish u could help…
    it is an easier job for u

    question is

    A class telcall calculates the monthly phone bill of a consumer. Some of the members of the class are given below [10]
    Class name telcall
    Data member/instance variables
    phno phone number
    name name of the consumer
    n number of calls made
    amt bill amount
    memberfunctions/methods
    telcall() parameterized constructor to assign
    values to data members
    void compute() to calculate the phone bill amount on the
    slabs given below
    void dspdata() to display the details in the specified
    format

    Number of calls Rate
    1-100 Rs. 500/-rental charge only
    101-200 Rs. 1.00 per call + rental charge
    201-300 Rs. 1.20 per call + rental charge
    above 300 Rs. 1.50 per call + rental charge

    The calculationsneed to be done as per slab.
    Specify the class telcall, giving the details of the constructor, void compute(), and void dispdata(). In the main function, create an object of type telcall and display the phone bill in the following format
    Phone Number Name Total Calls Amount
    xxxxxxxxxxxx xxxxx xxxxxxxxx xxxxxxx

  5. Gautam,

    The program is not very difficult, but quite long one to be mentioned here. But I could email you the main parts of the program anyway.

  6. Sir,

    I want to prepare a program for getting the restaurant bill.Is it possible using the c++ program? If it is then please guide me i’ll be very thankful to you.

  7. Sir,
    I want to prepare for CATIA and PRO-E is it necessary for me to take classes can’t i use my time by myself studying on my own?

    If it is possible then can you recommend me some good books and websites where from i can get proper help.

    Thank you.

  8. Tufail,

    Yes, it is possible to write a program in C++ that would calculate a restaurant’s bill. This could be done in many ways, but the best method would be to use “classes” which would contain data variables and member functions to manipulate those variables related to bill.

    And regarding CATIA and PRO-E, I am sorry, I haven’t heard of it, so cannot help you here.

  9. Thnk u sir,but if u can write a program on any topic….cricket….food….cars….

  10. Sharmistha,

    What kind of program do you want me to write? If you want help in some particular topic, maybe I can help.

  11. Hey cool program , i use to make it when i was doing my comp. science.

  12. @Ruchi
    Well, coding is always fun. 🙂

  13. very interesting logic in swapping the numbers
    i liked it.

  14. @nikhil
    Yes, that is an elegant way of swapping two numbers, without using a temporary variable.

  15. thanks sir but can u give me a project of c++ of 50-60 pages

  16. @ankit
    What kind of project are you looking for?

  17. on any topic like cricket,quiz,games

  18. Input two numbers from the keyboard.WAP to find the value of one number raisedto the power of another.

  19. Write a menu driven program which has following options:
    1.Factorial of a number
    2.Prime or not
    3.Odd or Even
    4.Eit

  20. hi sir ,i am willing to learn languages like c, c++ with more efficiency. which book shall i follow to learn those languages….pls

  21. ankit :

    Input two numbers from the keyboard.WAP to find the value of one number raisedto the power of another.

    This is quite an easy program and would take less space in writing.

    #include<conio.h>
    #include<iostream.h>
    #include<math.h>
     
    void main()
    {
      clrscr();
      int num, expo;
      cout<<"Enter the number: ";
      cin>>num;
      cout<<"nEnter the exponent: ";
      cin>>expo;
      cout<<"nnThe result is: "<<pow(num,expo);
      getch();
    }
  22. nivetha :

    hi sir ,i am willing to learn languages like c, c++ with more efficiency. which book shall i follow to learn those languages….pls

    For leaning C++, I recommend Balaguruswami and Robert Lafore.

  23. Aur sir second one

  24. WAP to convert a binary number to the decimal number
    second program is:
    WAP to convert a decimal number to equivalent binary number(fractions also)

  25. @ankit
    These are very common programs. You’ll easily find source code for these if you do a simple Google search on them. 🙂

  26. nahi mil rahe

  27. If a number 972 is entered through the keyboard, your program should print “Nine Seven Two”.WAP the prgrm such that it does this for any +ve integer

  28. sir prjct bana kya

  29. hai….i think u r doing a real good job here…and i suppose this is a wonderfull place to seek help..i m a student from singapore doing my electronics eng..

    so let me ask for ur kind help with this program …….i am not so sure how to finish this one..!!!
    IT WILL BE OF GREAT HELP IF YOU CAN MAIL ME THE CODE FOR THIS…AND FAVOURABLY WIF THE COMMENT LINES SO THAT I CAN UNDERSTAND HW IT WORKS..THNXX!!

    here is the ” prob.statement”[i was asked to use classes and to impliment as a multi-file program.]
    //i am using visual studio 2005****

    Assume that ABC telephone corporation offers three types of services for customers as
    shown in the table below.
    Home line Long Distance call Mobile phone line
    Monthly subscription $10 $5 $25
    Free calls (minutes) – – 100
    Excess airtime charge $0.08/min ($)/min lookup from
    the oversea call table
    $0.20/min
    Free SMS – – 200
    Excess SMS charge – – $0.05/sms
    The monthly subscription for home line is $10, customer will pay $0.08 per min of local
    air time. The monthly subscription for mobile phone line is $25, it comes with 100
    minutes of free local calls and 200 free SMS. Any additional air time is charged at
    $0.20/min; and additional SMS is charged at $0.05/sms.
    Customer with a home line and mobile line can subscribe to Long Distance call at an
    additional monthly subscription of $5. The rate per minute depends on the country. It is
    indicated in the Long Distance rate table.
    Design the necessary classes and member functions to achieve the following tasks :
    a. Allow user to enter customer information such as customer name, account number,
    address and type of phone line.
    b. Accept entry of the number of minutes for local call, and/or long distance call.
    c. Accept entry of the number of SMS.
    d. Compute the local air time charges, and/or long distance air time charges based on
    the destination country.
    e. Compute the total SMS charges.
    f. Compute and output the detailed phone bill to a text file.
    g. Additional features (Propose by students or given by lecturer) :
    _________________________________________________________________
    _________________________________________________________________
    Country Rate/minutes
    Australia $0.60
    Hong Kong $0.50
    Malaysia $0.20
    Indonesia $1.20
    Thailand $1.15

  30. your program is great ,……..Thank u for ur help…….
    keep up ur great job..

  31. sajith karingat :

    your program is great ,……..Thank u for ur help…….
    keep up ur great job..

    Glad to know it proved useful to you. 🙂

  32. Hello Sir,
    This has to be the most helpful blog on programming I have yet to see, and you definitely one of the most generous people, giving away all that knowledge of you. Full respect on that!

    Now, I do have a little project assigned for my first year class at programming.. and since i’m really novice to it, I don’t know of where exactly to start.

    I’m supposed to make a program for a tavern (or a bar)

    the bar-guy should be able to take a different bill off each table he serves (with the prices etc), as well as ability to pay attention to products running out of quantity. Eventually to order them. But he shouldn’t have full rights on ordering.. only mentioning the missing products. The owner of the bar should be able to make the complete order.

    also, some kinf of a database should be kept trace on..

    any idea how this all could be done ?

    thank you in advance!

  33. @Yavor Bachvarov

    You did not mention which language you need to work in. But anyway, if it’s going to be database-based, I suggest considering Java or Python. And if it is going to be a web-app, you can consider PHP as well. For the database part, you can always rely on SQLite (can be used with almost all languages).

  34. Sorry I forgot to mention.

    It “must” be in C++ . It’s a C++ clas, ..

    about the database stuff, our professor said, we should keep it simple, and do it in C++ (we can obviously use other languages as well, but he said it we shouldn’t make things more complex than what they already are.

  35. write a program to show invoice of super store .pls help me c++ program

  36. sajith karingat :hai….i think u r doing a real good job here…and i suppose this is a wonderfull place to seek help..i m a student from singapore doing my electronics eng..
    so let me ask for ur kind help with this program …….i am not so sure how to finish this one..!!!IT WILL BE OF GREAT HELP IF YOU CAN MAIL ME THE CODE FOR THIS…AND FAVOURABLY WIF THE COMMENT LINES SO THAT I CAN UNDERSTAND HW IT WORKS..THNXX!!
    here is the ” prob.statement”[i was asked to use classes and to impliment as a multi-file program.]//i am using visual studio 2005****
    Assume that ABC telephone corporation offers three types of services for customers asshown in the table below.Home line Long Distance call Mobile phone lineMonthly subscription $10 $5 $25Free calls (minutes) – – 100Excess airtime charge $0.08/min ($)/min lookup fromthe oversea call table$0.20/minFree SMS – – 200Excess SMS charge – – $0.05/smsThe monthly subscription for home line is $10, customer will pay $0.08 per min of localair time. The monthly subscription for mobile phone line is $25, it comes with 100minutes of free local calls and 200 free SMS. Any additional air time is charged at$0.20/min; and additional SMS is charged at $0.05/sms.Customer with a home line and mobile line can subscribe to Long Distance call at anadditional monthly subscription of $5. The rate per minute depends on the country. It isindicated in the Long Distance rate table.Design the necessary classes and member functions to achieve the following tasks :a. Allow user to enter customer information such as customer name, account number,address and type of phone line.b. Accept entry of the number of minutes for local call, and/or long distance call.c. Accept entry of the number of SMS.d. Compute the local air time charges, and/or long distance air time charges based onthe destination country.e. Compute the total SMS charges.f. Compute and output the detailed phone bill to a text file.g. Additional features (Propose by students or given by lecturer) :__________________________________________________________________________________________________________________________________Country Rate/minutesAustralia $0.60Hong Kong $0.50Malaysia $0.20Indonesia $1.20Thailand $1.15

    Can you also mail me the c++ code too?Thanks

  37. @James
    Sent you the code through email. 🙂

  38. sir please can u email or post a project of c++ on restaurant managment of 30 pages its urgent

  39. hai.. i’m student from malaysia doing IT..so I ask for ur kind help with this program…and it must be in C++.. Write a program that computes the cost of a long-distance call. The cost of the call is determined according to the following rate schedule:
    a. Any call started between 8:00 A.M. and 6:00 P.M., Monday through Friday, is billed at a rate of $0.40 per minute.
    b. Any call starting before 8:00 A.M. or after 6:00 P.M., Monday through Friday, is charged at a rate of $0.25 per minute.
    c. Any call started on Saturday or Sunday is charged at a rate of $0.15 per minute.

    The input will consist of
    – the day of the week
    – the time the call started
    – the length of the call in minutes
    The output will be the cost of the call. The time is to be input in 24-hour notation (e.g. the time 1:30 P.M. is input as 13:30). The day of the week will be read as one of the following pairs of character values, which are stored in two variables of type char:

    Mo Tu We Th Fr Sa Su

    Be sure to allow the user to use either uppercase or lowercase letters or a combination of the two. The number of minutes will be input as a value of type int. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

  40. sir, if u could mail me the source code for canteen management in c++ using data file handling please

  41. sirji i want c++ project on restaurant management can u help

  42. @mak
    I can only guide you in the project. Rest you’d have to develop yourself. 🙂

  43. sir i have got a project on canteen billing system in c++ using file handling!!!!
    plzzz rply soon as it is possible

  44. @Avinash: How can I help you, buddy?

  45. hey sir ….
    i m a student of 11th std
    it would be really nice of u if u can help me!!
    i need a C++ program for a “restaurant menu and bill”
    it is like- displaying a menu card for few specific items to the user and also calculating the bill of the items they have ordered…
    sir plzz rply as soon as possible 🙂

  46. sir i have got a project on bank’s account management in c++ using file handling.
    ie say about creating , depositing , withdrawing , showing current details.
    plzz revert as soon as possible.

  47. hi i came across this site and im not sure if it is still being used but anyway. I need help with a program i keep getting stuck and rewriting the code and im frustrated. Maybe you can help me.

    Design and develop a program for Long-Distance Calls. A Long-Distance carrier charges the following rates for long distance calls:

    Start Time of Call…..Rate per Minutes
    00.00-7:59…. ……….0.07
    08:00-18:59 ………….0.12
    19:00-2359……………0.28
    Your program should prompt the user and ask for he starting time and number of minutes of the call, and displays the charges. The program should ask for the starting time to be entered as floating point number in the form of HH.MM (for example 09:00 would be entered as 09.00).

    INPUT VALIDATION: Your program should not accept HH that less than 0, nor greater than 23. Minutes less than zero and greater than 59 are invalid. Number of hours and minutes should be only accepted as a positive number.

  48. Hello,..sir..Gudevening..###

    sir,..i’m bca final yr stdnt,.actually sir,i hvn’t more knwlegde of programing.bcoz i wasn’t intrstd prog.languages n m also confusd.but nw m intrstd n i want to be ur guidelines sir.n
    can u guidnc for coding of library managment .pls,sir..

  49. Hello,..sir..Gudevening..###

    sir,..i’m bca final yr stdnt,.actually sir,i hvn’t more knwlegde of programing.bcoz i wasn’t intrstd prog.languages n m also confusd.but nw m intrstd n i want to be ur guidelines sir.n
    can u guidnc for coding of library managment .pls,sir..

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.