Menu BAR

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

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

31 Jan 2013

C-program to find PERFECT NUMBER

Q.) Find out the perfect number using C- program .


Sol)

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

int main( )
{
    int n, i=1, sum=0;
    printf("Enter a Number : ") ;
    scanf("%d",&n) ;
    
    while(i<n)
      {
           if(n%i==0)
                 {  sum=sum+i ; }
           i++ ;
       }
    if(sum==n)
           printf("\n %d is a perfect number", i) ;
    else
           printf("\n %d is not a perfect number", i)  ;
    return 0;
}

                     :: OUTPUT ::
      
Enter a Number : 6
6 is perfect number
                                            

30 Jan 2013

Linear Search using C program

#include<stdio.h>
int main( )
{
    int a[10], i, n, m, c=0 ;
    printf("Enter the size of an array : ") ;
    scanf("%d", &n) ;
    printf("Enter the elements of an array: ") ;
    for(i=0;i<=n-1;i++)
      {   
            scanf("%d",&a[i]);
       }
     printf("Enter the number to be searched: ") ;
     scanf("%d",&m) ;
     for(i=0;i<=n-1;i++)
        {
              if(a[i]==m)
                {   c=1;
                   break;
                }
          }
     if(c= =0)
                printf("The number is found at %d position",i+1) ;
     else
                printf("The number is not in an array  ") ;
     return 0 ;
}

29 Jan 2013

Table

Q.)  Write a program to printout the following table:
        For ex->
                        5*1=5
                        5*2=10
                        5*3=15
                        5*4=20
                        5*5=25
                        5*6=30
                        5*7=35
                        5*8=40
                        5*9=45
                        5*10=50

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

int main( )
{
   int x=1, num, res ;
   printf("Enter Number : " ) ;
   scanf("%d", &num) ;
   while(x<=10)
     {
          res=num*x ;
          printf("\n%d*%d=%d",num,x,res ) ;
          x++ ;
      }

    return 0;
}