Labels

Showing posts with label Placement paper questions. Show all posts
Showing posts with label Placement paper questions. Show all posts

Tcs question to print series 1,2,1,3,2,5,3,8...

#include <stdio.h>

int main()
{
     int n,j=2,i,a=1,b=2,c;
     int arr[20];
     scanf("%d",&n);
     arr[0]=a;
     arr[1]=b;
         
         for(i=1;i<=n;i++){
             
             c=b-a;
             arr[j]=c;
             j++;
             a=b;
             b=c;
             
             
             c=b+a;
              arr[j]=c;
             j++;
             a=b;
             b=c;
            
             
         }
     //display series
     
     for(i=0;i<10;i++){
         printf("%d,",arr[i]);
     }
      
}

Tcs question - C program to reverse string using command line

#include <stdio.h>


int main(int argc,char *argv[])

{

    int len;
     char *ptr;//take pointer to traverse
     
    ptr=argv[1];
    
    
    len=strlen(argv[1]);

    if(argc==1){

        printf("Error:Please enter atleast one argument");

        return 0;

    }

        while(len>=0){
            
            printf("%c ",ptr[len--]);
        }



      return 0;

      

     

     

}

Tcs question C program to find LCM in command line

#include <stdio.h>


 int main(int argc, char *argv[])
 {

 int lcm,i,num1,num2,max_value=0;
  num1=atoi(argv[1]);
  num2=atoi(argv[2]);
   
   max_value=(num1>num2)?num1:num2;
   
  while(1){//Always true till we get the lcm
      
       if(max_value%num1==0&&max_value%num2==0){
           
       printf("LCM is :%d",max_value);
       break;
       }
       ++max_value;
  }
  
  

return 0;
}

Tcs question find out gcd of two number in c using command line

#include <stdio.h>


 int main(int argc, char *argv[])
 {

 int gcd,i,num1,num2=0;
  num1=atoi(argv[1]);
  num2=atoi(argv[2]);
   
  
  for(i=1;i<=num1 && i<=num2;i++){
      
      if(num1%i==0 && num2%i==0){
      gcd=i;
          
      }
  }
  printf("GCD is :%d",gcd);

return 0;
}


Tcs question print string in c using command line

#include <stdio.h>

int main(int argc,char *argv[])
{
     
    if(argc==1){
        printf("Error:Please enter atleast one argument");
        return 0;
    }
    
//Note:- argv[0] is by default 0 our input string start storing from argv[1]. So it's better to start for loop from 1

      int i,v=0,size=argc-1;
      
      char *str;//intialize pointer
      
      //allocate memory to which pointer points
      
      str=(char*)malloc(v);//intially zero
      
      //now re allocate memory as per size of input sting arguments
      for(i=1;i<=size;i++){
          
     int length=v+strlen(argv[i]);
      str=(char*)realloc(str,length);
      
      //now concatenate the string argument
      strcat(str,argv[i]);
      //give a namespace
      strcat(str," ");
      }
      
      //now print string use %s not %concatenate
      printf("%s\n",str);
      
      return 0;
      
     
     
}