none
Gimme C/C++ Programs Definitions.. RRS feed

  • Question

  • Hey guys, i am in 2nd Semester MCA, i want a list of C/C+ programs that i can do for practice.
    Saturday, March 31, 2007 2:36 PM

Answers

  • If possible solutions to it would also be appreciated.
    Saturday, March 31, 2007 2:36 PM
  • /*
    [1]    To check whether the given number is odd or even.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no%2==0)
            printf("%d is an even number",no);
        else
            printf("%d is an odd number",no);
        getch();
    }
    Saturday, March 31, 2007 3:59 PM
  • /*
    [2]    To check whether the given number is positive or negative or zero.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no>0)
            printf("%d is a Positive number",no);
        else if(no<0)
            printf("%d is a Negative number",no);
        else
            printf("%d is ZERO",no);
        getch();
    }
    Saturday, March 31, 2007 3:59 PM
  • /*
    [3] To check whether the given number is palindrome or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tp,ans=0;
        clrscr();
        printf("Enter a number to check whether it is a palindrome or not : ");
        scanf("%d",&no);
        tp=no;
        while(no!=0)
        {
            ans = (no%10) + (ans*10);
            no/=10;
        }
        if (tp==ans)
            printf("%d is a palindrome\n",tp);
        else
            printf("%d is not a palindrome\n",tp);
        getch();
    }
    Saturday, March 31, 2007 4:00 PM
  • /*
    [4]  To check whether the given number is of one digited
        or two digited or three digited or more than three digited.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        tp=no;
        no = (no<0)?no*-1:no;
        if(no>=0 && no<=9)
            printf("%d is a ONE Digited Number");
        else if(no>=10 && no<=99)
            printf("%d is a TWO Digited Number");
        else if(no>=100 && no<=999)
            printf("%d is a THREE Digited Number");
        else if(no>=999)
            printf("%d is More than THREE Digited Number");
        getch();
    }
    Saturday, March 31, 2007 4:00 PM
  • /*
    [5]    To check whether the given number is a perfect square or not.
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int no;
        clrscr();
        printf("Enter a number to check whether its a perfect square or not : ");
        scanf("%d",&no);
        if ( (float)sqrt(no) == (int)sqrt(no) )
            printf("%d is a perfect square",no);
        else
            printf("%d is not a perfct square",no);
        getch();
    }

    Saturday, March 31, 2007 4:00 PM
  • /*
    Devil    To check  whether the given character is an uppercase letter
        or lowercase letter or a digit or a special character.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        char ch;
        clrscr();
        printf("Enter a character : ");
        ch = getche();
        if(ch>='a' && ch<='z')
            printf("\n\'%c\' is a Lower Case Character",ch);
        else if(ch>='A' && ch<='Z')
            printf("\n\'%c\' is a Upper Case Character",ch);
        else if(ch>='0' && ch<='9')
            printf("\n\'%c\' is a digit",ch);
        else
            printf("\n\'%c\' is a Special Character",ch);
        getch();


    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    [7]    To determine whether the given year is a leap year or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int year;
        clrscr();
        printf("Enter an year to check whether its leap year or not : ");
        scanf("%d",&year);
        if ((year%4==0 && year%100!=0) || year%400==0)
            printf("Year %d is a leap year",year);
        else
            printf("Year %d is not a leap year",year);
        getch();
    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    [10]    Write a program that reads three positive numbers a, b, c and
        determines whether they can form the three sides of a triangle.
        If yes, determine whether the triangle will be an obtuse-angle,
        or a right-angle or an acute-angle triangle. If the triangle is
        an acute angle triangle, determine further whether the triangle
        is equilateral, isosceles or scalene.
    */

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int a,b,c,AB,BC,AC,cnt=0;
        clrscr();
        printf("Enter side 'a' of ABC : ");
        scanf("%d",&a);
        printf("Enter side 'b' of ABC : ");
        scanf("%d",&b);
        printf("Enter side 'c' of ABC : ");
        scanf("%d",&c);
        AB = c;
        BC = a;
        AC = b;
        printf("AB(c) = %d\n",c);
        printf("BC(a) = %d\n",a);
        printf("AC(b) = %d\n",b);
        if((AC*AC) == (BC*BC)+(AB*AB) )
            printf("ABC(%d,%d,%d) is a Right Angled triangle\n",a,b,c);
        if( (AC*AC) > (AB*AB)+(BC*BC) )
            printf("ABC(%d,%d,%d) is a Obtuse Angled triangle\n",a,b,c);
        if( (AC*AC) < (AB*AB)+(BC*BC) ) //Triangle is acute so determine whether
        {                    // it is Equilateral, isoceles or scalene.
            printf("ABC(%d,%d,%d) is a Acute Angled Triangle\n",a,b,c);
            if(AB==BC) cnt++;
            if(AB==AC) cnt++;
            if(BC==AC) cnt++;
            if(cnt==3) printf("ABC(%d,%d,%d) is an Equilateral Triangle\n",a,b,c); //all sides are same
            else if(cnt==1) printf("ABC(%d,%d,%d) is an Isoceles Triangle\n",a,b,c); //1pair is equal (which means that 2 sides are same)
            else printf("ABC(%d,%d,%d) is a scalene Triangle\n",a,b,c);
        }
        getch();
    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    Music    To check whether the given number is divisible by 5 or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no%5==0)
            printf("%d is perfectly divisible by 5",no);
        else
            printf("%d is not perfectly divisible by 5",no);
        getch();
    }
    Saturday, March 31, 2007 4:02 PM
  • /*
    [9]    To find maximum number out of given three numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int a,b,c;
        clrscr();
        printf("Enter no1 : ");
        scanf("%d",&a);
        printf("Enter no2 : ");
        scanf("%d",&b);
        printf("Enter no3 : ");
        scanf("%d",&c);

        printf("a =  %d\nb =  %d\nc =  %d\n\n",a,b,c);
        if(a>b)
        {
            if(a>c)
                printf("a (%d) is the largest number",a);
            else
                printf("c (%d) is the largest number",c);
        }
        else
        {
            if(b<c)
                printf("c (%d) is the largest nubmer",c);
            else
                printf("b (%d) is the largest number",b);
        }

        getch();
    }
    Saturday, March 31, 2007 4:02 PM
  • /* Reverse string without using duplicate storage */

     

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char *A;
    int l,i;
    clrscr();
    printf("Enter the string:-> ");
    gets(A);
    l=strlen(A);
    printf("\nThe given string is:-> ");
    puts(A);
    printf("\nThe Reverse of the string is:-> ");
    for(i=l;i>=0;i--)
    {
    printf("%c",AIdea);
    }
    getch();
    }

    Saturday, March 31, 2007 5:57 PM
  • Well brother even i m in mca 2nd sem.. and it will be nice to share with each other...
    Code Snippet


    // This program will use the static memory n static function in a bank prog.

    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>

    class bank
    {
        int act_no;
        float balance;
        static float int_rate;

    public :
        void getdata(FILE *bk_file);
        void adddata();
        void printdata();
        void updatedata();
        void savedata(FILE *bk_file);
        static void getrate();
    };

    float bank::int_rate;

    void bank::getdata(FILE *bk_file)
    {
        fscanf(bk_file,"%d",&act_no);
        fscanf(bk_file,"%f",&balance);
    }

    void bank::adddata()
    {
        cout<<"Acount No : ";
        cin>>act_no;
        cout<<"Balance   :";
        cin>>balance;
    }

    void bank::printdata()
    {
        cout<<"Account No : "<<act_no<<"\n";
        cout<<"Balance    : "<<balance<<"\n";
    }

    void bank::savedata(FILE *bk_file)
    {
        fprintf(bk_file,"%5d %8.2f", act_no, balance);
    }

    void bank::updatedata()
    {
        balance *= 1 + int_rate/100;
    }

    void bank::getrate()
    {
        cout<<"Please give the rate of interest : ";
        cin>>int_rate;
    }

    void main()
    {
        bank ac[20];
        FILE *bk_file;
        short i,k;
        char choice;

        clrscr();
        bk_file = fopen("bank.dat", "r+");
        if(bk_file == NULL)
        {
            bk_file = fopen("bank.dat", "w+");
        }
        i=0;
        ac[i].getdata(bk_file);
        while(!feof(bk_file))
        {
            i++;
            ac[i].getdata(bk_file);
        }
        cout<<"Do you want to add Data : ";
        cin>>choice;
        while(choice != 'n')
        {
            i++;
            ac[i].adddata();

            cout<<"Do you want to add Data : ";
            cin>>choice;
        }

        cout<<"Do you want to add interest to the balance : ";
        cin>>choice;
        if(choice=='y')
        {
            bank::getrate();
            for(k=0;k<=i;k++)
                ac[k].updatedata();
        }
        cout<<"The Balance of the Account\n";
        for(k=0;k<=i;k++)
            ac[k].printdata();
        rewind(bk_file);
        for(k=0;k<=i;k++)
        {
            ac[k].savedata(bk_file);
        }
        fclose(bk_file);
        getch();
    }


    Sunday, April 1, 2007 5:59 AM
  • Code Snippet

    // This program will use the Construtor to initialize the values of a object...

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>

    class student
    {
        int rollno;
        char *name;
        float marks;
        static int tmp;
        public :
        student()
        {
            rollno = tmp++;
            marks = 0;
            name = NULL;
        }
        student(int rn, char *pt, float mks)
        {
            rollno = rn;
            marks = mks;
            name = new char[strlen(pt) + 1];
            strcpy(name, pt);
        }
        student(student &stud)
        {
            rollno = stud.rollno;
            marks = stud.marks;
            name = new char[strlen(stud.name) + 1];
            strcpy(name, stud.name);
        }
        void display()
        {
            cout<<"Roll No. : "<<rollno<<"\nName : "<<name<<"\nMarks : "<<marks<<"\n";
        }
        void update(int rn, char *pt, float mks)
        {
            rollno = rn;
            marks = mks;
            name = (char *)realloc(name,strlen(pt) + 1);
            strcpy(name, pt);
        }

    };

    int student::tmp = 1;
    void main()
    {
        student s1[3];
        student s2(8,"xyz",80);
        student s3(s2);
        clrscr();
        cout<<"Default Initializing by automatic incrementing Roll No. values\n";
        for(int i=0;i<3;i++)
            s1[i].display();
        cout<<"\nDynamic & Parameter Initializing the object :\n";
        s2.display();
        cout<<"\nCopy Contrutor :\n";
        s3.display();
        cout<<"\nUpdating the object :\n";
        s2.update(10,"ABCDEF",100);
        s2.display();
        s3.display();
        getch();
    }


    Sunday, April 1, 2007 6:01 AM
  • Here goes another one
    Code Snippet

    // WAP to Implement Euclide's Theorem to find the greatest common Divisor..

    #include<stdio.h>
    #include<conio.h>
    int gcd(int x, int y);

    void main()
    {
        int x, y;
        clrscr();
        printf("Please give the 2 numbers to find GCD between : \n");
        scanf("%d %d",&x,&y);
        if(x>y)
            printf("The GCD is = %d",gcd(x,y));
        else
            printf("The Gcd is = %d",gcd(y,x));
        getch();
    }

    int gcd(int x, int y)
    {
        if(y==0)
            return (x);
        else
            return(gcd(y, x%y));
    }


    Sunday, April 1, 2007 6:07 AM
  • Brother this is only a trailer, i can show you the whole movie.. just let me know if you liked them.. also, i hv lots of code, so it will be better if you tell me some specific ones that you want...
    Sunday, April 1, 2007 6:10 AM
  • Hey Var and Harshil, you guys have posted nice code.thanks for it.

    And if possible just keep posting any number of codes that you guys like, it will be too much helpful for me.

     

    and Varun, i dont have any specific ones that i can request for, but just post all you want and ill mark as answer ...

    Sunday, April 1, 2007 9:12 AM

  • Code Snippet

    /*...................Write a program to do the following...................*/
    /*
    ABC publishing company markets both book and audio cassette versions of its
    work. Create a class called publication that stores the title (a string)
    and price (type float) of a publication. From this class derive two
    classes : book, which adds a page count (type int); and tape, which adds
    playing time in minutes (type float). Write a main program that reads both
    book and tape information in one array. When the user has finished entering
    data for all books and tapes, displays the resulting data for all the books
    and tapes entered. Also count no. of books and cassette entries in the array
    using runtime identification feature of C++.
    */
    /*.............................Sanket T. Shah..............................*/
    /*.........................MCA - II; Roll No. - 22.........................*/

    #include<typeinfo.h>
    #include<iostream.h>
    #include<process.h>
    #include<string.h>
    #include<conio.h>

    class Publication
    {
        protected:

        char title[10];
        float price;

        public:

        void virtual readData(void);
        void virtual showData(void);
    };

    void Publication::readData(void)
    {
        cout<<"\nEnter Title : ";
        cin>>title;
        cout<<"\nEnter Price : ";
        cin>>price;
    }

    void Publication::showData(void)
    {
        cout<<"\nTitle : "<<title;
        cout<<"\nPrice : "<<price;
    }

    class Book:public Publication
    {
        int pages;

        public:

        void readData(void);
        void showData(void);
    };

    void Book::readData(void)
    {
        Publication::readData();

        cout<<"\nEnter Book Pages : ";
        cin>>pages;
    }

    void Book::showData(void)
    {
        Publication::showData();

        cout<<"\nBook Pages : "<<pages;
    }

    class Tape:public Publication
    {
        float time;

        public:

        void readData(void);
        void showData(void);
    };

    void Tape::readData(void)
    {
        Publication::readData();

        cout<<"\nEnter Playing Time in minutes : ";
        cin>>time;
    }

    void Tape::showData(void)
    {
        Publication::showData();

        cout<<"\nPlaying Time in minutes : "<<time;
    }

    int main(void)
    {
        int maxPublications, choice, index = -1, booksCount = 0, i;

    //    clrscr();

        cout<<"\nEnter maximum no. of publications : ";
        cin>>maxPublications;

        Publication **P;
        P = new Publication *[maxPublications];

        while(1)
        {
    //        clrscr();

            cout<<"\nProgram to show polymorphism for a Publication House"
                <<"\n----------------------------------------------------"
                <<"\n1. Enter data for Books"
                <<"\n2. Enter data for Tapes"
                <<"\n3. Show all data"
                <<"\n4. Show count of Books and Tapes"
                <<"\n0. Exit"
                <<"\n"
                <<"\nEnter your choice : ";

            cin>>choice;

            switch(choice)
            {
                case 1 :index++;
                    if(index >= maxPublications)
                    {
                        cout<<"\nInvalid index.";
                        index--;
                        break;
                    }

                    P[index] = new Book;
                    P[index]->readData();

                    break;

                case 2 :index++;
                    if(index >= maxPublications)
                    {
                        cout<<"\nInvalid index.";
                        index--;
                        break;
                    }

                    P[index] = new Tape;
                    P[index]->readData();

                    break;

                case 3 :for(i = 0 ; i <= index ; i++)
                    {
                        P[i]->showData();
                        cout<<"\n";
                    }
                    break;

                case 4 :for(i = 0 ; i <= index ; i++)
                    {
                        if(strcmp(typeid(*P[i]).name(), "class Book") == 0)
                        {
                            booksCount++;
                        }
                        cout<<"\n";
                    }
                    cout<<"\nTotal Books : "<<booksCount;
                    cout<<"\nTotal Tapes : "<<maxPublications - booksCount;
                    break;

                case 0 ://clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
            }
            getch();
        }
        return 0;
    }

    /*..............................End Of Program.............................*/

    Sunday, April 1, 2007 4:45 PM
  • you can also mark my answer ?.

    i will be sending u the unique programs later.

    Monday, April 2, 2007 6:48 PM
  • Yes friends, just post the questions and solutions, and ill mark as anwer for sure.....Guys keep posting,  This thread is being very useful for me to learn c/c++ .
    Tuesday, April 3, 2007 12:54 PM

  • // This program will create a dynamic 2d array in a  class...


    #include<iostream.h>
    #include<conio.h>

    class student
    {
        int rollno;
        char name[25];
        float **marks;
        int sub_size, exam_size;

        public :
        student(int,int);
        ~student();
        void display();
        void getdata();

    };

    student::student(int subj = 5, int exam = 3)
    {
        sub_size = subj; exam_size = exam;
        marks = new float*[subj];
        int i;
        for(i=0;i<subj;i++)
            marks[i] = new float[exam];
    }
    student::~student()
    {
        for(int i=0; i<sub_size;i++)
                delete marks[i];
        delete marks;
    }
    void student::getdata()
    {
        cout<<"Please give the marks :\n";
        for(int i=0;i<sub_size;i++)
        {
            cout<<"Subject No "<<i+ 1<<"\n";
            for(int j=0;j<exam_size;j++)
            {
                cout<<"Exam No "<<j + 1<<" : ";
                cin>>marks[i][j];
            }
        }
    }
    void student::display()
    {
        for(int i=0;i<sub_size; i++)
        {
            cout<<"Subject No "<<i + 1<<"\n";
            for(int j=0;j<exam_size;j++)
            {
                cout<<"Exam No "<<j + 1<<" : "<<marks[i][j]<<"   ";
            }
            cout<<"\n";
        }
    }

    void main()
    {
        student s1, s2(1,1), s3(1);
        clrscr();
        s1.getdata();
        s2.getdata();
        s3.getdata();
        clrscr();
        cout<<"Object With Default Parameter :\n";
        s1.display();
        cout<<"\nobject with both parameters being passed :\n";
        s2.display();
        cout<<"\nobject with one default parameter and one default :\n";
        s3.display();
        getch();
    }

    Tuesday, April 3, 2007 4:06 PM
  • // This program uses a friend function to give out the max scorer from a dca & mca student.



    #include<iostream.h>
    #include<conio.h>

    class dcastud;
    class mcastud
    {
        char name[15];
        int marks;
        float percentage;

        public :
        void getdata();

        friend void scorer(mcastud &m, dcastud &d);
    };

    void mcastud::getdata()
    {
        cout<<"Please give the name of the mca student : ";
        cin>>name;
        cout<<"Marks of the student : ";
        cin>>marks;
        cout<<"Percentage of the student : ";
        cin>>percentage;
    }

    class dcastud
    {
        char name[15];
        float percentage;

        public :
        void getdata();

        friend void scorer(mcastud &m, dcastud &d);
    };

    void dcastud::getdata()
    {
        cout<<"Please give the name of the dca student : ";
        cin>>name;
        cout<<"Percentage of the student : ";
        cin>>percentage;
    }

    void scorer(mcastud &m, dcastud &d)
    {
        if(m.percentage > d.percentage)
            cout<<"Mca Student scores more than Dca";
        else if (d.percentage > m.percentage)
            cout<<"Dca Student Scores more";
        else
            cout<<"Both scores equally";
    }


    void main()
    {
        mcastud m;
        dcastud d;
        clrscr();
        m.getdata();
        d.getdata();
        scorer(m,d);
        getch();

    }


    Tuesday, April 3, 2007 4:08 PM
  • //This program will display the use of inheriting base class in derieved class..


    #include<iostream.h>
    #include<conio.h>

    class person
    {
        protected :
        char name[15];
        char phone[12];
        char dob[10];

        public :
        void getdata();
        void putdata();
    };
    class student: public person
    {
        int roll_no;
        int marks;

        public:
        void get_stud_data();
        void put_stud_data();
    };
    void person::getdata()
    {
        cout<<"Please give the name : ";
        cin>>name;
        cout<<"Please give the phone no. : ";
        cin>>phone;
        cout<<"Please give the dob : ";
        cin>>dob;
    }
    void person::putdata()
    {
        cout<<"Name : "<<name<<"\n";
        cout<<"Phone : "<<phone<<"\n";
        cout<<"D O B : "<<dob<<"\n";
    }
    void student::get_stud_data()
    {
        getdata();
        cout<<"Please give the roll no : ";
        cin>>roll_no;
        cout<<"Please give the marks : ";
        cin>>marks;
    }
    void student::put_stud_data()
    {
        putdata();
        cout<<"Roll no : "<<roll_no<<"\n";
        cout<<"Marks : "<<marks<<"\n";
    }

    void main()
    {
        student s;
        clrscr();

        s.get_stud_data();
        s.put_stud_data();
        getch();
    }


    Tuesday, April 3, 2007 4:09 PM
  • // This program will demonstrate Casting Operator...


    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class student;
    class teacher
    {
        char *tname;
        float salary;
        public:
        teacher()
        {
            tname = NULL;
            salary = 0;
        }
        teacher(char *s, float f)
        {
            tname = new char[strlen(s) + 1];
            strcpy(tname,s);
            salary = f;
        }
        ~teacher()
        {
            delete tname;
        }
    /*    teacher(student s)
        {
            strcpy(tname, s.getname());
        }     */
        void getdata();
        void putdata();
    };
    class student
    {
        int rollno;
        char *name;
        float marks;

        public:
        student()
        {
            rollno = 0;
            marks = 0;
            name = NULL;
        }
        ~student()
        {
            delete name;
        }
        operator teacher()
        {

            return (teacher(name,0 ));
        }
        void getdata();
        void putdata();
    };


    void student::getdata()
    {
        cout<<"Please give the student Details : \n";
        char nm[20];
        cout<<"Please give the Rollno : ";
        cin>>rollno;
        cout<<"Please give the name : ";
        cin>>nm;
        cout<<"Please give the marks : ";
        cin>>marks;
        cout<<nm;
        name = new char[sizeof(strlen(nm) + 1)];
        strcpy(name,nm);
        cout<<"Debug ::: \n"<<name<<"::: Debug\n";
        name[strlen(nm)]=NULL;
    }
    void student::putdata()
    {
        cout<<"Roll No : "<<rollno<<"   Name :"<<name<<"\n   Marks : "<<marks<<"\n";
    }
    void teacher::getdata()
    {
        cout<<"Please give the Teacher Details : \n";
        char nm[20];
        cout<<"Please give the name : ";
        cin>>nm;
        cout<<"Please give the salary :";
        cin>>salary;
        tname = new char[sizeof(strlen(nm) + 1)];
        strcpy(tname, nm);
        tname[strlen(tname)]=NULL;
    }

    void teacher::putdata()
    {
        cout<<"Name : "<<tname<<"   Salary :"<<salary<<"\n";
    }

    void main()
    {
        student s;
        teacher t;

        clrscr();
        s.getdata();
        t.getdata();
        s.putdata();
        t.putdata();
    /*    t = s;
        t.putdata();  */
        getch();
    }


    Tuesday, April 3, 2007 4:10 PM
  • // WAP to find the Exponent of a number using recursive function.


    #include<stdio.h>
    #include<conio.h>
    float exponent(float number, int power);

    void main()
    {
        float number;
        int power;

        clrscr();
        printf("Please give the number : ");
        scanf("%f",&number);
        printf("Please give the power of the number : ");
        scanf("%d", &power);
        printf("Ans = %.3f",exponent(number, power));
        getch();
    }

    float exponent(float number, int power)
    {
        if(power==1)
            return(number);
        else if(power%2==0)
            return(exponent(number*number,power/2));
        else
            return(exponent(number*number,power/2)*number);
    }


    Tuesday, April 3, 2007 4:14 PM
  • Very good work Varun, it seems like you are the only one who knows c/c++. 

     

    Keep posting more..... thx a lot in advance.

    Tuesday, April 3, 2007 6:27 PM
  • #include<iostream.h>
     class triangle
     {
      int i,j,value;
      public:
       void getdata(void);
       void tri(void);
    };

    void triangle::getdata(void)
    {
     cout<<"\n enter the value:";
     cin>>value;

     void triangle::tri(void)
     {
      int k=1;
      for(i=1;i<=value;i++)
     {
       for(j=1;j<=i;j++)

       cout<<k++;
       cout<<"\n";
     }
     }
     int main()
     {
      triangle t;
      t.getdata();
      t.tri();

      return 0;
    }

    Wednesday, April 4, 2007 3:56 PM
  • great work guys, keep it up. Keep on posting Wink
    Wednesday, April 4, 2007 4:46 PM
  • If statements:
    The structure of an if statement is as follows:
     
    if ( TRUE )
    Execute the next statement
    To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of a function. Anything inside braces is called a compound statement, or a block.

    For example:
     
    if ( TRUE ) {
    Execute all statements inside the braces
    }
    There is also the else statement. The code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.

    It can look like this:
     
    if ( TRUE ) {
    // Execute these statements if TRUE
    }
    else {
    // Execute these statements if FALSE
    }
    One use for else is if there are two conditional statements that may both evaluate to true, yet you wish only one of the two to have the code block following it to be executed. You can use an else if after the if statement; that way, if the first statement is true, the else if will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements.

    Let's look at a simple program for you to try out on your own.
     
    #include <iostream>

    using namespace std;

    int main() // Most important part of the program!
    {
    int age; // Need a variable...

    cout<<"Please input your age: "; // Asks for age
    cin>> age; // The input is put in age
    cin.ignore(); // Throw away enter
    if ( age < 100 ) { // If the age is less than 100
    cout<<"You are pretty young!\n"; // Just to show you it works...
    }
    else if ( age == 100 ) { // I use else just to show an example
    cout<<"You are old\n"; // Just to show you it works...
    }
    else {
    cout<<"You are really old\n"; // Executed if no other statement is
    }
    cin.get();
    }

    Tuesday, April 10, 2007 7:57 PM
  • A Program Using Functions :
    #include <iostream>

    using namespace std;

    int mult ( int x, int y );

    int main()
    {
    int x;
    int y;

    cout<<"Please input two numbers to be multiplied: ";
    cin>> x >> y;
    cin.ignore();
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    cin.get();
    }

    int mult ( int x, int y )
    {
    return x * y;
    }

    Tuesday, April 10, 2007 8:03 PM
  • A Program Using Switch Case:
    #include <stdio.h>

    void playgame();
    void loadgame();
    void playmultiplayer();

    int main()
    {
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
    case 1: /* Note the colon, not a semicolon */
    playgame();
    break;
    case 2:
    loadgame();
    break;
    case 3:
    playmultiplayer();
    break;
    case 4:
    printf( "Thanks for playing!\n" );
    break;
    default:
    printf( "Bad input, quitting!\n" );
    break;
    }
    getchar();

    }

    Tuesday, April 10, 2007 8:04 PM

All replies

  • If possible solutions to it would also be appreciated.
    Saturday, March 31, 2007 2:36 PM
  • /*
    [1]    To check whether the given number is odd or even.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no%2==0)
            printf("%d is an even number",no);
        else
            printf("%d is an odd number",no);
        getch();
    }
    Saturday, March 31, 2007 3:59 PM
  • /*
    [2]    To check whether the given number is positive or negative or zero.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no>0)
            printf("%d is a Positive number",no);
        else if(no<0)
            printf("%d is a Negative number",no);
        else
            printf("%d is ZERO",no);
        getch();
    }
    Saturday, March 31, 2007 3:59 PM
  • /*
    [3] To check whether the given number is palindrome or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tp,ans=0;
        clrscr();
        printf("Enter a number to check whether it is a palindrome or not : ");
        scanf("%d",&no);
        tp=no;
        while(no!=0)
        {
            ans = (no%10) + (ans*10);
            no/=10;
        }
        if (tp==ans)
            printf("%d is a palindrome\n",tp);
        else
            printf("%d is not a palindrome\n",tp);
        getch();
    }
    Saturday, March 31, 2007 4:00 PM
  • /*
    [4]  To check whether the given number is of one digited
        or two digited or three digited or more than three digited.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        tp=no;
        no = (no<0)?no*-1:no;
        if(no>=0 && no<=9)
            printf("%d is a ONE Digited Number");
        else if(no>=10 && no<=99)
            printf("%d is a TWO Digited Number");
        else if(no>=100 && no<=999)
            printf("%d is a THREE Digited Number");
        else if(no>=999)
            printf("%d is More than THREE Digited Number");
        getch();
    }
    Saturday, March 31, 2007 4:00 PM
  • /*
    [5]    To check whether the given number is a perfect square or not.
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int no;
        clrscr();
        printf("Enter a number to check whether its a perfect square or not : ");
        scanf("%d",&no);
        if ( (float)sqrt(no) == (int)sqrt(no) )
            printf("%d is a perfect square",no);
        else
            printf("%d is not a perfct square",no);
        getch();
    }

    Saturday, March 31, 2007 4:00 PM
  • /*
    Devil    To check  whether the given character is an uppercase letter
        or lowercase letter or a digit or a special character.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        char ch;
        clrscr();
        printf("Enter a character : ");
        ch = getche();
        if(ch>='a' && ch<='z')
            printf("\n\'%c\' is a Lower Case Character",ch);
        else if(ch>='A' && ch<='Z')
            printf("\n\'%c\' is a Upper Case Character",ch);
        else if(ch>='0' && ch<='9')
            printf("\n\'%c\' is a digit",ch);
        else
            printf("\n\'%c\' is a Special Character",ch);
        getch();


    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    [7]    To determine whether the given year is a leap year or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int year;
        clrscr();
        printf("Enter an year to check whether its leap year or not : ");
        scanf("%d",&year);
        if ((year%4==0 && year%100!=0) || year%400==0)
            printf("Year %d is a leap year",year);
        else
            printf("Year %d is not a leap year",year);
        getch();
    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    [10]    Write a program that reads three positive numbers a, b, c and
        determines whether they can form the three sides of a triangle.
        If yes, determine whether the triangle will be an obtuse-angle,
        or a right-angle or an acute-angle triangle. If the triangle is
        an acute angle triangle, determine further whether the triangle
        is equilateral, isosceles or scalene.
    */

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int a,b,c,AB,BC,AC,cnt=0;
        clrscr();
        printf("Enter side 'a' of ABC : ");
        scanf("%d",&a);
        printf("Enter side 'b' of ABC : ");
        scanf("%d",&b);
        printf("Enter side 'c' of ABC : ");
        scanf("%d",&c);
        AB = c;
        BC = a;
        AC = b;
        printf("AB(c) = %d\n",c);
        printf("BC(a) = %d\n",a);
        printf("AC(b) = %d\n",b);
        if((AC*AC) == (BC*BC)+(AB*AB) )
            printf("ABC(%d,%d,%d) is a Right Angled triangle\n",a,b,c);
        if( (AC*AC) > (AB*AB)+(BC*BC) )
            printf("ABC(%d,%d,%d) is a Obtuse Angled triangle\n",a,b,c);
        if( (AC*AC) < (AB*AB)+(BC*BC) ) //Triangle is acute so determine whether
        {                    // it is Equilateral, isoceles or scalene.
            printf("ABC(%d,%d,%d) is a Acute Angled Triangle\n",a,b,c);
            if(AB==BC) cnt++;
            if(AB==AC) cnt++;
            if(BC==AC) cnt++;
            if(cnt==3) printf("ABC(%d,%d,%d) is an Equilateral Triangle\n",a,b,c); //all sides are same
            else if(cnt==1) printf("ABC(%d,%d,%d) is an Isoceles Triangle\n",a,b,c); //1pair is equal (which means that 2 sides are same)
            else printf("ABC(%d,%d,%d) is a scalene Triangle\n",a,b,c);
        }
        getch();
    }
    Saturday, March 31, 2007 4:01 PM
  • /*
    Music    To check whether the given number is divisible by 5 or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no%5==0)
            printf("%d is perfectly divisible by 5",no);
        else
            printf("%d is not perfectly divisible by 5",no);
        getch();
    }
    Saturday, March 31, 2007 4:02 PM
  • /*
    [9]    To find maximum number out of given three numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int a,b,c;
        clrscr();
        printf("Enter no1 : ");
        scanf("%d",&a);
        printf("Enter no2 : ");
        scanf("%d",&b);
        printf("Enter no3 : ");
        scanf("%d",&c);

        printf("a =  %d\nb =  %d\nc =  %d\n\n",a,b,c);
        if(a>b)
        {
            if(a>c)
                printf("a (%d) is the largest number",a);
            else
                printf("c (%d) is the largest number",c);
        }
        else
        {
            if(b<c)
                printf("c (%d) is the largest nubmer",c);
            else
                printf("b (%d) is the largest number",b);
        }

        getch();
    }
    Saturday, March 31, 2007 4:02 PM
  • /* Reverse string without using duplicate storage */

     

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char *A;
    int l,i;
    clrscr();
    printf("Enter the string:-> ");
    gets(A);
    l=strlen(A);
    printf("\nThe given string is:-> ");
    puts(A);
    printf("\nThe Reverse of the string is:-> ");
    for(i=l;i>=0;i--)
    {
    printf("%c",AIdea);
    }
    getch();
    }

    Saturday, March 31, 2007 5:57 PM
  • Well brother even i m in mca 2nd sem.. and it will be nice to share with each other...
    Code Snippet


    // This program will use the static memory n static function in a bank prog.

    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>

    class bank
    {
        int act_no;
        float balance;
        static float int_rate;

    public :
        void getdata(FILE *bk_file);
        void adddata();
        void printdata();
        void updatedata();
        void savedata(FILE *bk_file);
        static void getrate();
    };

    float bank::int_rate;

    void bank::getdata(FILE *bk_file)
    {
        fscanf(bk_file,"%d",&act_no);
        fscanf(bk_file,"%f",&balance);
    }

    void bank::adddata()
    {
        cout<<"Acount No : ";
        cin>>act_no;
        cout<<"Balance   :";
        cin>>balance;
    }

    void bank::printdata()
    {
        cout<<"Account No : "<<act_no<<"\n";
        cout<<"Balance    : "<<balance<<"\n";
    }

    void bank::savedata(FILE *bk_file)
    {
        fprintf(bk_file,"%5d %8.2f", act_no, balance);
    }

    void bank::updatedata()
    {
        balance *= 1 + int_rate/100;
    }

    void bank::getrate()
    {
        cout<<"Please give the rate of interest : ";
        cin>>int_rate;
    }

    void main()
    {
        bank ac[20];
        FILE *bk_file;
        short i,k;
        char choice;

        clrscr();
        bk_file = fopen("bank.dat", "r+");
        if(bk_file == NULL)
        {
            bk_file = fopen("bank.dat", "w+");
        }
        i=0;
        ac[i].getdata(bk_file);
        while(!feof(bk_file))
        {
            i++;
            ac[i].getdata(bk_file);
        }
        cout<<"Do you want to add Data : ";
        cin>>choice;
        while(choice != 'n')
        {
            i++;
            ac[i].adddata();

            cout<<"Do you want to add Data : ";
            cin>>choice;
        }

        cout<<"Do you want to add interest to the balance : ";
        cin>>choice;
        if(choice=='y')
        {
            bank::getrate();
            for(k=0;k<=i;k++)
                ac[k].updatedata();
        }
        cout<<"The Balance of the Account\n";
        for(k=0;k<=i;k++)
            ac[k].printdata();
        rewind(bk_file);
        for(k=0;k<=i;k++)
        {
            ac[k].savedata(bk_file);
        }
        fclose(bk_file);
        getch();
    }


    Sunday, April 1, 2007 5:59 AM
  • Code Snippet

    // This program will use the Construtor to initialize the values of a object...

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>

    class student
    {
        int rollno;
        char *name;
        float marks;
        static int tmp;
        public :
        student()
        {
            rollno = tmp++;
            marks = 0;
            name = NULL;
        }
        student(int rn, char *pt, float mks)
        {
            rollno = rn;
            marks = mks;
            name = new char[strlen(pt) + 1];
            strcpy(name, pt);
        }
        student(student &stud)
        {
            rollno = stud.rollno;
            marks = stud.marks;
            name = new char[strlen(stud.name) + 1];
            strcpy(name, stud.name);
        }
        void display()
        {
            cout<<"Roll No. : "<<rollno<<"\nName : "<<name<<"\nMarks : "<<marks<<"\n";
        }
        void update(int rn, char *pt, float mks)
        {
            rollno = rn;
            marks = mks;
            name = (char *)realloc(name,strlen(pt) + 1);
            strcpy(name, pt);
        }

    };

    int student::tmp = 1;
    void main()
    {
        student s1[3];
        student s2(8,"xyz",80);
        student s3(s2);
        clrscr();
        cout<<"Default Initializing by automatic incrementing Roll No. values\n";
        for(int i=0;i<3;i++)
            s1[i].display();
        cout<<"\nDynamic & Parameter Initializing the object :\n";
        s2.display();
        cout<<"\nCopy Contrutor :\n";
        s3.display();
        cout<<"\nUpdating the object :\n";
        s2.update(10,"ABCDEF",100);
        s2.display();
        s3.display();
        getch();
    }


    Sunday, April 1, 2007 6:01 AM
  • Here goes another one
    Code Snippet

    // WAP to Implement Euclide's Theorem to find the greatest common Divisor..

    #include<stdio.h>
    #include<conio.h>
    int gcd(int x, int y);

    void main()
    {
        int x, y;
        clrscr();
        printf("Please give the 2 numbers to find GCD between : \n");
        scanf("%d %d",&x,&y);
        if(x>y)
            printf("The GCD is = %d",gcd(x,y));
        else
            printf("The Gcd is = %d",gcd(y,x));
        getch();
    }

    int gcd(int x, int y)
    {
        if(y==0)
            return (x);
        else
            return(gcd(y, x%y));
    }


    Sunday, April 1, 2007 6:07 AM
  • Brother this is only a trailer, i can show you the whole movie.. just let me know if you liked them.. also, i hv lots of code, so it will be better if you tell me some specific ones that you want...
    Sunday, April 1, 2007 6:10 AM
  • Hey Var and Harshil, you guys have posted nice code.thanks for it.

    And if possible just keep posting any number of codes that you guys like, it will be too much helpful for me.

     

    and Varun, i dont have any specific ones that i can request for, but just post all you want and ill mark as answer ...

    Sunday, April 1, 2007 9:12 AM

  • Code Snippet

    /*...................Write a program to do the following...................*/
    /*
    ABC publishing company markets both book and audio cassette versions of its
    work. Create a class called publication that stores the title (a string)
    and price (type float) of a publication. From this class derive two
    classes : book, which adds a page count (type int); and tape, which adds
    playing time in minutes (type float). Write a main program that reads both
    book and tape information in one array. When the user has finished entering
    data for all books and tapes, displays the resulting data for all the books
    and tapes entered. Also count no. of books and cassette entries in the array
    using runtime identification feature of C++.
    */
    /*.............................Sanket T. Shah..............................*/
    /*.........................MCA - II; Roll No. - 22.........................*/

    #include<typeinfo.h>
    #include<iostream.h>
    #include<process.h>
    #include<string.h>
    #include<conio.h>

    class Publication
    {
        protected:

        char title[10];
        float price;

        public:

        void virtual readData(void);
        void virtual showData(void);
    };

    void Publication::readData(void)
    {
        cout<<"\nEnter Title : ";
        cin>>title;
        cout<<"\nEnter Price : ";
        cin>>price;
    }

    void Publication::showData(void)
    {
        cout<<"\nTitle : "<<title;
        cout<<"\nPrice : "<<price;
    }

    class Book:public Publication
    {
        int pages;

        public:

        void readData(void);
        void showData(void);
    };

    void Book::readData(void)
    {
        Publication::readData();

        cout<<"\nEnter Book Pages : ";
        cin>>pages;
    }

    void Book::showData(void)
    {
        Publication::showData();

        cout<<"\nBook Pages : "<<pages;
    }

    class Tape:public Publication
    {
        float time;

        public:

        void readData(void);
        void showData(void);
    };

    void Tape::readData(void)
    {
        Publication::readData();

        cout<<"\nEnter Playing Time in minutes : ";
        cin>>time;
    }

    void Tape::showData(void)
    {
        Publication::showData();

        cout<<"\nPlaying Time in minutes : "<<time;
    }

    int main(void)
    {
        int maxPublications, choice, index = -1, booksCount = 0, i;

    //    clrscr();

        cout<<"\nEnter maximum no. of publications : ";
        cin>>maxPublications;

        Publication **P;
        P = new Publication *[maxPublications];

        while(1)
        {
    //        clrscr();

            cout<<"\nProgram to show polymorphism for a Publication House"
                <<"\n----------------------------------------------------"
                <<"\n1. Enter data for Books"
                <<"\n2. Enter data for Tapes"
                <<"\n3. Show all data"
                <<"\n4. Show count of Books and Tapes"
                <<"\n0. Exit"
                <<"\n"
                <<"\nEnter your choice : ";

            cin>>choice;

            switch(choice)
            {
                case 1 :index++;
                    if(index >= maxPublications)
                    {
                        cout<<"\nInvalid index.";
                        index--;
                        break;
                    }

                    P[index] = new Book;
                    P[index]->readData();

                    break;

                case 2 :index++;
                    if(index >= maxPublications)
                    {
                        cout<<"\nInvalid index.";
                        index--;
                        break;
                    }

                    P[index] = new Tape;
                    P[index]->readData();

                    break;

                case 3 :for(i = 0 ; i <= index ; i++)
                    {
                        P[i]->showData();
                        cout<<"\n";
                    }
                    break;

                case 4 :for(i = 0 ; i <= index ; i++)
                    {
                        if(strcmp(typeid(*P[i]).name(), "class Book") == 0)
                        {
                            booksCount++;
                        }
                        cout<<"\n";
                    }
                    cout<<"\nTotal Books : "<<booksCount;
                    cout<<"\nTotal Tapes : "<<maxPublications - booksCount;
                    break;

                case 0 ://clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
            }
            getch();
        }
        return 0;
    }

    /*..............................End Of Program.............................*/

    Sunday, April 1, 2007 4:45 PM
  • you can also mark my answer ?.

    i will be sending u the unique programs later.

    Monday, April 2, 2007 6:48 PM
  • Yes friends, just post the questions and solutions, and ill mark as anwer for sure.....Guys keep posting,  This thread is being very useful for me to learn c/c++ .
    Tuesday, April 3, 2007 12:54 PM

  • // This program will create a dynamic 2d array in a  class...


    #include<iostream.h>
    #include<conio.h>

    class student
    {
        int rollno;
        char name[25];
        float **marks;
        int sub_size, exam_size;

        public :
        student(int,int);
        ~student();
        void display();
        void getdata();

    };

    student::student(int subj = 5, int exam = 3)
    {
        sub_size = subj; exam_size = exam;
        marks = new float*[subj];
        int i;
        for(i=0;i<subj;i++)
            marks[i] = new float[exam];
    }
    student::~student()
    {
        for(int i=0; i<sub_size;i++)
                delete marks[i];
        delete marks;
    }
    void student::getdata()
    {
        cout<<"Please give the marks :\n";
        for(int i=0;i<sub_size;i++)
        {
            cout<<"Subject No "<<i+ 1<<"\n";
            for(int j=0;j<exam_size;j++)
            {
                cout<<"Exam No "<<j + 1<<" : ";
                cin>>marks[i][j];
            }
        }
    }
    void student::display()
    {
        for(int i=0;i<sub_size; i++)
        {
            cout<<"Subject No "<<i + 1<<"\n";
            for(int j=0;j<exam_size;j++)
            {
                cout<<"Exam No "<<j + 1<<" : "<<marks[i][j]<<"   ";
            }
            cout<<"\n";
        }
    }

    void main()
    {
        student s1, s2(1,1), s3(1);
        clrscr();
        s1.getdata();
        s2.getdata();
        s3.getdata();
        clrscr();
        cout<<"Object With Default Parameter :\n";
        s1.display();
        cout<<"\nobject with both parameters being passed :\n";
        s2.display();
        cout<<"\nobject with one default parameter and one default :\n";
        s3.display();
        getch();
    }

    Tuesday, April 3, 2007 4:06 PM
  • // This program uses a friend function to give out the max scorer from a dca & mca student.



    #include<iostream.h>
    #include<conio.h>

    class dcastud;
    class mcastud
    {
        char name[15];
        int marks;
        float percentage;

        public :
        void getdata();

        friend void scorer(mcastud &m, dcastud &d);
    };

    void mcastud::getdata()
    {
        cout<<"Please give the name of the mca student : ";
        cin>>name;
        cout<<"Marks of the student : ";
        cin>>marks;
        cout<<"Percentage of the student : ";
        cin>>percentage;
    }

    class dcastud
    {
        char name[15];
        float percentage;

        public :
        void getdata();

        friend void scorer(mcastud &m, dcastud &d);
    };

    void dcastud::getdata()
    {
        cout<<"Please give the name of the dca student : ";
        cin>>name;
        cout<<"Percentage of the student : ";
        cin>>percentage;
    }

    void scorer(mcastud &m, dcastud &d)
    {
        if(m.percentage > d.percentage)
            cout<<"Mca Student scores more than Dca";
        else if (d.percentage > m.percentage)
            cout<<"Dca Student Scores more";
        else
            cout<<"Both scores equally";
    }


    void main()
    {
        mcastud m;
        dcastud d;
        clrscr();
        m.getdata();
        d.getdata();
        scorer(m,d);
        getch();

    }


    Tuesday, April 3, 2007 4:08 PM
  • //This program will display the use of inheriting base class in derieved class..


    #include<iostream.h>
    #include<conio.h>

    class person
    {
        protected :
        char name[15];
        char phone[12];
        char dob[10];

        public :
        void getdata();
        void putdata();
    };
    class student: public person
    {
        int roll_no;
        int marks;

        public:
        void get_stud_data();
        void put_stud_data();
    };
    void person::getdata()
    {
        cout<<"Please give the name : ";
        cin>>name;
        cout<<"Please give the phone no. : ";
        cin>>phone;
        cout<<"Please give the dob : ";
        cin>>dob;
    }
    void person::putdata()
    {
        cout<<"Name : "<<name<<"\n";
        cout<<"Phone : "<<phone<<"\n";
        cout<<"D O B : "<<dob<<"\n";
    }
    void student::get_stud_data()
    {
        getdata();
        cout<<"Please give the roll no : ";
        cin>>roll_no;
        cout<<"Please give the marks : ";
        cin>>marks;
    }
    void student::put_stud_data()
    {
        putdata();
        cout<<"Roll no : "<<roll_no<<"\n";
        cout<<"Marks : "<<marks<<"\n";
    }

    void main()
    {
        student s;
        clrscr();

        s.get_stud_data();
        s.put_stud_data();
        getch();
    }


    Tuesday, April 3, 2007 4:09 PM
  • // This program will demonstrate Casting Operator...


    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class student;
    class teacher
    {
        char *tname;
        float salary;
        public:
        teacher()
        {
            tname = NULL;
            salary = 0;
        }
        teacher(char *s, float f)
        {
            tname = new char[strlen(s) + 1];
            strcpy(tname,s);
            salary = f;
        }
        ~teacher()
        {
            delete tname;
        }
    /*    teacher(student s)
        {
            strcpy(tname, s.getname());
        }     */
        void getdata();
        void putdata();
    };
    class student
    {
        int rollno;
        char *name;
        float marks;

        public:
        student()
        {
            rollno = 0;
            marks = 0;
            name = NULL;
        }
        ~student()
        {
            delete name;
        }
        operator teacher()
        {

            return (teacher(name,0 ));
        }
        void getdata();
        void putdata();
    };


    void student::getdata()
    {
        cout<<"Please give the student Details : \n";
        char nm[20];
        cout<<"Please give the Rollno : ";
        cin>>rollno;
        cout<<"Please give the name : ";
        cin>>nm;
        cout<<"Please give the marks : ";
        cin>>marks;
        cout<<nm;
        name = new char[sizeof(strlen(nm) + 1)];
        strcpy(name,nm);
        cout<<"Debug ::: \n"<<name<<"::: Debug\n";
        name[strlen(nm)]=NULL;
    }
    void student::putdata()
    {
        cout<<"Roll No : "<<rollno<<"   Name :"<<name<<"\n   Marks : "<<marks<<"\n";
    }
    void teacher::getdata()
    {
        cout<<"Please give the Teacher Details : \n";
        char nm[20];
        cout<<"Please give the name : ";
        cin>>nm;
        cout<<"Please give the salary :";
        cin>>salary;
        tname = new char[sizeof(strlen(nm) + 1)];
        strcpy(tname, nm);
        tname[strlen(tname)]=NULL;
    }

    void teacher::putdata()
    {
        cout<<"Name : "<<tname<<"   Salary :"<<salary<<"\n";
    }

    void main()
    {
        student s;
        teacher t;

        clrscr();
        s.getdata();
        t.getdata();
        s.putdata();
        t.putdata();
    /*    t = s;
        t.putdata();  */
        getch();
    }


    Tuesday, April 3, 2007 4:10 PM
  • // WAP to find the Exponent of a number using recursive function.


    #include<stdio.h>
    #include<conio.h>
    float exponent(float number, int power);

    void main()
    {
        float number;
        int power;

        clrscr();
        printf("Please give the number : ");
        scanf("%f",&number);
        printf("Please give the power of the number : ");
        scanf("%d", &power);
        printf("Ans = %.3f",exponent(number, power));
        getch();
    }

    float exponent(float number, int power)
    {
        if(power==1)
            return(number);
        else if(power%2==0)
            return(exponent(number*number,power/2));
        else
            return(exponent(number*number,power/2)*number);
    }


    Tuesday, April 3, 2007 4:14 PM
  • Very good work Varun, it seems like you are the only one who knows c/c++. 

     

    Keep posting more..... thx a lot in advance.

    Tuesday, April 3, 2007 6:27 PM
  • #include<iostream.h>
     class triangle
     {
      int i,j,value;
      public:
       void getdata(void);
       void tri(void);
    };

    void triangle::getdata(void)
    {
     cout<<"\n enter the value:";
     cin>>value;

     void triangle::tri(void)
     {
      int k=1;
      for(i=1;i<=value;i++)
     {
       for(j=1;j<=i;j++)

       cout<<k++;
       cout<<"\n";
     }
     }
     int main()
     {
      triangle t;
      t.getdata();
      t.tri();

      return 0;
    }

    Wednesday, April 4, 2007 3:56 PM
  • great work guys, keep it up. Keep on posting Wink
    Wednesday, April 4, 2007 4:46 PM
  • If statements:
    The structure of an if statement is as follows:
     
    if ( TRUE )
    Execute the next statement
    To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of a function. Anything inside braces is called a compound statement, or a block.

    For example:
     
    if ( TRUE ) {
    Execute all statements inside the braces
    }
    There is also the else statement. The code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.

    It can look like this:
     
    if ( TRUE ) {
    // Execute these statements if TRUE
    }
    else {
    // Execute these statements if FALSE
    }
    One use for else is if there are two conditional statements that may both evaluate to true, yet you wish only one of the two to have the code block following it to be executed. You can use an else if after the if statement; that way, if the first statement is true, the else if will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements.

    Let's look at a simple program for you to try out on your own.
     
    #include <iostream>

    using namespace std;

    int main() // Most important part of the program!
    {
    int age; // Need a variable...

    cout<<"Please input your age: "; // Asks for age
    cin>> age; // The input is put in age
    cin.ignore(); // Throw away enter
    if ( age < 100 ) { // If the age is less than 100
    cout<<"You are pretty young!\n"; // Just to show you it works...
    }
    else if ( age == 100 ) { // I use else just to show an example
    cout<<"You are old\n"; // Just to show you it works...
    }
    else {
    cout<<"You are really old\n"; // Executed if no other statement is
    }
    cin.get();
    }

    Tuesday, April 10, 2007 7:57 PM
  • A Program Using Functions :
    #include <iostream>

    using namespace std;

    int mult ( int x, int y );

    int main()
    {
    int x;
    int y;

    cout<<"Please input two numbers to be multiplied: ";
    cin>> x >> y;
    cin.ignore();
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    cin.get();
    }

    int mult ( int x, int y )
    {
    return x * y;
    }

    Tuesday, April 10, 2007 8:03 PM
  • A Program Using Switch Case:
    #include <stdio.h>

    void playgame();
    void loadgame();
    void playmultiplayer();

    int main()
    {
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
    case 1: /* Note the colon, not a semicolon */
    playgame();
    break;
    case 2:
    loadgame();
    break;
    case 3:
    playmultiplayer();
    break;
    case 4:
    printf( "Thanks for playing!\n" );
    break;
    default:
    printf( "Bad input, quitting!\n" );
    break;
    }
    getchar();

    }

    Tuesday, April 10, 2007 8:04 PM
  • Hey friends, your responses has been really overwhelming. Hope this continues .... This thread has been really very helpful for me.
    Wednesday, April 11, 2007 3:45 PM
  • I have lots and lots of programs, but i am not getting much time for this. exams are coming near, but dont worry m8, ill send more and more when i am free Smile
    Dont say its spam when i start posting the C Programs Stick out tongue j/k
    Wednesday, April 11, 2007 4:04 PM

  • There are two classes Emp and Employee. Emp is defined in the payroll
    department containing details about employee id and details about his / her
    payment. Employee is a Human Resource Department class containing only basic
    salary details and full personal details like name of spouse, number of
    children, previous experience of an employee etc. Add code in the Emp
    class such that, conversion from one type of employee object into another
    is possible. While converting, items which are not there in the source
    class (like No. of children when source class is Employee) should take a
    default value.


    #include<iostream.h>
    #include<string.h>
    #include<conio.h>

    class Emp;

    class Employee
    {
        int empId, child;
        char name[30], spouse[40];
        float exp, salary;

        public:

        Employee(void){}
        Employee(int, char *, float);

        char * returnName(void);
        int returnId(void);
        float returnSalary(void);

        void readData(void);
        void showData(void);
    };

    Employee::Employee(int id, char *n, float s)
    {
        int ch;
        empId = id;
        strcpy(name, n);
        strcpy(spouse, "Mrs. ");
        strcat(spouse, n);

        cout<<"\n\nDo you want to enter the number of children(1yes/0no) : ";
        cin>>ch;

        if(ch == 1)
        {
            cout<<"\n\nEnter the number of children of "<<n<<" : ";
            cin>>child;
        }
        else
        {
            child = 2;
            cout<<"\n\nNumber of children set to 2.";
        }

        cout<<"\n\nDo you want to enter the experience(1yes/0no) : ";
        cin>>ch;

        if(ch == 1)
        {
            cout<<"\n\nEnter the number of years of "
                <<"experienece of "<<n<<" : ";
            cin>>exp;
        }
        else
        {
            exp = 2;
            cout<<"\n\nExperience set to 2 years.";
        }
        salary = s;
    }

    char * Employee::returnName(void)
    {
        char *n = new char[strlen(name)];
        strcpy(n, name);
        return n;
    }

    int Employee::returnId(void)
    {
        return empId;
    }

    float Employee::returnSalary(void)
    {
        return salary;
    }

    void Employee::readData(void)
    {
        cout<<"\n\nEnter the id                           : ";
        cin>>empId;
        cout<<"\n\nEnter the name of the employee         : ";
        cin>>name;
        cout<<"\n\nEnter the name of his spouse           : ";
        cin>>spouse;
        cout<<"\n\nEnter the number of his children       : ";
        cin>>child;
        cout<<"\n\nEnter the years of his past experience : ";
        cin>>exp;
        cout<<"\n\nEnter his basic salary                 : ";
        cin>>salary;
    }

    void Employee::showData(void)
    {
        cout<<"\nID           : "<<empId;
        cout<<"\nName         : "<<name;
        cout<<"\nSpouse       : "<<spouse;
        cout<<"\nChildren     : "<<child;
        cout<<"\nExperience   : "<<exp;
        cout<<"\nBasic Salary : "<<salary;
    }

    class Emp
    {
        int empId;
        char name[20];
        float sal, da, hra, ta;

        public:

        Emp(){}
        Emp(Employee);

        void readData(void);
        operator Employee(void);
        void showData(void);
    };

    Emp::Emp(Employee E)
    {
        int ch;
        char *c = E.returnName();

        strcpy(name,c);
        empId = E.returnId();
        sal = E.returnSalary();

        cout<<"\n\nDo you want to enter the da(1yes/0no) : ";
        cin>>ch;

        if(ch == 1)
        {
            cout<<"\n\nEnter the da : ";
            cin>>da;
        }
        else
        {
            da = (sal * 20) / 100;
            cout<<"\n\nDA set to 20% of salary.";
        }

        cout<<"\n\nDo you want to enter the ta(1yes/0no) : ";
        cin>>ch;

        if(ch == 1)
        {
            cout<<"\n\nEnter the ta : ";
            cin>>ta;
        }
        else
        {
            ta = (sal * 15) / 100;
            cout<<"\n\nTA set to 15% of salary.";
        }

        cout<<"\n\nDo you want to enter the hra(1yes/0no) : ";
        cin>>ch;

        if(ch == 1)
        {
            cout<<"\n\nEnter the hra : ";
            cin>>hra;
        }
        else
        {
            hra = (sal * 40) / 100;
            cout<<"\n\nHRA set to 40% of salary.";
        }
    }

    void Emp::readData(void)
    {
        cout<<"\n\nEnter the employee's ID : ";
        cin>>empId;
        cout<<"\n\nEnter your name         : ";
        cin>>name;
        cout<<"\n\nEnter your basic salary : ";
        cin>>sal;
        cout<<"\n\nEnter your da           : ";
        cin>>da;
        cout<<"\n\nEnter your ta           : ";
        cin>>ta;
        cout<<"\n\nEnter your hra          : ";
        cin>>hra;
    }

    Emp::operator Employee(void)
    {
        return Employee(empId, name, (sal+da+ta+hra));
    }

    void Emp::showData(void)
    {
        cout<<"\nEmployee ID : "<<empId;
        cout<<"\nName        : "<<name;
        cout<<"\nBasic       : "<<sal;
        cout<<"\nD/A         : "<<da;
        cout<<"\nT/A         : "<<ta;
        cout<<"\nHRA         : "<<hra;
    }

    int main(void)
    {
        Emp E1, E2;
        Employee Emp1, Emp2;

        clrscr();

        cout<<"\n\nData for E1 : ";
        E1.readData();

        cout<<"\n\nEmp2 = E1";
        Emp2 = E1;

        cout<<"\n\nE1 : \n\n";
        E1.showData();
        cout<<"\n\nemp2 : \n\n";
        Emp2.showData();

        getch();

        cout<<"\n\n Data for Emp1 : ";
        Emp1.readData();

        cout<<"\n\nE2 = Emp1";
        E2 = Emp1;

        cout<<"\n\nEmp1 : \n\n";
        Emp1.showData();
        cout<<"\n\ne2 : \n\n";
        E2.showData();

        getch();
        return 0;
    }

    Friday, April 13, 2007 1:01 PM

  • Create a base class called shape. Use this class to store two double type
    values that could be used to compute the area of figures. Derive three
    specific classes called triangle, rectangle and circle from the base shape.
    Add to the base class, a member function get_data() to initialize base class
    data members and another member function display_area() to compute and
    display the area of figures. Make display_area() as a virtual function and
    redefine this function in derived classes to suit their requirements. Using
    these three classes, design a program that will accept dimensions of a
    triangle or rectangle interactively and store it in one array. After having
    read all the input display the area of all the figures whose area has been
    read in the program. Remember the two values given as input will be treated
    as lengths of two sides in the case of rectangle and as base and height in
    case of triangle. In case of circle, only one value should be accepted which
    will be taken as the radius and the default value of the next parameter
    should be 0.


    #include<iostream.h>
    #include<process.h>
    #include<conio.h>

    class Shape
    {
        protected:

        double dimension1, dimension2;

        public:

        void virtual getData(void){}
        void virtual displayArea(void){}
    };

    class Rectangle:public Shape
    {
        public:

        void getData(void);
        void displayArea(void);
    };

    void Rectangle::getData(void)
    {
        cout<<"\n";
        cout<<"Enter Length  : ";
        cin>>dimension1;
        cout<<"Enter Breadth : ";
        cin>>dimension2;
    }

    void Rectangle::displayArea(void)
    {
        cout<<"\nLength  : "<<dimension1;
        cout<<"\nBreadth : "<<dimension2;
        cout<<"\nArea    : "<<dimension1 * dimension2;
    }

    class Triangle:public Shape
    {
        public:

        void getData(void);
        void displayArea(void);
    };

    void Triangle::getData(void)
    {
        cout<<"\n";
        cout<<"Enter Base   : ";
        cin>>dimension1;
        cout<<"Enter Height : ";
        cin>>dimension2;
    }

    void Triangle::displayArea(void)
    {
        cout<<"\nBase   : "<<dimension1;
        cout<<"\nHeight : "<<dimension2;
        cout<<"\nArea   : "<<(dimension1 * dimension2)/2.0;
    }

    class Circle:public Shape
    {
        public:

        void getData(void);
        void displayArea(void);
    };

    void Circle::getData(void)
    {
        cout<<"\nEnter Radius : ";
        cin>>dimension1;
        dimension2 = 0;
    }

    void Circle::displayArea(void)
    {
        cout<<"\nRadius : "<<dimension1;
        cout<<"\nArea   : "<<3.14 * dimension1 * dimension1;
    }

    int main(void)
    {
        Shape *S[10];

        int choice, ctr=0;

        while(1)
        {
            clrscr();

            cout<<"\nProgram to perform operations on Shape Class"
                <<"\n--------------------------------------------"
                <<"\n1. Enter dimensions for Rectangle"
                <<"\n2. Enter dimensions for Triangle"
                <<"\n3. Enter dimensions for Circle"
                <<"\n4. Display Areas of all Shapes"
                <<"\n0. Exit"
                <<"\n"
                <<"\nEnter your choice : ";

            cin>>choice;

            switch(choice)
            {
                case 1 :S[ctr]=new Rectangle;
                    S[ctr]->getData();
                    ctr++;
                    break;

                case 2 :S[ctr]=new Triangle;
                    S[ctr]->getData();
                    ctr++;
                    break;

                case 3 :S[ctr]=new Circle;
                    S[ctr]->getData();
                    ctr++;
                    break;

                case 4 :clrscr();

                    cout<<"\nAreas of different shapes are : "
                        <<"\n--------------------------------\n";

                    for(int j = 0 ; j < ctr ; j++)
                    {
                        S[j]->displayArea();
                        cout<<"\n\n";
                        cout<<"-=-=-=-=-=-=-=-=-=-=-=-=-\n";
                    }

                    break;

                case 0 :clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
            }
            getch();
        }
    }

    Friday, April 13, 2007 1:04 PM

  • Overload all the four arithmetic operators to operate on a vector class and
    also overload the * operator to multiply scalar values to the vector class.
    Overload the >> operator to input a vector and the << operator to display
    the vector in the form (10, 20, 30, ...). Also overload [] operator to access
    the individual member of the vector. Use dynamic memory allocation to
    achieve the solution. Write appropriate constructor and destructor for
    the class.


    #include<iostream.h>
    #include<process.h>
    #include<conio.h>

    int min(int value1, int value2)
    {
        return ( (value1 < value2) ? value1 : value2);
    }

    int max(int value1, int value2)
    {
        return ( (value1 > value2) ? value1 : value2);
    }

    class Vector
    {
        float *vector;
        int size;

        public:

        Vector(void);
        Vector(int);
        Vector(Vector &);

        friend istream & operator>>(istream &, Vector &);
        friend ostream & operator<<(ostream &, Vector &);

        Vector operator+(Vector &);
        Vector operator-(Vector &);
        Vector operator*(int);
        Vector operator/(int);
        void operator=(Vector &);

        ~Vector(void);
    };

    Vector::Vector(void)
    {
        vector = NULL;
        size = 0;
    }

    Vector::Vector(int length)
    {
        size = length;
        vector = new float [size];
    }

    Vector::Vector(Vector &V)
    {
        size = V.size;
        vector = new float [size];

        for(int ctr = 0 ; ctr < size ; ctr++)
        {
            vector[ctr] = V.vector[ctr];
        }
    }

    istream & operator>>(istream &din, Vector &V)
    {
        for(int ctr = 0 ; ctr < V.size ; ctr++)
        {
            cout<<"Enter value for "<<ctr+1<<" position : ";
            din>>V.vector[ctr];
        }
        return din;
    }

    ostream & operator<<(ostream &dout, Vector &V)
    {
        dout<<"(";
        for(int ctr = 0 ; ctr < V.size ; ctr++)
        {
            dout<<V.vector[ctr]<<", ";
        }
        dout<<"\b\b)";
        return dout;
    }

    Vector Vector::operator+(Vector &V)
    {
        int minsize, ctr;
        Vector Ans;

        minsize = min(size, V.size);
        Ans = Vector(max(size, V.size));

        for(ctr = 0 ; ctr < minsize ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr] + V.vector[ctr];
        }

        for(; ctr < size ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr];
        }

        for(; ctr < V.size ; ctr++)
        {
            Ans.vector[ctr] = V.vector[ctr];
        }

        return Ans;
    }

    Vector Vector::operator-(Vector &V)
    {
        int minsize, ctr;
        Vector Ans;

        minsize = min(size, V.size);
        Ans = Vector(max(size, V.size));

        for(ctr = 0 ; ctr < minsize ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr] - V.vector[ctr];
        }

        for(; ctr < size ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr];
        }

        for(; ctr < V.size ; ctr++)
        {
            Ans.vector[ctr] = -V.vector[ctr];
        }

        return Ans;
    }

    Vector Vector::operator*(int scalar)
    {
        Vector Ans(size);

        for(int ctr = 0 ; ctr < size ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr] * scalar;
        }

        return Ans;
    }

    Vector Vector::operator/(int scalar)
    {
        Vector Ans(size);

        for(int ctr = 0 ; ctr < size ; ctr++)
        {
            Ans.vector[ctr] = vector[ctr] / scalar;
        }

        return Ans;
    }

    void Vector::operator=(Vector &V)
    {
        size = V.size;
        vector = new float[size];

        for(int ctr = 0 ; ctr < size ; ctr++)
        {
            vector[ctr] = V.vector[ctr];
        }
    }

    Vector::~Vector(void)
    {
        delete vector;
    }

    int main(void)
    {
        Vector V1, V2;
        int choice, size, value;

        while(1)
        {
            clrscr();
            cout<<"\nProgram to perform operations on Vector "
                <<"using operator overloading"
                <<"\n----------------------------------------"
                <<"--------------------------"
                <<"\n1. Create Vectors"
                <<"\n2. Show Vectors"
                <<"\n3. Add Vectors (V1 + V2)"
                <<"\n4. Subtract Vectors (V1 - V2)"
                <<"\n5. Multiply Vectors by scalar (Vector * scalar)"
                <<"\n6. Divide Vectors by scalar (Vector / scalar)"
                <<"\n7. Exit"
                <<"\n"
                <<"\nEnter your choice : ";
            cin>>choice;
            switch(choice)
            {
                case 1 :cout<<"\nEnter size for 1st vector : ";
                    cin>>size;
                    V1 = Vector(size);
                    cin>>V1;
                    cout<<"\nEnter size for 2nd vector : ";
                    cin>>size;
                    V2 = Vector(size);
                    cin>>V2;
                    break;
                case 2 :cout<<"\n1st Vector is : \n";
                    cout<<V1;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2;
                    cout<<"\n";
                    break;
                case 3 :cout<<"\n1st Vector is : \n";
                    cout<<V1;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2;
                    cout<<"\n";
                    cout<<"\nResultant Vector after "
                        <<"addition is : \n";
                    cout<<V1 + V2;
                    cout<<"\n";
                    break;
                case 4 :cout<<"\n1st Vector is : \n";
                    cout<<V1;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2;
                    cout<<"\n";
                    cout<<"\nResultant Vector after "
                        <<"subtraction is : \n";
                    cout<<V1-V2;
                    cout<<"\n";
                    break;
                case 5 :cout<<"\nEnter scalar value to multiply "
                        <<"vectors with : ";
                    cin>>value;
                    cout<<"\n1st Vector is : \n";
                    cout<<V1;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2;
                    cout<<"\n";
                    cout<<"\nVectors after scalar "
                        <<"multiplication are : \n";
                    cout<<"\n1st Vector is : \n";
                    cout<<V1 * value;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2 * value;
                    cout<<"\n";
                    break;
                case 6 :do
                    {
                        cout<<"\nEnter scalar value to "
                            <<"divide vectors with : ";
                        cin>>value;
                    }while(value==0);
                    cout<<"\n1st Vector is : \n";
                    cout<<V1;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2;
                    cout<<"\n";
                    cout<<"\nVectors after scalar "
                        <<"division are : \n";
                    cout<<"\n1st Vector is : \n";
                    cout<<V1 / value;
                    cout<<"\n";
                    cout<<"\n2nd Vector is : \n";
                    cout<<V2 / value;
                    cout<<"\n";
                    break;
                case 7 :clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
            }
            getch();
        }
    }

    Friday, April 13, 2007 1:07 PM

  • Create a date class with the following capabilities :
        (a) Output the date in multiple formats such as
            DD MM YYYY
            MM/DD/YY
            May 14, 2001
        (b) Use overloaded constructors to create Date objects initialized
            with date format in section (a).
        (c) Overload operators for testing equality of two dates and for
            comparing dates to determine if one date is prior to, or after
            another date.
        (d) Create a member function nextday to increment the day by day.
            The date object function should always remain in consistent
            state. Be sure to test following cases :
            (i)  Incrementing to next month
            (ii) Incrementing into the next year.


    #include<iostream.h>
    #include<process.h>
    #include<conio.h>
    #include<string.h>

    class Date
    {
        int dd;
        int mm;
        int yy;

        static int monthDays[12];
        static char months[12][4];

        public:

        Date(void);

        Date(int, int, int);
        Date(int, char, int, char, int);
        Date(char *, int, int);

        void loadMonths(void);

        int leapYear(int);
        int validDate(void);

        void displayDate(int);

        int operator==(Date &);
        int operator<(Date &);
    };

    int Date::monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    char Date::months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",  "Nov", "Dec"};

    Date::Date(void)
    {
        dd = mm = yy = 0;
    }

    Date::Date(int d, int m, int y)
    {
        dd = d;
        mm = m;
        yy = y;

        if(validDate() == 0)
        {
            cout<<"\nInvalid Date.";
            dd = mm = yy = 0;
            return;
        }
    }

    Date::Date(int m, char c1, int d, char c2 , int y)
    {
        mm = m;
        dd = d;
        yy = y;

        if(validDate() == 0)
        {
            cout<<"\nInvalid Date.";
            dd = mm = yy = 0;
            return;
        }
    }

    Date::Date(char month[3], int d, int y)
    {
        dd = d;
        yy = y;

        for(int ctr = 0 ; ctr < 12 ; ctr++)
        {
            if(strcmp(months[ctr], month) == 0)
            {
                mm = ctr+1;
            }
        }

        if(validDate() == 0)
        {
            cout<<"\nInvalid Date.";
            dd = mm = yy = 0;
            return;
        }
    }

    int Date::leapYear(int year)
    {
        if(year%4 == 0)
        {
            if(year%100 == 0)
            {
                if(year%400 == 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 0;
        }
    }

    int Date::validDate(void)
    {
        if(mm > 0 && mm < 13)
        {
            if(leapYear(yy) == 1 && mm == 2)
            {
                if(dd > 0 && dd < 30)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                if(dd > 0 && dd <= monthDays[mm-1])
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
        else
        {
            return 0;
        }
    }

    void Date::displayDate(int format)
    {

        if(dd > 0 && (format > 0 && format < 4))
        {
            cout<<"\nDate in desired format : ";
            if(format == 1)
            {
                cout<<dd<<" "<<mm<<" "<<yy;
            }
            else if(format == 2)
            {
                cout<<mm<<"/"<<dd<<"/"<<yy%100;
            }
            else if(format == 3)
            {
                cout<<months[mm-1]<<" "<<dd<<", "<<yy;
            }
        }
        else
        {
            cout<<"\nInvalid date or date not entered.";
        }
    }

    int Date::operator==(Date &D)
    {
        if(dd == D.dd && mm == D.mm && yy == D.yy)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    int Date::operator<(Date &D)
    {
        if(yy < D.yy)
        {
            return 1;
        }
        else if(yy == D.yy)
        {
            if(mm < D.mm)
            {
                return 1;
            }
            else if(mm == D.mm)
            {
                if(dd < D.dd)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    int main(void)
    {
        Date D1, D2, D3;
        int choice, dd, mm, yy;
        char month[3];

        while(1)
        {
            clrscr();

            cout<<"\nProgram to perform operations on Date class"
                <<"\n-------------------------------------------"
                <<"\n1. Enter date in format DD MM YYYY   (1st Date)"
                <<"\n2. Enter date in format MM/DD/YY     (2nd Date)"
                <<"\n3. Enter date in format MMM DD, YYYY (3rd Date)"
                <<"\n4. Show date in format DD MM YYYY"
                <<"\n5. Show date in format MM/DD/YY"
                <<"\n6. Show date in format MMM DD, YYYY"
                <<"\n7. Compare two dates"
                <<"\n8. Check sequences of dates"
                <<"\n9. Exit"
                <<"\n"
                <<"\nEnter your choice : ";

            cin>>choice;

            switch(choice)
            {
                case 1 :cout<<"\nEnter day   : ";
                    cin>>dd;
                    cout<<"\nEnter month : ";
                    cin>>mm;
                    cout<<"\nEnter year  : ";
                    cin>>yy;
                    D1 = Date(dd, mm, yy);
                    break;

                case 2 :cout<<"\nEnter month : ";
                    cin>>mm;
                    cout<<"\nEnter day   : ";
                    cin>>dd;
                    cout<<"\nEnter year  : ";
                    cin>>yy;
                    D2 = Date(mm, '/', dd, '/',  yy);
                    break;

                case 3 :cout<<"\nEnter month : ";
                    cin>>month;
                    cout<<"\nEnter day   : ";
                    cin>>dd;
                    cout<<"\nEnter year  : ";
                    cin>>yy;
                    D3 = Date(month, dd, yy);
                    break;

                case 4 :cout<<"\nEntered dates are : ";
                    cout<<"\nDate 1 : ";
                    cout<<"\n---------";
                    D1.displayDate(1);
                    cout<<"\nDate 2 : ";
                    cout<<"\n---------";
                    D2.displayDate(1);
                    cout<<"\nDate 3 : ";
                    cout<<"\n---------";
                    D3.displayDate(1);
                    break;

                case 5 :cout<<"\nEntered dates are : ";
                    cout<<"\nDate 1 : ";
                    cout<<"\n---------";
                    D1.displayDate(2);
                    cout<<"\nDate 2 : ";
                    cout<<"\n---------";
                    D2.displayDate(2);
                    cout<<"\nDate 3 : ";
                    cout<<"\n---------";
                    D3.displayDate(2);
                    break;

                case 6 :cout<<"\nEntered dates are : ";
                    cout<<"\nDate 1 : ";
                    cout<<"\n---------";
                    D1.displayDate(3);
                    cout<<"\nDate 2 : ";
                    cout<<"\n---------";
                    D2.displayDate(3);
                    cout<<"\nDate 3 : ";
                    cout<<"\n---------";
                    D3.displayDate(3);
                    break;

                case 7 :cout<<"\n1st and 2nd Date : ";
                    cout<<"\n-------------------";
                    if(D1 == D2)
                    {
                        cout<<"\nBoth dates are equal.";
                    }
                    else
                    {
                        cout<<"\nBoth dates are unequal.";
                    }
                    cout<<"\n";
                    cout<<"\n1st and 3rd Date : ";
                    cout<<"\n-------------------";
                    if(D1 == D3)
                    {
                        cout<<"\nBoth dates are equal.";
                    }
                    else
                    {
                        cout<<"\nBoth dates are unequal.";
                    }
                    cout<<"\n";
                    cout<<"\n2nd and 3rd Date : ";
                    cout<<"\n-------------------";
                    if(D2 == D3)
                    {
                        cout<<"\nBoth dates are equal.";
                    }
                    else
                    {
                        cout<<"\nBoth dates are unequal.";
                    }
                    break;

                case 8 :cout<<"\n1st and 2nd Date : ";
                    cout<<"\n-------------------";
                    if(D1 < D2)
                    {
                        cout<<"\n1st date is prior to 2nd.";
                    }
                    else
                    {
                        cout<<"\n2nd date is prior to 1st.";
                    }
                    cout<<"\n";
                    cout<<"\n1st and 3rd Date : ";
                    cout<<"\n-------------------";
                    if(D1 < D3)
                    {
                        cout<<"\n1st date is prior to 3rd.";
                    }
                    else
                    {
                        cout<<"\n3rd date is prior to 1st.";
                    }
                    cout<<"\n";
                    cout<<"\n2nd and 3rd Date : ";
                    cout<<"\n-------------------";
                    if(D2 < D3)
                    {
                        cout<<"\n2nd date is prior to 3rd.";
                    }
                    else
                    {
                        cout<<"\n3rd date is prior to 2nd.";
                    }
                    break;

                case 9 :clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
                    break;
            }
            getch();
        }
    }

    Friday, April 13, 2007 1:09 PM

  • Design a class date which sets date object to dd, mm, yyyy format. Overload
    + and - operators for the class date to add given no. of days to a
    particular date object as d1 = d1 + 10 and d1 = 10 + d1 where d1 is set
    using a constructor to some valid date and to find difference between two
    given date. Display the original dates, after addition and the difference.
    The date objects created should be validated in the contructor itself.


    #include<iostream.h>
    #include<process.h>
    #include<conio.h>
    #include<string.h>

    class Date
    {
        int dd;
        int mm;
        int yy;

        static int monthDays[12];
        static char months[12][4];

        public:

        Date(void);

        Date(int, int, int);

        void loadMonths(void);

        int leapYear(int);
        int validDate(void);

        friend ostream & operator<<(ostream &, Date &);

        friend Date operator+(Date &, int);
        friend Date operator+(int, Date &);

        friend Date operator-(Date &, int);
    };

    int Date::monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    char Date::months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",  "Nov", "Dec"};

    Date::Date(void)
    {
        dd = mm = yy = 0;
    }

    Date::Date(int d, int m, int y)
    {
        dd = d;
        mm = m;
        yy = y;

        if(validDate() == 0)
        {
            cout<<"\nInvalid Date.";
            dd = mm = yy = 0;
            return;
        }
    }

    int Date::leapYear(int year)
    {
        if(year%4 == 0)
        {
            if(year%100 == 0)
            {
                if(year%400 == 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 1;
            }
        }
        else
        {
            return 0;
        }
    }

    int Date::validDate(void)
    {
        if(mm > 0 && mm < 13)
        {
            if(leapYear(yy) == 1 && mm == 2)
            {
                if(dd > 0 && dd < 30)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                if(dd > 0 && dd <= monthDays[mm-1])
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
        else
        {
            return 0;
        }
    }

    ostream & operator<<(ostream &dout, Date &D)
    {
        dout<<D.dd<<" "<<D.mm<<" "<<D.yy;

        return dout;
    }

    Date operator+(Date &D, int days)
    {
        Date Temp;

        if(D.leapYear(D.yy) == 0)
        {
            if(D.dd + days <= Date::monthDays[D.mm-1])
            {
                Temp.dd = D.dd + days;
                Temp.mm = D.mm;
                Temp.yy = D.yy;
            }
            else
            {
                Temp.dd = (D.dd + days) - Date::monthDays[D.mm-1];
                if(D.mm < 12)
                {
                    Temp.mm = D.mm + 1;
                    Temp.yy = D.yy;
                }
                else
                {
                    Temp.mm = 1;
                    Temp.yy = D.yy + 1;
                }
            }
        }
        else
        {
            if(29 - (D.dd + days) >= 0)
            {
                Temp.dd = D.dd + days;
                Temp.mm = 2;
                Temp.yy = D.yy;
            }
            else
            {
                Temp.dd = D.dd + days - 29;
                Temp.mm = 3;
                Temp.yy = D.yy;
            }
        }
        return Temp;
    }

    Date operator+(int days, Date &D)
    {
        Date Temp;

        if(D.dd + days <= Date::monthDays[D.mm-1])
        {
            Temp.dd = D.dd + days;
            Temp.mm = D.mm;
            Temp.yy = D.yy;
        }
        else
        {
            Temp.dd = (D.dd + days) - Date::monthDays[D.mm-1];
            if(D.mm < 12)
            {
                Temp.mm = D.mm + 1;
                Temp.yy = D.yy;
            }
            else
            {
                Temp.mm = 1;
                Temp.yy = D.yy + 1;
            }
        }
        return Temp;
    }

    Date operator-(Date &D, int days)
    {
        Date Temp;

        if(D.dd - days > 0)
        {
            Temp.dd = D.dd - days;
            Temp.mm = D.mm;
            Temp.yy = D.yy;
        }
        else
        {
            if(D.mm > 1)
            {
                if(D.mm == 3 && D.leapYear(D.yy) == 1)
                {
                    Temp.dd = 29 + D.dd - days;
                }
                else
                {
                    Temp.dd = D.monthDays[D.mm-2] + D.dd - days;
                }
                Temp.mm = D.mm - 1;
                Temp.yy = D.yy;
            }
            else
            {
                Temp.dd = 31 + D.dd - days;
                Temp.mm = 12;
                Temp.yy = D.yy - 1;
            }
        }

        return Temp;
    }

    int main(void)
    {
        Date D1;
        int choice, dd, mm, yy, daysToOperate;

        while(1)
        {
            clrscr();

            cout<<"\nProgram to perform operations on Date class"
                <<"\n-------------------------------------------"
                <<"\n1. Enter date"
                <<"\n2. Show date"
                <<"\n3. Add days (Date + days)"
                <<"\n4. Add days (days + Date)"
                <<"\n5. Subtract days (Date - days)"
                <<"\n6. Exit"
                <<"\n"
                <<"\nEnter your choice : ";

            cin>>choice;

            switch(choice)
            {
                case 1 :cout<<"\nEnter day   : ";
                    cin>>dd;
                    cout<<"\nEnter month : ";
                    cin>>mm;
                    cout<<"\nEnter year  : ";
                    cin>>yy;
                    D1 = Date(dd, mm, yy);
                    break;

                case 2 :cout<<D1;
                    break;

                case 3 :cout<<"\nEnter days to add : ";
                    cin>>daysToOperate;
                    cout<<D1 + daysToOperate;
                    break;

                case 4 :cout<<"\nEnter days to add : ";
                    cin>>daysToOperate;
                    cout<<daysToOperate + D1;
                    break;

                case 5 :cout<<"\nEnter days to subtract : ";
                    cin>>daysToOperate;
                    cout<<D1 - daysToOperate;
                    break;

                case 6 :clrscr();
                    cout<<"\n\n\n\n\n"
                        <<"Thank you for using the program."
                        <<"\n\n"
                        <<"Press any key to exit...";
                    getch();
                    exit(0);
                    break;
            }
            getch();
        }
    }

    Friday, April 13, 2007 1:11 PM
  • #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    #include<dos.h>

    const char *mname[]={"January","February","March","April","May",
            "June","July","August","September","October","November",
            "December"
          };

    int mday[]={31,28,31,30,31,30,31,31,30,31,30,31};

     int isleap(unsigned int year)
     {
      if((year%400==0) || ((year%4==0) && (year%100!=0)))
        return 1;
      else
       return 0;
     }

     int countday(int year)
     {
      int i,sum=0;
      for(i=1;i<year;i++)
       if(isleap(i))
        sum++;
       return(year-1+sum+1)%7;
     }

     void prnmonth(int day,int year,int month)
     {
      int i,j,k=1,x=20,y=7;
      clrscr();
      _setcursortype(0);
      gotoxy(x,y);
      textattr(30);
      cprintf("  Sun");
      textattr(26);
      cprintf("   Mon   Tue   Wed   Thr   Fri   Sat ");
      gotoxy(x,y+1);
      textattr(7);
      textcolor(7);
      printf("%c",201);

      for(i=1;i<42;i++)
        printf("%c",205);
      printf("%c",187);
      for(i=2;i<11;i++)
      {
       gotoxy(x,y+i);
       printf("%c",186);
      }
      for(i=2;i<11;i++)
      {
       gotoxy(x+42,y+i);
       printf("%c",186);
      }
      gotoxy(x,y+11);
      printf("%c",200);
      for(i=1;i<42;i++)
       printf("%c",205);
      printf("%c",188);

      textcolor(12);
      gotoxy(x+2,y+10);
      cprintf("%s",mname[month-1]);
      gotoxy(x+36,y+10);
      cprintf("%5d",year);
      textcolor(7);
      i=x+3+6*day;
      j=y+3;
      gotoxy(i,j);
      for(k=1;k<=mday[month-1];k++)
      {
       printf("%2d",k);
       if(i<=x+35)
         i+=6;
       else
       {
        j++;
        i=x+3;
       }
       gotoxy(i,j);
      }
     }

     void calender(int year,int month)
     {
      int i,sum=0;
      clrscr();
      if(isleap(year))
       mday[1]=29;
      else
       mday[1]=28;
      for(i=0;i<month;i++)
      sum+=mdayIdea;
      prnmonth((sum+countday(year))%7,year,month);
     }

     int valid(int year,int month)
     {
      if((year>=0 && year<=32766) &&(month>=1 && month<=12))
        return 1;
      else
       return 0;
     }

     void main(void)
     {
      int year,month;
      struct date cuda;
      char ch=' ';
      clrscr();
      textcolor(7);
      printf("Enter Year>");
      scanf("%d",&year);
      printf("\n Enter month>");
      scanf("%d",&month);
      if(!valid(year,month))
      {
       printf("Invalid Date\n\a\nPress any key to view this month.");
       getch();
       getdate(&cuda);
       year=cuda.da_year;
       month=cuda.da_mon;
      }
      do
      {
       calender(year,month);
       gotoxy(1,24);
       printf("Press N for Next month,B for Previous month,Esc for Exit.");
       printf("\n  X for Next year,Z for Previous year.");
       ch=getch();
       if(ch==27)
        break;
       if(ch=='n'|| ch=='N')
       {
        if(month>=1 && month<12)
         month++;
        else
        {
         year++;
         month=1;
        }
       }
       if(ch=='b'||ch=='B')
       {
        if(month>1  && month<=12)
          month--;
        else
        {
         if(year!=1)
         {
          year--;
          month=12;
         }
        }
      }
      if(ch=='x'||ch=='X')
        year=year+1;
      if(ch=='z'||ch=='Z')
        if(year!=1)
          year=year-1;
     } while(1);
     clrscr();
     _setcursortype(2);
     }

    Friday, April 13, 2007 5:00 PM

  • Code Snippet

    /*
    [8]    To check whether the given number is divisible by 5 or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        if(no%5==0)
            printf("%d is perfectly divisible by 5",no);
        else
            printf("%d is not perfectly divisible by 5",no);
        getch();
    }
    Sunday, April 29, 2007 5:01 PM

  • Code Snippet

    /*
    [7]    To determine whether the given year is a leap year or not.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int year;
        clrscr();
        printf("Enter an year to check whether its leap year or not : ");
        scanf("%d",&year);
        if ((year%4==0 && year%100!=0) || year%400==0)
            printf("Year %d is a leap year",year);
        else
            printf("Year %d is not a leap year",year);
        getch();
    }
    Sunday, April 29, 2007 5:01 PM

  • Code Snippet

    /*
    [6]    To check  whether the given character is an uppercase letter
        or lowercase letter or a digit or a special character.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        char ch;
        clrscr();
        printf("Enter a character : ");
        ch = getche();
        if(ch>='a' && ch<='z')
            printf("\n\'%c\' is a Lower Case Character",ch);
        else if(ch>='A' && ch<='Z')
            printf("\n\'%c\' is a Upper Case Character",ch);
        else if(ch>='0' && ch<='9')
            printf("\n\'%c\' is a digit",ch);
        else
            printf("\n\'%c\' is a Special Character",ch);
        getch();


    }
    Sunday, April 29, 2007 5:02 PM

  • Code Snippet

    /*
    [15]    SUM = 1^2+2^2+3^2+4^2+5^2+6^2+7^2+.. and so on
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int n,i,sum=0;
        clrscr();
        printf("Enter the limit : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++)
            sum+=(i*i);
        printf("Ans = %d",sum);
        getch();


    }
    Sunday, April 29, 2007 5:02 PM

  • Code Snippet

    /*
    [14]    SUM = 1 + 4 - 9 + 16 - 25 + 36 - _ and so on
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int n,i,sum=0;
        clrscr();
        printf("Enter the limit : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++)
        {
            if(i==1) sum+=(i*i);
            else if(i%2==0) sum+=(i*i);
            else sum-=(i*i);
        }
        printf("Ans = %d",sum);
        getch();


    }
    Sunday, April 29, 2007 5:02 PM

  • Code Snippet

    /*
    [13] Write a program to do the following operations :
          -    Read any two positive integer operands (say op1 & op2) and
            one character type operator (say opr).
            Note that opr is any mathematical operator.
          -    Depending upon the operator, do the appropriate operation.
            e. g. if opr is '+' then the display the value
            obtained by evaluating the expression (op1 + op2).
    */

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int n1,n2;
        float ans=0.0F;
        char op=0;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n1);
        printf("Enter another number : ");
        scanf("%d",&n2);
        printf("Enter a Operator to per form calculation between %d and %d :\n",n1,n2);
        printf("[%,/,*,+,-] : ");
        fflush(stdin);
        op=getchar();
        if (op=='%')    ans = n1 % n2;
        else if (op=='*')   ans = (float)n1 * n2;
        else if (op=='/')
        {
            if (n2==0)
            {
                printf("Division not possible because Demominator is ZERO\n");
                getch();
                exit();
            }
            ans = (float)n1 / n2;
        }
        else if (op=='+') ans = (float)n1 + n2;
        else if (op=='-') ans = (float)n1 - n2;
        else    printf("Invalid Operator");
        printf("%d %c %d = %.2f",n1,op,n2,ans);
        getch();


    }
    Sunday, April 29, 2007 5:03 PM

  • Code Snippet

    /*
    [12]A function f is defined as follows :
        f(x)=ax3 - bx2 + cx -d,if x > k
        =0,if x = k
        =-ax3 + bx2 - cx +d,if x < k
    Write a program that reads a, b, c, d, k and x and prints the value of f(x).
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int a,b,c,d,k,x,ans=0;
        clrscr();
        printf("Enter a : ");    scanf("%d",&a);
        printf("Enter b : ");    scanf("%d",&b);
        printf("Enter c : ");    scanf("%d",&c);
        printf("Enter d : ");    scanf("%d",&d);
        printf("Enter k : ");    scanf("%d",&k);
        printf("Enter x : ");    scanf("%d",&x);
        if(x>k) ans=(a*x*x*x) - (b*x*x) + (c*x) - d;
        else if(x<k) ans = -(a*x*x*x) + (b*x*x) - (c*x) + d;
        else  ans = 0;
        printf("Ans : f(%d) = %d",x,ans);
        getch();
    }
    Sunday, April 29, 2007 5:03 PM

  • Code Snippet

    /*
    [11]    An electronic component vendor supplies three products :
      transistors, resistors and capacitors. The vendor gives a discount
      of 10% on orders for transistors if the order is for more than Rs.1000.
      On orders of more than Rs. 100 for resistors, a discount of 5% is given,
      and a discount of 10% is given on orders for capacitors of value more
      than Rs. 500. Assume that the numeric codes 1,2 and 3 are used for
      transistors, capacitors and resistors respectively. Write a program
      that reads the product code and the order amount and prints out the
      net amount that the customer is required to pay after discount.
    */

    #include<stdio.h>
    #include<conio.h>

    #define N 20
    void main()
    {
        int pid[N]={0},uprice[N]={0}, qty[N]={0},i=0,n;
        float disc[N]={0.0},netamt[N]={0.0},biltot=0;
        char cname[25]="Harshil",pname[N][15]={0},ch;
        clrscr();
        printf("Customer Name : ");
        gets(cname);
        for(i=0 ; i<N ; i++)
        {
        againpid:
            clrscr();
            printf("Enter Product Id : \n1.  Transistor\n2.  Capacitor\n3.  Register\n[ ]\b\b");
            scanf("%d",&pid[i]);
            printf("Enter Quantity : ");
            scanf("%d",&qty[i]);
            if(pid[i]==3)
            {
                strcpy(pname[i],"Register");
                uprice[i] = 30;
                if(qty[i]*uprice[i] > 100) disc[i] = 5.0;
                else disc[i]=0.0;
                netamt[i] -= (float)(qty[i] * uprice[i]) * (float)disc[i]/100;
                netamt[i] += (float)(qty[i] * uprice[i]);
                biltot += netamt[i];
            }
            else if(pid[i]==1)
            {
                strcpy(pname[i],"Transistor");
                uprice[i] = 10;
                if(qty[i]*uprice[i] > 1000) disc[i] =10.0;
                else disc[i]=0.0;
                netamt[i] -= (float)(qty[i] * uprice[i]) * (float)disc[i]/100;
                netamt[i] += (float)(qty[i] * uprice[i]);
                biltot += netamt[i];
            }
            else if(pid[i]==2)
            {
                strcpy(pname[i],"Capacitor");
                uprice[i] = 20;
                if(qty[i]*uprice[i] > 500) disc[i] =10.0;
                else disc[i]=0.0;
                netamt[i] -= (float)(qty[i] * uprice[i]) * (float)disc[i]/100;
                netamt[i] += (float)(qty[i] * uprice[i]);
                biltot += netamt[i];
            }
            else
            {
                printf("\nProduct ID entered incorrect..\n");
                netamt[i]=0;
                goto againpid;
            }
            if(i!=N-1)
            {
                printf("Do you want to enter another item? [Y/N]");
                fflush(stdin);
                ch=getchar();
                if(ch=='Y' || ch=='y'){}
                else
                    break;
            }
        }
        n=i;
        clrscr();
        printf("Customer Name : %s\n",cname);
        printf("=======================================================\n");
        printf("PID\tProd. Name\tQty.\tU.Price\t Disc%%\tNet Amt.\n");
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        for(i=0 ; i<=n ; i++)
            printf("%d\t%s\t%d\t%d\t%5.2f%%\t%7.2f\n",pid[i],pname[i],qty[i],uprice[i],disc[i],netamt[i]);
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("Item Count : %d\t\t\t\tTotal:\t%7.2f\n",n+1,biltot);
        printf("=======================================================\n");
        getch();
    }
    Sunday, April 29, 2007 5:03 PM
  • Code Snippet

    */
    /*
    [10]    Write a program that reads three positive numbers a, b, c and
        determines whether they can form the three sides of a triangle.
        If yes, determine whether the triangle will be an obtuse-angle,
        or a right-angle or an acute-angle triangle. If the triangle is
        an acute angle triangle, determine further whether the triangle
        is equilateral, isosceles or scalene.
    */

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int a,b,c,AB,BC,AC,cnt=0;
        clrscr();
        printf("Enter side 'a' of ABC : ");
        scanf("%d",&a);
        printf("Enter side 'b' of ABC : ");
        scanf("%d",&b);
        printf("Enter side 'c' of ABC : ");
        scanf("%d",&c);
        AB = c;
        BC = a;
        AC = b;
        printf("AB(c) = %d\n",c);
        printf("BC(a) = %d\n",a);
        printf("AC(b) = %d\n",b);
        if((AC*AC) == (BC*BC)+(AB*AB) )
            printf("ABC(%d,%d,%d) is a Right Angled triangle\n",a,b,c);
        if( (AC*AC) > (AB*AB)+(BC*BC) )
            printf("ABC(%d,%d,%d) is a Obtuse Angled triangle\n",a,b,c);
        if( (AC*AC) < (AB*AB)+(BC*BC) ) //Triangle is acute so determine whether
        {                    // it is Equilateral, isoceles or scalene.
            printf("ABC(%d,%d,%d) is a Acute Angled Triangle\n",a,b,c);
            if(AB==BC) cnt++;
            if(AB==AC) cnt++;
            if(BC==AC) cnt++;
            if(cnt==3) printf("ABC(%d,%d,%d) is an Equilateral Triangle\n",a,b,c); //all sides are same
            else if(cnt==1) printf("ABC(%d,%d,%d) is an Isoceles Triangle\n",a,b,c); //1pair is equal (which means that 2 sides are same)
            else printf("ABC(%d,%d,%d) is a scalene Triangle\n",a,b,c);
        }
        getch();
    }


    Sunday, April 29, 2007 5:04 PM

  • Code Snippet

    /*
    [9]    To find maximum number out of given three numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int a,b,c;
        clrscr();
        printf("Enter no1 : ");
        scanf("%d",&a);
        printf("Enter no2 : ");
        scanf("%d",&b);
        printf("Enter no3 : ");
        scanf("%d",&c);

        printf("a =  %d\nb =  %d\nc =  %d\n\n",a,b,c);
        if(a>b)
        {
            if(a>c)
                printf("a (%d) is the largest number",a);
            else
                printf("c (%d) is the largest number",c);
        }
        else
        {
            if(b<c)
                printf("c (%d) is the largest nubmer",c);
            else
                printf("b (%d) is the largest number",b);
        }

        getch();
    }
    Sunday, April 29, 2007 5:04 PM

  • Code Snippet

    /*
    [17]    SUM = e1 +e2 +e3 +e4 +e5+_ and so on
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int i,n;
        float ans=0.0F;
        clrscr();
        printf("Enter the limit : ");
        scanf("%ld",&n);
        for(i=1 ; i<=n ; ans+=exp(i),i++);
        printf("Ans = %f",ans);
        getch();

    }
    Sunday, April 29, 2007 5:05 PM

  • Code Snippet

    /*
    [16]    SUM = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! -_ and so on
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int n, i,flag=0,x=5;
        int powa=1, fact=1;
        float ans=0.0F,sum=0.0F;
        clrscr();
        printf("Enter the limit : ");
        scanf("%ld",&n);
        for(i=1 ; i<=n*2 ; i++)
        {
            powa=powa*x;
            fact=fact*i;
            if(i%2==1)
            {
                ans=(float)powa/fact;
                if(flag==0)
                {
                    sum+=ans;
    //                printf("+%d\t%d\t%.6f\n",powa,fact,ans);
                    flag=1;
                }
                else
                {
                    sum-=ans;
    //                printf("-%d\t%d\t%.6f\n",powa,fact,ans);
                    flag=0;
                }
            }
        }
        printf("Ans = %.2f",sum);
        getch();

    }
    Sunday, April 29, 2007 5:05 PM

  • Code Snippet

    /*
    [20]    SUM = 1 + 8 + 27 + 64 + _ and so on
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int i,n,sq;
        long int ans = 0;
        clrscr();
        printf("Enter the limit : ");
        scanf("%ld",&n);
        for(i=1 ; i<=n ; ans+=(i*i*i),i++);
        printf("Ans = %ld",ans);
        getch();

    }
    Sunday, April 29, 2007 5:05 PM

  • Code Snippet

    /*
    [19]    SUM = 1 + 1/4 + 1/9 + 1/16 + 1/25 + and so on
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int i,n,sq;
        float ans = 0.0F;
        clrscr();
        printf("Enter the limit : ");
        scanf("%ld",&n);
        for(i=1 ; i<=n ; ans+=1.0F/(i*i),i++);
        printf("Ans = %f",ans);
        getch();

    }
    Sunday, April 29, 2007 5:06 PM

  • Code Snippet

    /*
    [18]    SUM = 1 + 2 + 6 + 24 + 120 + _ and so on
    */
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int i,n;
        long int fact=1,ans=0;
        clrscr();
        printf("Enter the limit : ");
        scanf("%ld",&n);
        for(i=1 ; i<=n ; fact*=i,ans+=fact,i++);
        printf("Ans = %ld",ans);
        getch();

    }
    Sunday, April 29, 2007 5:06 PM

  • Code Snippet

    /*
    [21]    To print multiplication table from 1x1 to 10x10.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int i,j;
        clrscr();
        for(printf("\n\n"),i=1 ; i<=10 ; i++)
            for(j=1 ; j<=5 ; j++)
                printf("%2dx%2d = %3d\t",j,i,i*j);
        for(printf("\n"),i=1 ; i<=10 ; i++)
            for(j=6 ; j<=10 ; j++)
                printf("%2dx%2d = %3d\t",j,i,i*j);
        getch();
    }

    Sunday, April 29, 2007 5:07 PM

  • Code Snippet

    /*
    [22]    To compute the sum of the digits of a given positive integer number.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tno;
        int ans=0;
        clrscr();
        do
        {
            printf("Enter a number : ");
            scanf("%ld",&no);
        }while(no<0);
        tno = no;
        while(no>0)
        {
            ans = ans + (no%10);
            no/=10;
        }
        printf("Sum of the digits of %d is %d",tno,ans);
        getch();
    }
    Sunday, April 29, 2007 5:07 PM

  • Code Snippet

    /*
    [30] Write a program to print the pattern :
                        1
                      2 2
                    3 3 3
                  4 4 4 4
                5 5 5 5 5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2d",i);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:07 PM

  • Code Snippet

    /*
    [23]    To read any five real numbers and print the average value.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,tot=0,i;
        float avg=0.0;
        clrscr();
        for(i=0 ; i<5 ; i++)
        {
            printf("Enter a number : ");
            scanf("%d",&no);
            tot+=no;
        }
        avg=(float)tot/i;
        printf("Average : %.2f",avg);
        getch();
    }
    Sunday, April 29, 2007 5:08 PM

  • Code Snippet

    /*
    [24]    To calculate the sum of first  N natural numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,sum=0,i;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        for(i=1 ; i<=no ; sum+=i,i++);
        printf("Sum of first %d natural numbers is : %d",no,sum);
        getch();
    }

    Sunday, April 29, 2007 5:08 PM
  • Code Snippet

    /*
    [25]    To calculate the average of first N odd numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,sum=0,i;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        for(i=1 ; i<=no ; i++)
            sum+= (i%2==1)?i:0;

        printf("Sum of first %d odd natural numbers is : %d",no,sum);
        getch();
    }


    Sunday, April 29, 2007 5:08 PM

  • Code Snippet

    /*
    [26]    To calculate the average of first N even numbers.
    */
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int no,sum=0,i;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&no);
        for(i=1 ; i<=no ; i++)
            sum+= (i%2==0)?i:0;

        printf("Sum of first %d odd natural numbers is : %d",no,sum);
        getch();
    }

    Sunday, April 29, 2007 5:09 PM

  • Code Snippet

    /*
    [27] Write a program to print the pattern :
                1    
                1 2   
                1 2 3  
                1 2 3 4
                1 2 3 4 5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%4d",j);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:09 PM

  • Code Snippet

    /*
    [28] Write a program to print the pattern :
                    1
                  2 1
                3 2 1
              4 3 2 1
            5 4 3 2 1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--*2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%d ",i-j+1);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:09 PM

  • Code Snippet

    /*
    [29] Write a program to print the pattern :
                1
                2 2
                3 3 3
                4 4 4 4
                5 5 5 5 5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%4d",i);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:10 PM

  • Code Snippet

    /*
    [31] Write a program to print the pattern :
                        1
                      2   2
                    3   3   3
                  4   4   4   4
                5   5   5   5   5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%4d",i);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:10 PM

  • Code Snippet

    /*
    [32] Write a program to print the pattern :
                        1
                      1   2
                    1   2   3
                  1   2   3   4
                1   2   3   4   5

    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%4d",j);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:10 PM

  • Code Snippet

    /*
    [33] Write a program to print the pattern :
                5 5 5 5 5
                4 4 4 4
                3 3 3
                2 2
                1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",i);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:11 PM

  • Code Snippet

    /*
    [34] Write a program to print the pattern :
                        a
                      b b
                    c c c
                  d d d d
                e e e e e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",i+'a'-1);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:11 PM

  • Code Snippet

    /*
    [35] Write a program to print the pattern :
                5 4 3 2 1
                5 4 3 2
                5 4 3
                5 4
                5
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=n;
        for(i=n ; i>=1 ; i--, printf("\n")) {
    //        printf("%*c",sp--*3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",n-j+1);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:11 PM

  • Code Snippet

    /*
    [36] Write a program to print the pattern :
                1 2 3 4 5
                  1 2 3 4
                    1 2 3
                      1 2
                        1
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            printf("%*c",sp+=3,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%3d",j);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:11 PM

  • Code Snippet

    /*
    [37] Write a program to print the pattern :
                A B C D E
                A B C D
                A B C
                A B
                A
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for (ch='A', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:12 PM

  • Code Snippet

    /*
    [38] Write a program to print the pattern :
                a b c d e
                  a b c d
                    a b c
                      a b
                        a
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp;
        char ch = 'a';
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        sp=0;
        for(i=n ; i>=1 ; i--, printf("\n")) {
            for(j=0 ; j<n-i ; j++)    {
                printf("  ");
            }
            for (ch='a', j=1 ; j<=i ; j++)    {
                printf("%2c",ch++);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:12 PM

  • Code Snippet

    /*
    [39] Write a program to print the pattern :
                        a
                      a   b
                    a   b   c
                  a   b   c   d
                a   b   c   d   e
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp--,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%2c",j+'a'-1);
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:12 PM

  • Code Snippet

    /*
    [40] Write a program to print the pattern :
                        *
                      *   *
                    *   *   *
                  *   *   *   *   
                *   *   *   *   *
    */
    #include<stdio.h>
    #include<stdio.h>

    void main()
    {
        int i,j,n,sp=40;
        clrscr();
        printf("Enter a number : ");
        scanf("%d",&n);
        for(i=1 ; i<=n ; i++, printf("\n")) {
            printf("%*c",sp-=2,' ');
            for (j=1 ; j<=i ; j++)
            {
                printf("%4c",'*');
            }
        }
        getch();
    }
    Sunday, April 29, 2007 5:13 PM
  • wap to print the following pattern

     

    *****************************

    *                                        *

    *                                        *

    *                                        *

    *                                        *

    *                                        *

    *                                        *

    *****************************

     

    the code is simple,

    lazy to write the whole code, and i dont have a readymadecode, inorder to copy-paste

    Friday, May 4, 2007 10:07 PM

  • Here is the solution to your problem m8, ill only write the code snippet here.


    void main() {

        int i,j,n=10;
        for(i=1 ; i<=n ; i++) {
           for(j=1 ; j<=n ; j++) {
              if(  i==1 || i==n || j==1 || j==n )
                    printf("* ");
              else
                    printf("  ");
           }
           printf("\n");
        }

       

    }


    Saturday, May 5, 2007 4:21 AM
  • exactly harshil, actually in the whole code, the main part is the follwoing statement

    Code Snippet
    if(  i==1 || i==n || j==1 || j==n )

     


    and if you just keep on modifying this particular line, you can get various different patterns, like diagonal, left line missing, right line missing, etc....

    Saturday, May 5, 2007 8:20 PM
  • here is the line to print the popular pattern SAATHIYA


    if( (i==1 && j>=n/2) || j==n/2 || (i==n && j<=n/2) ||

       (j==1 && i<=n/2) || i==n/2 || (j==n && i>=n/2) )



    Sunday, May 6, 2007 6:46 AM
  •  Harshil_Patel_03b5f2 wrote:
    here is the line to print the popular pattern SAATHIYA


    if( (i==1 && j>=n/2) || j==n/2 || (i==n && j<=n/2) ||

       (j==1 && i<=n/2) || i==n/2 || (j==n && i>=n/2) )



    1st sem ki yaadien taaza ho gayi :-)
    Sunday, May 6, 2007 9:57 PM
  • Mujhe to first year BCA ki yaadien tazaa ho gaye Stick out tongue
    Monday, May 7, 2007 9:30 AM
  • i require source code for the following program?

     

    5 4 3 2 1

    5 4 3 2

    5 4 3

    5 4

    5

     

     

    and

     

    *

    * *

    * * *

    * * * *

    * * * * *

     

     

    thanks in advance !!

    Rajeev Bajoria

    Tuesday, May 22, 2007 10:15 AM
  • no more definitions anyone?
    Tuesday, May 22, 2007 1:17 PM
  • Ill post more if its requested. Cause if no one is going to read the programs, why should i waste my time.
    Tuesday, May 22, 2007 2:23 PM
  • i dont know about others, but i was reading the programs....
    Wednesday, May 23, 2007 1:18 PM
  • Ill try posting more questions soon . good to see someone reading the posts.
    Wednesday, May 23, 2007 1:35 PM
  • Sure harshil.....

    Why not start a part two of this thread?...its already 6 pages long, difficult to navigate and secondly, if you start a new thread, it will attract others to post in a fresh thread as well...

    Wednesday, May 23, 2007 2:18 PM
  • Ok guys, as the starter of the thread "Mahesh Pillai" isn't taking any interest in this thread, i think ill take the opportunity to continue this thread in a different thread as the pages in this one has increased and being hard to navigate.

    Here is the continuation of this thread guys.

    Gimme C/C++ Programs Definitions - Part-II


    Wednesday, May 23, 2007 4:34 PM