Pages

Write program to store data in file object oriented approch.

Tuesday 25 June 2013

#include <iostream>
#include <string.h>
#include <fstream>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void save()// dada save in file
      {
           ofstream outfile;  // create object of of class
           outfile.open("abs.txt");  //open file inwhich data will be stored
           outfile<<"Your name is "<<name<<"\nYour age is "<<age<<endl;  // data is storing in file.
           cout<<"saved\n";
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.save();
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Create a class of student, get student name and age and print on screen using setter and getter function.

#include <iostream>
#include <string.h>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Write a program to print counting from 1 to 10 using for loop

#include <stdio.h>
#include <conio.h>
int main()
{
    int x;
    // The loop goes while x < 10, and x increases by one every loop
    for ( x = 0; x < 10; x++ ) {
        /* Keep in mind that the loop condition checks 
           the conditional statement before it loops again.
           consequently, when x equals 10 the loop breaks.
           x is updated before the condition is checked. */   
        printf( "%d\n", x );
    }
    getch();
}

write a program to print Fibonacci series till given number.

Friday 7 June 2013

#include <stdio.h>
#include <conio.h>
int fibn(int n)
{
      int a, b, c;
      if(n==1 || n==2)
      return(n-1);
      a=fibn(n-1);
      b=fibn(n-2);
      c=a+b;
      return c;
}
int main()
{
    int fib, i;
    printf("Enter ending number where fibanoci series ends: ");
    scanf("%d", &fib);
    for(i=1; i<=fib; i++)
    {
             printf("Fibnocii of %d is %d.\n", i, fibn(i));
    }
    getch();
}

Write a program to print Fibonacci series using recursion.

#include <stdio.h>
#include <conio.h>
int fibn(int n)
{
      int a, b, c;
      if(n==1 || n==2)
      return(n-1);
      a=fibn(n-1);
      b=fibn(n-2);
      c=a+b;
      return c;
}
int main()
{
    int fib;
    printf("Enter number: ");
    scanf("%d", &fib);
    printf("Fibnocii of %d is %d.", fib, fibn(fib));
    getch();
}

Write a program in c language to printf factorial using recursion.

Thursday 6 June 2013

#include <stdio.h>
#include <conio.h>
int fact(int a)
{
       int f;
       if(a==1)
       return 1;
       f=a*fact(a-1);
       return f;
}
int main()
{
    int a, ans;
    printf("Enter number for firctorial: ");
    scanf("%d", &a);
    printf("Fictorial of %d is %d.", a, fact(a));
    getch();
}


Create a double link list which have functionality of insertion, deletion , searching and traversing

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *forw, *backw;
};
struct node *n, *first, *last, *this1, *dlt, *pre;
int main()
{
    char ch;
    int item, position;
    first=last=NULL;
    printf("Enter q to quit: ");
    while((ch=getche())!='q')
    {
          n=(struct node *)malloc(sizeof(struct node));
          printf("\nEnter data in node: ");
          scanf("%d", &n->data);
          n->forw=NULL;
          n->backw=NULL;
          if(first==NULL)
          {
                  first=n;
                  last=n;
          }
          else
          {
                  last->forw=n;
                  n->backw=last;
                  last=n;
          }
          printf("Enter q to quit: ");
    }
    // treversing
    printf("\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    //searching
    printf("Enter number which you want to find in list: ");
    scanf("%d", &item);
    this1=first;
    while(this1!=NULL && item != this1->data)
    {
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.\n");
    else
    printf("Number is found.\n");
    // deletion
    printf("Enter number which you want to delete in list: ");
    scanf("%d", &item);
    this1=first;
if(item==this1->data)
{
            first=this1->forw;
            this1->forw->backw=NULL;
            free(this1);
}
else if(item==last->data)
{
     dlt=last;
     last->backw->forw=NULL;
     free(dlt);
}
else
{
    while(this1!=NULL && item != this1->data)
    {
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
           this1->backw->forw=this1->forw;
           this1->forw->backw=this1->backw;
           free(this1);
    }
}
    printf("\nTreversing after deletion.\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    //insertion--------------
    printf("Enter position where you want to enter element: ");
    scanf("%d", &position);
if(first->data==position)
{
    n=(struct node *)malloc(sizeof(struct node));
    printf("Enter data in new node: ");
    scanf("%d", &n->data);
    n->backw=NULL;
    n->forw=first;
    first=n;
}
    else
{
    this1=first;
    while(this1!=NULL && position!=this1->data)
    {
           pre=this1;
           this1=this1->forw;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
           n=(struct node *)malloc(sizeof(struct node));
           printf("Enter data in new node: ");
           scanf("%d", &n->data);
           n->backw=pre;
           n->forw=pre->forw;
           pre->forw=n;
    }
}
// treversing
    printf("\n");
    this1=first;
    while(this1!=NULL)
    {
           printf("%d\n", this1->data);
           this1=this1->forw;
    }
    getch();
}

Create single link list and insert a new node in link list.

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *link;
};
struct node *n, *first, *this1, *pre;
int main()
{
    char ch;
    int position;
    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 %d\n", this1->data, this1->link);
                   this1=this1->link;
           }
           //insertion
    printf("where you want to enter number: ");
    scanf("%d", &position);
if(first->data==position)
{
             n=(struct node *)malloc(sizeof(struct node));
             printf("Enter number: ");
             scanf("%d", &n->data);
             n->link=first;
             first=n;
}
else
{
    this1=first;
    while(this1!=NULL && position!=this1->data)
    {
             pre=this1;
             this1=this1->link;
    }
    if(this1==NULL)
    printf("Number is not found.");
    else
    {
       
             n=(struct node *)malloc(sizeof(struct node));
             printf("Enter number: ");
             scanf("%d", &n->data);
             n->link=this1;
             pre->link=n;
    }
}
    //treversing after insertion
    this1=first;
           while(this1!=NULL)
           {
                   printf("%d\n", this1->data);
                   this1=this1->link;
           }
    getch();
   
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *