Home > Review, c++, fedora, interview, programming, python > Some C++ programs

Some C++ programs

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)
}
  1. somya
    November 15th, 2008 at 19:13 | #1

    yhanku

  2. somya
    November 15th, 2008 at 19:15 | #2

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

  3. November 15th, 2008 at 20:25 | #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. gautam
    December 16th, 2008 at 19:13 | #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. December 16th, 2008 at 19:26 | #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. TUFAIL SHEIKH
    December 21st, 2008 at 11:56 | #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. TUFAIL SHEIKH
    December 21st, 2008 at 11:59 | #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. December 21st, 2008 at 12:08 | #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. Sharmistha
    January 17th, 2009 at 13:05 | #9

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

  10. January 17th, 2009 at 17:07 | #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. May 1st, 2009 at 22:01 | #11

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

  12. May 1st, 2009 at 22:37 | #12

    @Ruchi
    Well, coding is always fun. :)

  13. nikhil
    May 12th, 2009 at 16:58 | #13

    very interesting logic in swapping the numbers
    i liked it.

  14. May 13th, 2009 at 00:11 | #14

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

  15. ankit
    July 4th, 2009 at 21:15 | #15

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

  16. July 4th, 2009 at 22:25 | #16

    @ankit
    What kind of project are you looking for?

  17. ankit
    July 6th, 2009 at 23:27 | #17

    on any topic like cricket,quiz,games

  18. ankit
    July 6th, 2009 at 23:48 | #18

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

  19. ankit
    July 6th, 2009 at 23:50 | #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. nivetha
    July 7th, 2009 at 05:56 | #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. July 7th, 2009 at 08:47 | #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<<"\n\nThe result is: "<<pow(num,expo);
      getch();
    }
  22. July 7th, 2009 at 12:31 | #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. ankit
    July 7th, 2009 at 19:53 | #23

    Aur sir second one

  24. ankit
    July 7th, 2009 at 20:09 | #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. July 7th, 2009 at 21:05 | #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. ankit
    July 7th, 2009 at 21:27 | #26

    nahi mil rahe

  27. ankit
    July 7th, 2009 at 21:44 | #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. ankit
    July 7th, 2009 at 22:24 | #28

    sir prjct bana kya

  29. sajith karingat
    August 1st, 2009 at 08:21 | #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. sajith karingat
    August 3rd, 2009 at 16:12 | #30

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

  31. August 3rd, 2009 at 19:43 | #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. Yavor Bachvarov
    March 10th, 2010 at 19:29 | #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. March 10th, 2010 at 19:49 | #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. Yavor Bachvarov
    March 10th, 2010 at 21:18 | #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. June 17th, 2010 at 19:46 | #35

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

  36. James
    July 27th, 2010 at 12:54 | #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. July 27th, 2010 at 13:08 | #37

    @James
    Sent you the code through email. :)

  1. No trackbacks yet.