Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

30 Jul 2014

CALL BY VALUE PROGRAM IN C

Program of Call by value for swapping two values

#include<stdio.h>
void swap(int a, int b)
{
  int temp;
  temp=a;
  a=b;
  b=temp;
 }
void main()
{
   int a=5;
   int b=10;
   /*printf("c-programcodes.blogspot.in");*/
   swap(a,b);  /*the values goes out of scope  after the function is executed*/
   printf("values are not swapped");
   printf("a=%d\n",a);
   printf("b=%d",b)
}
==OUTPUT==

values are not swapped
a=5
b=10

7 Jul 2014

Java Program #2

Q) Write a Program to print STAR Pattern.
   *
   **
   ***
   ****
   *****

Sol)

class Stars {
  public static void main(String[] args) {
    int row, numberOfStars;

    for (row = 1; row <= 5; row++) {
      for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
        System.out.print("*");
      }
      System.out.println(); // Go to next line
    }
  }
}

Java Program #1

Q) Write a Program to print " Hello World ".


Sol)

   public class HelloWorld {

    public static void main(String[] args) {
        
        System.out.println("Hello World");
        
    }

30 Jan 2014

LOWER TRIANGULAR MATRIX

Write a program to print lower 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<<"Lower tringular matrix is"<<endl;
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     if(j<=i)
     {
      cout<<"\t"<<a[i][j];
     }
     else
     {
      cout<<"\t ";
     }
    }
    cout<<endl;
  }


getch();
}

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

28 Aug 2013

STAR PATTERN2

Write a program to print the following star pattern

*****
****
***
**
*

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


23 Aug 2013

MULTIPLICATION OF TWO MATRICES

Write a program for multiplication  of two matrices
                        OR
Write a program to multiply 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,k,x[10];
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of colomns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of colomn 2nd matrix\n";
 cin>>q;
 if(n==p)
 {
  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\nfirst 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"<<b[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<q;j++)
   {
   c[i][j]=0;
    {
     for(k=0;k<n;k++)
      {  
        x[k]=0;
        x[k]=a[i][k]*b[k][j];
        c[i][j]=c[i][j]+x[k];
      }
    }}
   }
  cout<<"\n\nmultiplication of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<q;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be multiplied";
     }

getch();
}

==OUTPUT==


enter no of rows of 1st matrix
2
enter no of column of 1st matrix
2
enter no of rows of 1st matrix
2
enter no of column of 1st matrix
2

enter the elements of 1st matrix
a[1]=1
a[2]=2
a[3]=2
a[4]=3

first matrix u have entered is:=
1 2
2 3

enter the elements of 2nd matrix
b[1]=2
b[2]=3
b[3]=2
b[4]=1

2nd matrix u have entered is
2 3
2 1

multiplication of two matrices is
6 5
8 9

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;

}