Menu BAR

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

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

20 Feb 2013

Count the number of digits using while loop

Q) C program to count the number of digits in a number using while loop.

Sol)

#include<stdio.h>

int main()
{
    int num,count=0;

   printf("Enter a number : ") ;
   scanf("%d",&num);
   
   while(num)
      {
          num=num/10;
          count++ ;
       }
   
   printf("\nTotal digits is :  %d",count) ;
   return 0;
}

15 Feb 2013

Subtraction of two matrices

Q)Write a C program for SUBTRACTION of two matrices.


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

int main()
{
   int a[3][3],b[3][3],c[3][3],i,j ;
   printf("Enter the first matrix->") ;
   for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&a[i][j]) ;
                }
        }
     printf("\nEnter the Second matrix->");
     for(i=0;i<3;i++)
       {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&b[i][j]) ;
                }
        }
     for(i=0;i<3;i++)
         {
              for(j=0;j<3;j++) 
                  {
                       c[i][j]=a[i][j]-b[i][j] ;
                   }
          }
     printf("\nResultant matrix after Subtraction->\n ") ;
     for(i=0;i<3;i++)
      {    printf("\n");
            for(j=0;j<3;j++)
               {
                   printf("%d\t",c[i][j]) ;
                }
       }
    return 0; 
}

Addition of two matrices

Q) Write a C program for addition of two matrices using arrays.


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

int main()
{
    int a[3][3],b[3][3],c[3][3],i,j ;
    printf("Enter the first matrix->") ;
    for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&a[i][j]) ;
                }
       }
    printf("\nEnter the second matrix->") ;
    for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&b[i][j]) ;
                }
       }
    
    for(i=0;i<3;i++)
       {
            for(j=0;j<3;j++)
               {
                  c[i][j]=a[i][j]+b[i][j] ;
                }
        }
     printf("\nThe addition of two matrix is -> \n ") ;
     for(i=0;i<3;i++)
       {    printf("\n") ;
             for(j=0;j<3j++)
                {
                     printf("%d\t",c[i][j]) ;
                 }
        }
      return 0;
}  

1 Feb 2013

String concatenation without using strcat

Q.) Write a C-Program to concatenate the strings.


Sol)

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

void strconcat(char[],char[])
int main()
{
     char str1[100],str2[100];
     int compare ;

     printf("Enter first String : ") ;
     scanf("%s",str1) ;

     printf("Enter second String : ") ;
     scanf("%s",str2) ;

     strconcat(str1,str2) ;

     printf("String after concatenation : %s",str1) ;

     return 0 ;
}

void strconcat(char str1[], char str2[])
{
    int i=0,j=0;

    while(str[i]!='\0')
                i++ ;


    while(str2[j]!='\0')
          {
                str1[i]=str2[j] ;
                i++; j++;
          }
     str1[i]='\0' ;
}     

PASCAL TRIANGLE USING C PROGRAM

Q.) Write a C program to print PASCAL Triangle without using array
                                                or
      Write a C program to print PASCAL Triangle using for loop.

Sol )

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

long fact(int) ;
int main()
{
      int line,i,j ;
      printf("Enter the number of lines : ") ;
      scanf("%d",&line) ;
   
      for(i=0;i<line;i++)
          {
               for(j=0;j<line-i-1;j++)
                         printf("  ");

               for(j=0;j<=i;j++)
                         printf("%ld ",fact(i)/(fact(j))*fact(i-j))) ;
               printf("\n") ;
           }
       return 0;
}

long fact(int num)
{
          long f=1;
          int i=1;
          while(i<=num)
             {
                   f=f*i ;
                   i++ ;
              }
          return f;
}

                                :: SAMPLE OUTPUT ::
Enter the number of lines : 6
                           1
                       1       1
                     1    2    1
                  1    3    3   1
                1   4     6    4   1
              1  5  10    10   5  1

To print the FLOYD's TRIANGLE

Q.) Write a C-program to print Floyd's Triangle .
                               or
        How to print Floyd's Triangle in C.

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

int main()
{
   int i,j,r,k=1 ;
   
   printf("Enter the range : ") ;
   scanf("%d",&r) ;
   
   printf("FLOYD's TRIANGLE \n\n") ;
   for(i=1;i<=r;i++)
        {
            for(j=1;j<=i;j++,k++)
                       printf("%d",k) ;
             printf("\n") ;
         }
    return 0 ;
}  

Armstrong number or not

Q.) Write a C program to check whether the number is ARMSTRONG NUMBER or NOT .

Sol)

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

int main()
{
    int num,r,sum=0,temp ;
    
    printf("Enter a number: ") ;
    scanf("%d",&num) ;
    
    temp=num ;
    while(num!=0)
         {
               r=num%10;
               num=num/10;
               sum=sum+(r*r*r) ;
          }

     if(sum==temp)
                 print("%d is an ARMSTRONG NUMBER",temp) ;
      else
                 printf("%d is not an ARMSTRONG NUMBER", temp) ;
      return 0 ;
  }