Pages

Write a program to get row number from user and print diamond of given rows

Saturday, 4 May 2013


#include <stdio.h>
#include <conio.h>
int main()
{
    int i, j, k, r;
    printf("Enter number of rows: ");
    scanf("%d", &r);
    for(i=1; i<=r; i+=2)
    {
          for(j=r-2; j>=i; j-=2)
          {
               printf(" ");  
          }
          for(k=1; k<=i; k++)
          {
               printf("*");  
          }
          printf("\n");
    }
    for(i=r-2; i>=1; i-=2)
    {
          for(j=r-1; j>=i; j-=2)
          {
               printf(" ");  
          }
          for(k=1; k<=i-1; k++)
          {
               printf("*");  
          }
          printf("\n");
    }
    getch();
}

Write a program to get numbers from user and sort is print on screen

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10], i, temp;
    printf("Enter Numbers to sort: ");
    for(i=0; i<=9; i++)
    {
         scanf("%d", &a[i]);   
    }
    for(i=0; i<=9; i++)
    {
             if(a[i+1]<a[i])
             {
                    temp=a[i];
                    a[i]=a[i+1];
                    a[i+1]=temp;
                    i=-1;
             }
    }
   

    for(i=0; i<=9; i++)
    {
        printf("%d ", a[i]);    
    }
    getch();
}

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 rupees toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected. Model this tollbooth with a class called toll Booth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payingCar()increments the car total and adds 50 to the cash total. Another function, called nopayCar(), increments the car total but adds nothing to the cash total. Finally, a member function called display() displays the two totals. [Updated]

Friday, 3 May 2013

#include <iostream.h>
class Tollboth   //class declaretion
{
      //attributes
      int cars;
      double collect;
      public:
             //contstructor
      Tollboth()
      {
           cars=0;
           collect=0;
      }
      //methods
      void payingcar() //paying car method
      {
           cars++;
           collect+=50;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void nopayingcar() //nonpaying  car method
      {
           cars++;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void display() //methods to display
      {
           cout<<"Total numbers of cars: "<<cars<<endl;
           cout<<"Total payment: "<<collect<<endl;
      }
};
main()
{
      int ask;
      char ag;
      Tollboth a; // create objects
      again:   //lable
      cout<<"Press 1 if payment is delievered by car owner.\n";
      cout<<"Press 2 if payment is not delievered by car owner.\nPress 3 to display total payment and cars.\nPress 1 or 2 or 3: "<<endl;
      //ask paying cara or nonpaying car.
      cin>>ask;
      if(ask==1)
      {
            a.payingcar();
      }
      else if(ask==2)
      {
            a.nopayingcar();
      }
      else if(ask==3)
      {
           a.display();
      }
      else
      {
           cout<<"Enter Valid Data."<<endl;
           goto again;
      }
      //ask to calculation again.
      cout<<"You like to enter data again? Y/N ";
      cin>>ag;
      if(ag=='Y' || ag=='y')
      {
           goto again; //goto lable again at above.
      }
}

Create a class Rectangle with attributes length and width, each of which Defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. [Updated]

#include <iostream.h>
class area //declare class
{
      float width, lenght, areaofrect; //attributes
      public:
      area() //construcor
      {
          width=1;
          lenght=1;
          areaofrect=0;
      }
      //functions
      void setwidth() //function to set width
      {
           again:
           cout<<"Enter width of rectangle: ";
           cin>>width;
           if(width<0 || width>20)
           {
                  cout<<"You cannot enter width greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void setlenght() //function to set lenght
      {
           again:
           cout<<"Enter lenght of rectangle: ";
           cin>>lenght;
           if(lenght<0 || lenght>20)
           {
                  cout<<"You cannot enter lenght greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void calculation() //function to calculate area of rectangle
      {
           areaofrect=width*lenght; //farmula of area of rectangle
      }
      void display() //function to display result
      {
           cout<<"Area of rectangle according to given parameters is: "<<areaofrect<<endl;
      }
    
};
main()
{
      again:  //lable
      area c; //create object
      char ask;
      //cla function of class area
      c.setwidth();
      c.setlenght();
      c.calculation();
      c.display();
      // ask from user to calculate again.
      cout<<"You want to calculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask== 'y')
      {
            goto again;
      }
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *