Pages

Write a program to create single link list.

Wednesday 29 May 2013

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *link;
};
struct node *n, *first, *this1;
int main()
{
    char ch;
    first=NULL;
    printf("Enter q to quit: ");
    ch=getche();
    while(ch!='q')
    {
           n=(struct node *)malloc(sizeof(struct node));
           printf("\nEnter data to store in node: ");
           scanf("%d", &n->data);
           n->link=NULL;
           if(first==NULL)
           {
                  first=n;
           }
           else
           {
                  this1=first;
                  while(this1->link!=NULL)
                  {
                          this1=this1->link;
                  }
                  this1->link=n;
           }
           printf("Press q to quit: ");
           ch=getche();
    }
    printf("\n");
    this1=first;
           while(this1!=NULL)
           {
                   printf("%d\n", this1->data);
                   this1=this1->link;
           }
    getch();
   
}

Write a program to multiply two matrix and show result on screen

Tuesday 28 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, j, k, a1[5][5], a2[5][5], a3[5][5], r1, r2, c1, c2, sum;
    printf("Enter number of rows for fisrt metrix: ");
    scanf("%d", &r1);
    printf("Enter number of columns for fisrt metrix: ");
    scanf("%d", &c1);
    printf("Enter number of rows for second metrix: ");
    scanf("%d", &r2);
    printf("Enter number of columns for second metrix: ");
    scanf("%d", &c2);
    if(c1!=r2)
    {
           printf("Multiplication cannot take place, columns of first metrix should be equal to second row.");
    }
    else
    {
        // enter elements in metrix
        for(i=0; i<r1; i++)
                 {
                        for(j=0; j<c1; j++)
                        {
                                 printf("Enter element of row %d and colimn %d: ", i, j);
                                 scanf("%d",&a1[i][j]);
                        }
                 }
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("Enter element of row %d and colimn %d: ", i, j);
                                 scanf("%d", &a2[i][j]);
                        }
                 }
                
        for(i=0; i<r1; i++)
        {
                 for(j=0; j<c2; j++)
                 {
                          sum=0;
                          for(k=0; k<c1; k++)
                          {
                                   sum=sum+a1[i][k]*a2[k][j];
                                   a3[i][j]=sum;
                          }
                 }
        }
             //show elements of metrix.
        printf("\n");
        for(i=0; i<r1; i++)
                 {
                        for(j=0; j<c1; j++)
                        {
                                 printf("%d\t", a1[i][j]);
                        }
                        printf("\n");
                 }
        printf("\n");
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("%d\t", a2[i][j]);
                        }
                        printf("\n");
                 }
        printf("Multiplication of array: \n");
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("%d\t", a3[i][j]);
                        }
                        printf("\n");
                 }
    }
    getch();
}

Write a program in c language to find first and second largest number from array.

Monday 27 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc1=0, loc2=0, max1=0, max2=0;
    for(i=1; i<=5; i++)
    {
             printf("Enter number: ");
             scanf("%d", &a[i]);
    }
    for(i=1; i<=5; i++)
    {
             if(max1<a[i])
             {
                    max2=max1;
                    max1=a[i];
                    loc2=loc1;
                    loc1=i;
             }
             if(max2<a[i] && a[i]<max1)
             {
                     max2=a[i];
                     loc2=i;
             }
    }
    printf("First largest is %d  at %d and Second largest is %d at %d",max1, loc1, max2, loc2);
    getch();
}


Write a C program to implement the following: 1. Take an int array of 100 elements. Populate it using random number generator built-in function. 2. User will be given the following choices: a) To insert a new element in the array. b) To delete an element in the array. c) To search an item in the array. d) To update an item in the array. e) To show the elements of the array. f) To quit from the program.

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
    int i, a[100], loc, item, size=99;
    char option;
    for(i=0; i<=size; i++)
    {
             a[i]= rand() % 20;
    }
    printf("Press a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
    option=getche();
    while(option!='f')
    {
    switch(option)
    {
           case 'a':
                if(size==100)
                {
                             printf("you can never add more elements because array in completlty filled.");
                }
                else
                {
                    printf("\nEnter location where you want to enter number: ");
                    scanf("%d", &loc);
                    printf("\nEnter number: ");
                    scanf("%d", &item);
                    for(i=size+1; i>loc; i--)
                                {
                                         a[i]=a[i-1];
                                }
                   a[loc]=item;
                   size++;
                   break;
                }
    case 'b':
         printf("\nEnter location where you want to delete number: ");
    scanf("%d", &loc);
    for(i=loc; i<=size; i++)
    {
             a[i]=a[i+1];
    }
    size--;
    break;
    case 'c':
         printf("\nEnter number which you want to search from array: ");
    scanf("%d", &item);
    for(i=0; i<=size; i++)
    {
             if(item==a[i])
             {
                         loc=i;
                         break;
             }
    }
    if(i>size)
    {
           printf("\nNumber is not found in array.");
    }
    else
    {
        printf("\n%d is found at location %d", item, loc);
    }
    break;
    case 'd':
         printf("\nEnter location where you want to update number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    a[loc]=item;
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
    case 'e':
    printf("\n");
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
         default:
         printf("\nYou have entered invalid entry.");
}
printf("\nPress a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
option=getche();
}
}

Write a program in c language to insert a new element in array.

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc, item;
    for(i=0; i<=9; i++)
    {
             printf("Enter number %d: ", i);
             scanf("%d", &a[i]);
    }
   
    printf("Enter location where you want to enter number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    for(i=10; i>loc; i--)
    {
             a[i]=a[i-1];
    }
    a[loc]=item;
    for(i=0; i<=10; i++)
    {
             printf("%d ", a[i]);
            // scanf("%d", &a[i]);
    }
    getch();
}

Looping Tutorial in C++ language.

Saturday 18 May 2013

What is Loop?
Ans: Loop is a control structure which is used to repeat one task severel time.
For example we want to print our name 5 time it is easy with cin>> statement but if we want ti print name 1000 time it is very difficult to print with cin>> statement. So for this purpose we use loops.
Types of loops on C++
There are three loops in C++
  1. For Loop.
  2. While Loop.
  3. Do-While Loop.
For Loop: Used when we know starting point and ending point of loop.
Syntax of For Loop.
for(Initialization; Condition; Increment/Decrement)
{
           Body of Loop.
}

Initialization: From where we want to start our loop. for example we want to print counting from 1 to 100, here we start from 1, so our initialization is 1.
Condition: We want to print counting from 1 to 100 then our condition will be that loop process his work until reach to 100.
Increment/Decrement: We want to print counting from 1 to 100. So we make increment of 1 every time we next number will be greater than previous number by 1.
Now we write a program to print counting from 1 to 100.

#include <iostream.h> //header file
int main()  // main function from where program will start.

               int i;  // declare a variable i.
               for(i=1; i<=1; i=i+1)   // loop
               { //start loop body
                              cout<<i; //loop body
                } // end loop body
}

in this program i=1 is initialization and i<= 100 means loop run until i will be greater than 100. when i will be greater than 100 means 101 than loop will be terminated. And i=i+1 mean every time i will increased by 1. We can write i=i+1 like this i++.
cout<<i means print value of i as we learn in previous lectures.

While Loop: Used when don't know starting and ending point we know only condition.

Write a program in C language to find area of rectangle

#include<stdio.h>
#include <conio.h>
  int main()
{

    float length, width;
    float area;

    printf("Enter size of each sides of the rectangle: ");
    scanf("%f %f",&length, &width);
 
    area = length * width;  //calculation to find area of rectangle
    printf("Area of rectangle is: %.3f",area);
 
    getch();
}

Write a program in C language to find area of circle.

#include <stdio.h>
#include <conio.h>
int main()
{
         float radius, area; // declare vaviables
         printf("Enter radius of circle: ");
         scanf("%f", &radius);
         area = 3.1415 * radius * radius; // Formula to find area of circle
         printf("%f\n", area);
         getch();
}

Write a function in C++ to add two numbers using function.

Monday 13 May 2013

#include <iostream>
using namespace::std;
inline
int add(int , int);
int main()
{
    int a, b;
    cin>>a>>b;
    cout<<add(a, b)<<endl;
    system("pause");
}
int add(int a, int b)
{
    return a+b;
}

Write a program to create a class name employee and create three attributes and create a setter and a getter function for each attribute and call a default constructor in C++

#include <iostream>
#include <string>
using namespace::std;
class employee
{
      string first_name;
      string last_name;
      int salary;
      public:
             employee()
             {
                       first_name='NULL';
                       last_name='NULL';
                       salary=0;
             }
             void setFirstName()
             {
                  cout<<"Enter Your First Name: ";
                  cin>>first_name;
             }
             void setlastName()
             {
                  cout<<"Enter Your Last Name: ";
                  cin>>last_name;
             }
             void setSalary()
             {
                  cout<<"Enter Your Salary: ";
                  cin>>salary;
                  if(salary<0)
                  {
                              salary=0;
                  }
             }
             void dsplayFirstName()
             {
                  cout<<"Your First Name is: "<<first_name<<endl;
             }
             void displayLastName()
             {
                  cout<<"Your Last Name is: "<<last_name<<endl;
             }
             void displaySalary()
             {
                  cout<<"Your Salary is: "<<salary<<endl;
             }
};
int main()
{
    employee a1;
    a1.setFirstName();
    a1.setlastName();
    a1.setSalary();
    a1.dsplayFirstName();
    a1.displayLastName();
    a1.displaySalary();
    system("pause");
}

Scope resolution operator in C++

#include <iostream>
using namespace::std;
class date
{
      int day;
      int month;
      int year;
      public:
             int setdate(int, int, int);
             void display()
             {
                  cout<<day<<"/"<<month<<"/"<<year<<endl;
             }
};
int date::setdate(int d, int m, int y)
{
    day=d;
    month=m;
    year=y;
}
int main()
{
    int d, m, y;
    date a1;
    cout<<"Enter Day: "; cin>>d;
    cout<<"Enter Month: "; cin>>m;
    cout<<"Enter Year: "; cin>>y;
    a1.setdate(d, m, y);
    a1.display();
    system("pause");
}

Write A Program To Find A Maximum Number In Array

Wednesday 8 May 2013


#include <stdio.h>
#include <conio.h>
int main()
{
  int array[100], maximum, s, d, l = 1;

  printf("Enter the number of elements in array\n");
  scanf("%d", &s);

  printf("Enter %d integers\n", s);

  for (d = 0; d < s; d++)
    scanf("%d", &array[d]);

  maximum = array[0];

  for (d = 1; d < s; d++)
  {
    if (array[d] > maximum)
    {
       maximum  = array[d];
       l = d+1;
    }
  }

  printf("Maximum element is present at location %d and it's value is %d.\n", l, maximum);
 getch();
}

Write A Program Search In Given Number In Array Using Binary Search

#include <stdio.h>
#include <conio.h>
int main()
{
   int c, f, l, midl, n, s, array[100];

   printf("Enter number of elements: ");
   scanf("%d",&n);

   printf("Enter %d integers: ", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);

   printf("Enter value to find: ");
   scanf("%d",&s);

   f = 0;
   l = n - 1;
   midl = (f+l)/2;

   while( f <= l )
   {
      if ( array[midl] < s )
         f = midl + 1;
      else if ( array[midl] == s )
      {
         printf("%d found at location %d.", s, midl+1);
         break;
      }
      else
         l = midl - 1;

      midl = (f + l)/2;
   }
   if ( f > l )
      printf("Not found! %d is not present in the list.", s);

   getch();
}

Write A Program Search A Given Number In Array Using Linear Search

#include <stdio.h>
#include <conio.h>
int main()
{
   int array[100], s, d, number;

   printf("Enter the number of elements in array: ");
   scanf("%d",&number);

   printf("Enter %d numbers: ", number);

   for ( d = 0 ; d < number ; d++ )
      scanf("%d",&array[d]);

   printf("Enter the number to search: ");
   scanf("%d",&s);

   for ( d = 0 ; d < number ; d++ )
   {
      if ( array[d] == s )  
      {
         printf("%d is present at location %d.", s, d+1);
break;
      }
   }
   if ( d == number )
      printf("%d is not present in array.", s);  

   getch();
}

Write a program to swap numbers using two variables.

#include <stdio.h>
#include <conio.h>
main()
{
      int a, b;
      printf("Enter To Numbers: ");
      scanf("%d%d", &a, &b);
      //swaping
      a=a+b;
      b=a-b;
      a=a-b;
      printf("%d %d", a, b);
      getch();
}

Write a program to print ASCCI code for given character.

#include<stdio.h>
#include<conio.h>
int main()
{
   char ch;
   printf("Enter a chararcter: ");
   ch=getche();

   printf("\nThe ASCII Code for \%c is %d",ch,ch);
   getch();
}

Write A Program To Swap A Two Number With Each Other


#include <stdio.h>
#include <conio.h> 
int main()
{
   int x, y, temporay;
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temporay = x;
   x    = y;
   y    = temporay;
   printf("After Swapping\nx = %d\ny = %d\n",x,y);

 getch();
}

Write a program to calculate volum and area of sphere.

#include<stdio.h>
#include<conio.h>
int main()
{
    float r,v,area;
    printf("Enter the radius of the sphere\n ");
    scanf("%f", &r);
    area=4*3.14*r*r*r;
    v=(4/3.14)*3.14*r*r*r;
    printf("volume of the sphere=%f\n", v);
    printf("area of sphere=%f\n", area);
    getch();
    }

Write a program to add and subtract complex equations.

#include <iostream.h>
class complex //create class of name complex
{
      double real1, imaginary1, real2, imaginary2, sreal, simaginary; //declare attributes
      public:
      complex() //constructor
      {
               real1=0;
               imaginary1=0;
               real2=0;
               imaginary2=0;
               sreal=0;
               simaginary=0;
      }
      //create functions
      void setvalues() //function to set values of equations
      {
           cout<<"Enter real number of first eqaution: ";
           cin>>real1;
           cout<<"Enter imaginary number of first eqaution: ";
           cin>>imaginary1;
           cout<<"Enter real number of 2nd eqaution: ";
           cin>>real2;
           cout<<"Enter imaginary number of 2nd eqaution: ";
           cin>>imaginary2;
      }
      void add()//function to add two complex equations
      {
           sreal=real1+real2;
           simaginary=imaginary1+imaginary2;
      }
      void sub()//function to subtract two complex equations
      {
           sreal=real1-real2;
           simaginary=imaginary1-imaginary2;
      }
      void answer() //dispaly answer on the screen
      {
           cout<<"{("<<sreal<<")"<<"+"<<"("<<simaginary<<")i}"<<endl;
      }
};
main()
{
      again: // lable for calculation again
      complex a; //create objects
      int b;
      char ask;
      a.setvalues(); //call function to set values of funtions.
      cout<<"Press 1 to add\nPress 2 to subtract\n";
      cin>>b;
      //ask from user wheather equations add or subtract.
      if(b==1)
      {
              a.add();
      }
      else if(b==2)
      {
           a.sub();
      }
      else
      {
          cout<<"Enter valid number.";
          goto again;
      }
      a.answer(); //call function to display or subtract
      //ask from user that he want to calculate again?
      cout<<"You want to claculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask=='y')
      {
           goto again;
      }
}

Write a program in C language for bubble sorting in decending order.

Tuesday 7 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
  int s,temp,i,j,array[20];

  printf("Enter total numbers of elements: ");
  scanf("%d",&s);
  for(i=0;i<s;i++)
  {
      printf("Enter elements %d: ",i);
      scanf("%d",&array[i]);
  }
     
  for(i=s-2;i>=0;i--)
  {
      for(j=0;j<=i;j++)
      {
           if(array[j]<array[j+1])
           {
              temp=array[j];
              array[j]=array[j+1];
              array[j+1]=temp;
           }
      }
  }

  printf("After sorting: ");
 
  for(i=0;i<s;i++)
  {
      printf(" %d",array[i]);
  }

  getch();
}

Write a program in C language for bubble sorting in ascending order.

#include <stdio.h>
#include <conio.h>
int main()
{
  int s,temp,i,j,array[20];

  printf("Enter total numbers of elements: ");
  scanf("%d",&s);

  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
  {
      scanf("%d",&array[i]);
  }
     
  for(i=s-2;i>=0;i--)
  {
      for(j=0;j<=i;j++)
      {
           if(array[j]>array[j+1])
           {
              temp=array[j];
              array[j]=array[j+1];
              array[j+1]=temp;
           }
      }
  }

  printf("After sorting: ");
 
  for(i=0;i<s;i++)
  {
      printf(" %d",array[i]);
  }

  getch();
}

Code of Game in C++

Saturday 4 May 2013


Click Here To Download Compiled Game

Write a program to get 9 numbers from user in two dimensional array and print it as a matrix.


#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3];
int i,j;
printf("value in a case\n");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
      printf("Enter Value of row %d and column %d: ", i, j);          
      scanf("%d", &a[i][j]);
}
}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
}

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


#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 *