Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it
Showing posts with label HOME. Show all posts
Showing posts with label HOME. Show all posts

17 Oct 2013

C Program to remove all characters except string

Q) Write a C program to remove the characters in String except alphabets.

Sol)
 
#include<stdio.h>

int main()
{
  char line[150];
  int i,j;  

  printf("Enter a string:") ;
  gets(line);

  for(i=0; line[i]!='\0' ; ++i)
   {
      while(!((line[i]>='a' && line[i]<='z') || (line[i]>='A' && line[i]<='Z' || line[i] == '\0')))
           {
                for(j=i; line[j]!='\0';++j)
                        {
                                  line[j] = line[j+1];
                         }
                line[j]='\0' ;
            }
   }
  printf("Output String is :  ")
  puts(line);
  return 0;
}


                                  :: OUTPUT ::  

Enter a string: G-="o";][d 
Output String is: God

30 Jul 2013

PROGRAM TO PRINT SPIRAL MATRIX

Q) Write a program to print spiral matrix .


Sol)

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

void main()
{
      int m[20][20],i,j;
      int lc,hc,lr,hr,r,c,cnt ;
      clrscr();

     printf("www.c-programcodes.co.in");
     printf("\n Enter r & c ::") ;
     scanf("%d%d" ,&r, &c) ;;
    
     cnt=1;
     lr=0, lc =0 ;
     hr= r-1;
     hc = c-1;
    while(lr<=hr && lc <= hc )
        {
            i = lr;
            for(j=lc;j<= hc ; j++)
                 {
                         m[i][j] = cnt++ ;
                         j= hc;
         
                         for(i=lr+1;i<=hr)
                             {
                                 m[i][j]= cnt++;
                                 if( lr != hr)
                                      {
                                         i =hr;
              
                                         for(j=hc-1;j>=lc;j--)
                                               m[i][j] = cnt++ ;
                                     }
                                 if(lc != hc)
                                     {
                                         j =hc;
                                         for(i=hr-1;i>lr;i--)
                                                m[i][j] = cnt++ ;
                                     }
                          }
                }
        lr++;
        lc++;
        hr--;
        hc--;
       }

     printf("\n Spirally filled matrix is \n");
     for(i=0;i<r ;  i++)
     {
         for(j=0;j<c;j++)
           {
                   printf("%4d", m[i][j]) ;
                   printf("\n") ;
            }
      }
}

19 Jun 2013

OPEN a FILE

Q) Write a C program to open a file


Sol)

#include<stdio.h>
#include<stdlib.h>

int main()
{

   ch, file_name[24];
   FILE *fp;

    printf("c-programcodes.blogspot.in");
    printf("Enter the name of file you wish to see\n");
    get(file_name,"r");      //read mode

    if(fp==NULL)
       {
              perror("Error while opening the file \n") ;
              exit(EXIT_FAILURE) ;
       } 
    
    printf("The contents of %s file are: \n", file_name);

    while((ch=fgetc(fp)) != EOF)
             printf("%c",ch) ;
  
    fclose(fp);
    return 0;

}

16 May 2013

PROGRAM TO MAKE A FISH

Q) Write a program to make a FISH.


Sol)

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

Void main ()
 {

       int gd=DETECT, gm ; 
       initgraph (&gd,&gm, "     "); 

       ellipse (200, 200, 0, 360, 50, 30);  
       line(250, 200, 280, 170);
       line(280, 170, 280, 170);
       line(280, 230, 250, 200);
       circle(160, 190, 3); 

       getch (); 
       closegraph();

 }

18 Apr 2013

PROGRAM TO CONVERT CHARACTERS FROM LOWERCASE TO UPPERCASE AND FROM UPPERCASE TO LOWERCASE IN GIVEN STRING IN C++

Write a program to convert all characters from lowercase to uppercase and all upper case characters to lowercase in given string


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int m,t=0,i;
 char a[100];
 cout<<"Enter the string\n";
 cin.getline(a,100);
 for(i=0;a[i]!='\0';i++)  //to count total number of words include space
 {
  t++;
 }
 cout<<"\nString after conversion\n";
 for(i=0;i<t;i++)
 {
  m=0;
  m=a[i];
  if(m>=65&&m<=90)
  {
   m=m+32;
   cout<<char(m);
  }
  else
   if(m>=97&&m<=122)
   {
    m=m-32;
    cout<<char(m);
   }
   else
    {
     cout<<char(m);
    }
  }
 getch();
}
==OUTPUT==

9 Apr 2013

PROGRAM TO PRINT UPPER TRIANGULAR MATRIX IN C++


Write a program to print upper triangular matrix in c++

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of matrix\n";
 cin>>m;
 cout<<"enter no of colomns of matrix\n";
 cin>>n;
 cout<<"\n\nenter the elements of matrix\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\nmatrix u have entered is"<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"Upper tringular matrix is"<<endl;
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     if(i<=j)
     {
      cout<<"\t"<<a[i][j];
     }
     else
     {
      cout<<"\t ";
     }
    }
    cout<<endl;
  }


getch();
}

==OUTPUT==

8 Apr 2013

PROGRAM TO COUNT TOTAL NUMBER OF POSITIVE AND NEGATIVE ELEMENTS IN A MATRIX AND PRINT THEM IN C++


#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10];
 int i, j, n, m;
 int x[100],y[100],pos=0,neg=0;
 cout<<"enter no of rows\n";
 cin>>n;
 cout<<"enter no of colomn\n";
 cin>>m;
 cout<<"enter the values of matrix\n";
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>a[i][j];
    }

  }
 cout<<"matrix u have entered is"<<endl;
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
  }
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     if(a[i][j]>=0)
      {
       x[pos]=a[i][j];
       pos++;
      }
     else
       {
        y[neg]=a[i][j];
        neg++;
       }
    }
  }
  cout<<"total positive elements are= "<<pos<<endl;
  cout<<"positive elements are"<<endl;
  for(i=0;i<pos;i++)
   {
    cout<<x[i]<<endl;
   }
  cout<<"total neg elements are= "<<neg<<endl;
  cout<<"neg elements are"<<endl;
  for(i=0;i<neg;i++)
   {
    cout<<y[i]<<endl;
   }
getch();
}
==OUTPUT==


TO PRINT TRANSPOSE OF A MATRIX IN C++


Write a program to print transpose of a matrix in c++

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[10][10];
 int i, j, n, m;
 int x[100],y[100];
 cout<<"enter no of rows\n";
 cin>>n;
 cout<<"enter no of colomn\n";
 cin>>m;
 cout<<"enter the elements of matrix\n";
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>a[i][j];
    }

  }
 cout<<"matrix u have entered is"<<endl;
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
  }
  cout<<"\n\ntranspose of matrix is:="<<endl;
  for(j=0;j<m;j++)
  {
   for(i=0;i<n;i++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
  }
getch();
}

==OUTPUT==


3 Apr 2013

COUNT TOTAL NUMBER OF EVEN AND ODD NUMBERS IN A MATRIX AND PRINT THEM IN C++

Write a program to count total number of even and odd elements in a matrix and print that elements


#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10];
 int i, j, n, m;
 int x[100],y[100],even=0,odd=0;
 cout<<"enter no of rows\n";
 cin>>n;
 cout<<"enter no of colomn\n";
 cin>>m;
 cout<<"enter the elements of matrix\n";
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>a[i][j];
    }
  }
 cout<<"matrix u have entered is"<<endl;
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
  }
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     if(a[i][j]%2==0 && a[i][j]>=0)
      {
       x[even]=a[i][j];
       even++;
      }
     else
      if(a[i][j]%2!=0 && a[i][j]>=0)
       {
        y[odd]=a[i][j];
        odd++;
       }
    }
  }
  cout<<"total even elements are= "<<even<<endl;
  cout<<"even elements are"<<endl;
  for(i=0;i<even;i++)
   {
    cout<<x[i]<<endl;
   }
  cout<<"total odd elements are= "<<odd<<endl;
  cout<<"odd elements are"<<endl;
  for(i=0;i<odd;i++)
   {
    cout<<y[i]<<endl;
   }
getch();
}

==OUTPUT==

enter no of rows
2
enter no of column
2
enter the elements of matrix
a[1][1]:=1
a[1][2]:=2
a[2[1]:=3
a[2][2]:=4
matrix you have entered is
1  2
3  4
total even elements are=2
even elements are
2
4
total odd elements are=2
odd elements are
1
3

2 Apr 2013

Print Prime Number Pyramid

Q) Write a program to print the PRIME NUMBER PYRAMID.


Sol)

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

int prime(int num) ;
void main()
{
     clrscr();
     int i,j ;
     int number=2;
     printf("c-programcodes.blogspot.in")
     for(i=0;i<5;i++)
        {
              printf("\n");
              for(j=0;j<=i;j++)
                 {
                      while(!prime(number)) ;
                         {
                             number++;
                          }
                       printf("%d\t", number++)}
          }
      getch();
}

int prime(int num)
{
        int i, flag ;
        for(i=2;i<num;i++)
           {
                if(num%i!=0)
                      flag=1;
                else
                  {
                       flag=0;
                       break ;
                   }
              }
         if(flag==1 || num==2)
                return(1) ;
         else
                return(0);
 }

                             ===OUTPUT===
c-programcodes.blogspot.in
2

3     5

7     11    13

17   19    23    29

31    37    41    47

STAR PATTERN3

Q)Write a Program to Print The Following Star Pattern in c++
    *
   * *
  * * *
 * * * *
* * * * *

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

 void main()
{
    clrscr();
    int i,j,n,k;
    cout<<"Enter number of rows"<<endl;
    cin>>n;
    
    for(i=1;i<=n;i++)
          {
              for(j=n-1;j>=i;j--)
                {
                    cout<<" ";
                 }
             for(k=1;k<=i;k++)
                {
                   cout<<"*";
                }
            cout<<endl;
        }
    getch();
}

                                                    ===OUTPUT===
Enter number of rows
5
    *
   * *
  * * *
 * * * *
* * * * *

28 Mar 2013

STAR PATTERN5

Q) Write a Program to Print The Following Star Pattern in c++
     *
   ***
  *****
 *******
*********

Sol)


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j,n,k;
 cout<<"Enter number of rows"<<endl;
 cin>>n;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n-i;j++)
  {
   cout<<" ";
  }
   for(k=1;k<=2*i-1;k++)
    {
     cout<<"*";
    }
    cout<<endl;
 }
 getch();
}

                                               ===OUTPUT===

STAR PATTERN1

Q) Write a Program to Print The Following Star Pattern in c++
      *
      **
      ***
      ****
      *****

Sol)

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int i,j,n;

   cout<<"Enter number of rows"<<endl;
  cin>>n;
 
  for(i=1;i<=n;i++)
     {
         for(j=1;j<=i;j++)
           {
               cout<<"*";
           }
        cout<<endl;
     }
 getch();
}

                                     ===OUTPUT===

19 Mar 2013

SUBTRACTION OF TWO MATRICES IN C++

Write a program for subtraction of two matrices
                      OR
Write a program to subtract two matrices

#include<iostream.h>
#include<conio.h>
void main()
{  
clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of columns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of column 2nd matrix\n";
 cin>>q;
 if(m==p && n==q)
 {
  cout<<"\n\nenter the elements of 1st matrix:=\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\n1st matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix:=\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]-b[i][j];
    }
  }
  cout<<"\n\nsubtraction of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be subtracted";
     }

getch();
}

==OUTPUT==
enter no of rows of  1st matrix
2
enter no of columns of  1st matrix
2
enter no of rows of 2nd matrix
2
enter no of columns of 2nd matrix
2
enter the elements of 1st matrix:=
a[1][1]:=1
a[1][2]:=2
a[2][1]:=3
a[2][2]:=1

1st matrix you have entered is:=
    1    2
    3    1

enter the elements of 2nd matrix:=
b[1][1]:=2
b[1][2]:=5
b[2][1]:=0
b[2][2]:=5

2nd matrix you have entered is:=
    2    5
    0    5

subtraction of two matrices is:=
    -1   -3
     3   -4

ADDITION OF MATRIX IN C++

Write a program for addition of two matrices
                      OR
Write a program to add two matrices

#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of columns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of column 2nd matrix\n";
 cin>>q;
 if(m==p && n==q)
 {
  cout<<"\n\nenter the elements of 1st matrix:=\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\n1st matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix:=\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]+b[i][j];
    }
  }
  cout<<"\n\nsum of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be added";
     }

getch();
}

==OUTPUT==
enter no of rows of  1st matrix
2
enter no of columns of  1st matrix
2
enter no of rows of 2nd matrix
2
enter no of columns of 1st matrix
2
enter the elements of 1st matrix:=
a[1][1]:=1
a[1][2]:=2
a[2][1]:=3
a[2][2]:=1

1st matrix you have entered is:=
    1    2
    3    1

enter the elements of 2nd matrix:=
b[1][1]:=2
b[1][2]:=5
b[2][1]:=0
b[2][2]:=5

2nd matrix you have entered is:=
    2    5
    0    5

sum of two matrices is:=
    3    7
    3    6

10 Mar 2013

STAR PATTERN4

Q.) Write a Program to Print The Following Star Pattern in c++
          *****
          ****
          ***
          **
          *

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

void main()
{

    clrscr();
    int i,j,n;
    cout<<"Enter number of rows"<<endl;
    cin>>n;
    for(i=n;i>=1;i--)
     {
         for(j=1;j<=i;j++)
             {
                  cout<<"*";
             }
        cout<<endl;
     }

   getch();
}

                                              =OUTPUT=

Enter number of rows 5
*****
****
***
**
*

PRINTING A MATRIX IN C++

Q.) Write a C++ Program to input the elements of  a matrix and then print them in matrix form.

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

void main()
{
   clrscr();
   int a[10][10];
   int i, j, n, m;
  
   cout<<"enter no of rows  \n";
   cin>>n;
   cout<<"enter no of colomn  \n";
   cin>>m;
   cout<<"enter the values of matrix  \n";
  
   for(i=0;i<n;i++)
     {
         for(j=0;j<m;j++)
           {
               cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
               cin>>a[i][j];
           }
     }
   cout<<"matrix u have entered is \n ";
  
   for(i=0;i<n;i++)
      {
          for(j=0;j<m;j++)
            {
                  cout<<"\t"<<a[i][j];
            }
         cout<<"\n";
     }

  getch();
}


                                        =OUTPUT=

1 Mar 2013

Simple Linked List

Q) A C program of simple Linked List without any user defined function.


SOl)

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

void main()
{
   struct node
     {
          int data;
          struct node*next;
     };

   typedef struct node node ;
   int d ;
   node *start=NULL, temp, *save ;
   int option ;
     
   printf("Enter the data for the node\n ");
   scanf("%d",&d) ;
   
   start=(node*)malloc(sizeof (node)) ;
   start->data ;
   start->next= NULL ;
   save = start ;
   
   printf("Do you want to enter more data \n" ) ;
   printf("Press 1 for yes otherwise 0 \n ");
   scanf("%d",&option) ;

   while(option==1)
     {
         printf("Enter the data for the node\n ") ;
         scanf("%d", &d) ;

         temp = (node*)malloc(sizeof (node)) ;
         temp->data=d;
         temp->next= NULL ;
         save->next= temp ;
         save=temp;

         printf("Do you want to enter more data \n") ;
         printf("Press 1 for yes otherwise 0 \n ");
         scanf("%d", &option) ;
 
    }

  printf("The Linked List is \n") ;
  
  while(start!= NULL)
      {
           printf("%d->", start->data);
           start = start->next ;
      }
   getch();
}

28 Feb 2013

Convert Binary to Octal Number

Q)Write a C program to convert the BINARY NUMBER to OCTAL NUMBER


Sol)

#include<stdio.h>

int main( )
{

    long int binary_no, octal_no=0, j=1, rem;
   
    printf("Enter any Binary number: " ) ;
    scanf("%ld", &binary_no) ;

    while(binary_no!=0)
       {
              rem=binary_no%10 ;
              octal_no=octal_no+rem*j ;
              j=j*2;
              binary_no=binary_no/10 ;
       }

     printf("Equivalent octal value: %ld", octal_no) ;
     return 0;
}

22 Feb 2013

Sum of diagonal elements in Matrix

Q)Write a C program to find the sum of diagonal elements in  matrix.
Hint: 
        1    2    3
        4    5    6
        7    8    9
First diagonal elements are 1,5,9
Second diagonal elements are 3,5,7.

Sol)
 

#include<stdio.h>
#include<conio.h>
#define MAX 5

int main()
{
    int mat[MAX][MAX],row,col ;
    int i,j,d1=0,d2=0 ;

    printf("Enter no. of rows and columns : ") ;
    scanf("%d%d",&row,&col) ;

    printf("Enter the elements of matrix: \n") ;
    if(row==col)
       {
             for(i=0; i<row; i++)
                   {
                         for(j=0; j<col; j++)
                                 scanf("%d",&mat[i][j]) ;
                     }
              for(i=0, j=col-1; i<row || j>=0; i++,j--)
                     {
                             d1=d1+mat[i][i] ;
                             d2=d2+mat[i][j] ;
                      }
            
               printf("\nThe sum of first diagonal elements : %d", d1) ;
               printf("\n The sum of second diagonal elements : %d", d2) ;
         }
     else
          {
                printf("Rows and Columns are not equal !!") ;
                printf("\n Try again !!") ;
           }

      getch();
      return 0;
}

                                    :::::OUTPUT::::

Enter no. of rows and columns : 3  2
Enter the elements of matrix:
Rows and Columns are not equal !!
Try again !!
  
Enter no of rows and columns : 3  3
Enter the elements of matrix:
1   2   3
4   5   6
7   8   9
The sum of first diagonal elements : 15
The sum of second diagonal elements : 15