Pages

Write a program to subtract general order matrixes in c language.

Wednesday 24 July 2013

#include <iostream>
using namespace std;

int main()
{
    int a1[100][100], a2[100][100], sum[100][100], m, n, r, c;  
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of columns: ";
    cin>>n;
    cout<<"Enter elements of 1st metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a1[r][c];
          }
    }
    cout<<"\nEnter elements of 2nd metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a2[r][c];
          }
    }
    cout<<"\nElements of metrix 1: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a1[r][c]<<"\t";
          }
          cout<<endl;
    }
    cout<<"\nElements of matrix 2: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a2[r][c]<<"\t";
          }
          cout<<endl;
    }
    //Subtraction
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   sum[r][c]=a1[r][c]-a2[r][c];
          }
          cout<<endl;
    }
    cout<<"Subtraction of matrix is: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<sum[r][c]<<"\t";
          }
          cout<<endl;
    }
    system("pause");
}

Write a program to add general matrix in C language.

#include <iostream>
using namespace std;

int main()
{
    int a1[100][100], a2[100][100], sum[100][100], m, n, r, c;  
    cout<<"Enter number of rows: ";
    cin>>m;
    cout<<"Enter number of columns: ";
    cin>>n;
    cout<<"Enter elements of 1st metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a1[r][c];
          }
    }
    cout<<"\nEnter elements of 2nd metrix: ";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cin>>a2[r][c];
          }
    }
    cout<<"\nElements of metrix 1: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a1[r][c]<<"\t";
          }
          cout<<endl;
    }
    cout<<"\nElements of matrix 2: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<a2[r][c]<<"\t";
          }
          cout<<endl;
    }
    //addition
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   sum[r][c]=a1[r][c]+a2[r][c];
          }
          cout<<endl;
    }
    cout<<"Addition of matrix is: \n";
    for(r=0; r<m; r++)
    {
          for(c=0; c<n; c++)
          {
                   cout<<sum[r][c]<<"\t";
          }
          cout<<endl;
    }
    system("pause");
}

Write A Program To Find The Power Of Number

Wednesday 17 July 2013

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

{
  int p,n,l=1;
 int s=1;

  printf("\nEnter number = ");
  scanf("%d",&n);

  printf("\nEnter  power =");
  scanf("%d",&p);

  while(l<=p)
  {
      s=s*n;
      l++;
  }

  printf("\n%d  raise to power %d is= %ld",n,p,s);
  getche();
}

Write a c language to tell if a year is leap year or not.

Tuesday 16 July 2013

Leap Year: A year which is divided on four with 0 remainder is called leap year.

#include <stdio.h>
#include <conio.h>
int main()
{
               int year;
               printf("Enter a year to check if it is a leap year: ");
               scanf("%d", &year);

                if (year%400 == 0)
                      printf ("%d is a leap year.\n", year);
               else if ( year%100 == 0)
                      printf ("%d is not a leap year.\n", year);
               else if ( year%4 == 0 )
                      printf ("%d is a leap year.\n", year);
               else
                      printf ("%d is not a leap year.\n", year);
                getch();
}

Write a program to print on screen without semicolon.

Program 1


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

int main()
{
    if(printf("http://www.compprog.com"))
    {
           
    }
    getch();
}

Program 2


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

int main()
{
    while(!printf("http://www.compprog.com"))
    {
            
    }
    getch();
}

Program 3


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

int main()
{
    switch(printf("http://www.compprog.com"))
    {
            
    }
    getch();
}


Write a program to calculate factorial of given number in c language.

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

int main()
{
   int num, i, sum=1;
   printf("Enter a number: ");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
         sum=sum*i;
   }
   printf("Factorial of %d is %d.", num, sum);
   getch();
}

Write a program in c language to tell that given number is palindromic number or numeral palindrome or not?

palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, …

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

int main()
{
   int num, reverse = 0;
   int temp;

   printf("Enter a number to check if it is a palindrome or not: ");
   scanf("%d",&num);

   temp = num;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
   if ( num == reverse )
      printf("Number %d is a palindrome.\n", num);
   else
      printf("Number %d is not a palindrome.\n", num);
   getch();
}

Write a program in c language to concatenate two strings.

Monday 15 July 2013

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

void stringconcatenation(char[],char[]);

 main()
{

    char mrr1[100],mrr2[100];
    printf("Enter the first string: ");
   gets(mrr1);
    printf("Enter the second string: ");
    gets(mrr2);
    stringconcatenation(mrr1,mrr2);
   printf("The String after concatenation: %s",mrr1);
    getch();
}
void stringconcatenation(char mrr1[],char mrr2[])
{
    int l=0,k=0;
    while(mrr1[l]!='\0')
    {
         l++;
    }
  while(mrr2[k]!='\0')
    {
         mrr1[l] = mrr2[k];
         l++;
         k++;
    }
 mrr1[l] ='\0';
}

Write a c language program to convert decimal to binary using recursion.

#include <stdio.h>
#include <conio.h>
long DecToBin(int); //declaration

int main(){

    long BinNo;
    int DecNo;

    printf("Enter any decimal number: ");
    scanf("%d",&DecNo);

    BinNo = DecToBin(DecNo);
    printf("Binary value is: %ld", BinNo);

    getch();
}
//definition
long DecToBin(int DecNo)
{

    static long BinNo,remainder,factor = 1;

    if(DecNo != 0)
    {

         remainder = DecNo % 2;
         BinNo = BinNo + remainder * factor;
         factor = factor * 10;
         DecToBin(DecNo / 2);
    }

    return BinNo;
}

Write a program in c language to reverse a string using recursion.

#include<stdio.h>
#include <conio.h>
char* reverse(char[]); //function declaration

int main()
{

    char number[100],*rev;

    printf("Enter any string/number: ");
    scanf("%s",number);
    rev = reverse(number);
    printf("Reversed of given string/number is: %s",rev);
    getch();
}
//function definition.
char* reverse(char number[])
{

    static int i=0;
    static char rev[100];

    if(*number){
         reverse(number+1);
         rev[i++] = *number;
    }

    return rev;
}

Mouse pointer with menu in c language.

Friday 12 July 2013


Note: This program is compiled and executed in Turbo 3.0

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


//mouse pointer functions
union REGS in,out;

       int callmouse()
       {
     in.x.ax=1;
     int86(51,&in,&out);
     return 1;
       }
       void mouseposi(int &xpos,int &ypos,int &click)
       {
     in.x.ax=3;
     int86(51,&in,&out);
     click=out.x.bx;
     xpos=out.x.cx;
     ypos=out.x.dx;
}
      void setposi(int &xpos,int &ypos)
      {
    in.x.ax=4;
    in.x.cx=xpos;
    in.x.dx=ypos;
    int86(51,&in,&out);
      }
//end mouse pointer code

int set_command()
{
    int x,y,cl,a,b, change, i;
    a=100;
    b=400;
    setposi(a,b);
    callmouse();
    gotoxy(10, 12);
    //printf("%d", cl);
    do
    {
   change=cl;
   mouseposi(x,y,cl);
   gotoxy(10, 7);
   printf("x=%d  y=%d   click=%d", x, y, cl);
   gotoxy(10,9);
   if(i==26)
   {
printf("Enter New Entry.");
   }
   if(cl!=change)
   {
if((x>=10 && x<=95) && (y>=6 && y<=18))
{
      printf("Goto Setter.     ");
}
if((x>=95 && x<=190) && (y>=6 && y<=18))
{
    printf("Edit Data.       ");
}
if((x>=350 && x<=480) && (y>=6 && y<=18))
{
     printf("Goto Main.       ");
}
if((x>=495 && x<=575) && (y>=6 && y<=18))
{
     printf("Save data in file");
}
if((x>=585 && x<=630) && (y>=6 && y<=18))
{
     return 0;
}
   }
    }while(!kbhit());
}

int main(void)
{
   char command;
   clrscr();
   int gdriver = DETECT, gmode, errorcode;
   clrscr();
   initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");
   setcolor(getmaxcolor());
   settextstyle(0,0,1);
   outtextxy(10,8,"New Entry | Edit Entry                     Goto: Main Menu | Save Data | Quit");

   set_command();
}

Write a program to print time on screen.

Wednesday 10 July 2013

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string>
int main(void)
{
    time_t current_time;
    char* c_time_string, finel_time;

    current_time = time(NULL);
    c_time_string = tm(&current_time);
    (void) printf("%s", c_time_string+4);
    getch();
}

Write a program in c language to get numbers from user and print average.

# include<stdio.h>
#include<conio.h>
#define LIMIT 130
int main(){
float marks[LIMIT], average, sum = 0.0;
int i=0;
do {
printf("\nEnter the mark of the student(-1 to finish)");
scanf("%f", &marks[i]);
if (marks[i] >= 0)
sum = sum + marks[i];
}
while(marks[i++] >= 0);
average = sum/(i-1);
printf ("\nThe average is %.2f\n", average);
getch();
}

Write a program in c language to get three numbers from user and print maximum number.

#include<stdio.h>
#include<conio.h>
int maximum(int,int,int);
int main()
{
    int num1,num2,num3;
    printf("enter hte three integer: ");
    scanf("%d %d %d",&num1,&num2,&num3);
    printf("Max is %d",maximum(num1,num2,num3));
    getch();
}
int maximum(int x,int y,int z)
{
   int max;
       if(x>y && x>z)
       {
              max=x;
       }
       if(y>z && y>x)
       {
              max=z;
       }
       if(z>x && z>y)
       {
              max=z;          
       }
       return max;
}
         
                               
                     
                   


Write a program in c language to print counting using function.

#include<stdio.h>
#include<conio.h>
int mul(int);
int main()
{
    int a=10;
    printf("%d", mul(a));
    getch();
}
int mul(int a)
{
       int x;
       for(x=1;x<=a;x++)
       {
              printf("%d\n",x);
       }

}

Write a program in c language to convert weight in grams from kilograms using function [Updated]

#include<stdio.h>
#include<conio.h>
int grams(int); // function prototype
int main()
{
    int kg,gm;
    printf("Enter the weight in kilogram: ");
    scanf("%d",&kg);
    gm=grams(kg);
    printf("%d Kilograms = %d Grams\n", kg,gm);
    getch();
}
int grams(int weight) //function defination
{
    return weight*1000;
}

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();
   
}

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;
      }
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *