×
Every problem might not have a solution right now, but don’t forget that but every solution was once a problem.
--Your friends at LectureNotes
Close

Tata Consultancy Service Entrence Exam Sample Programming Ouestions

  • Other
  • 2 Topic/Links
  • 47 Views
  • Uploaded 1 year ago
0 User(s)
Order Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

1. QUES. WRITE A PROGRAM FIBONACCI SERIES USING COMMAND LINE ARGUMENTS FOR TCS #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]) { int n, first = 0, second = 1, next, c; n = atoi(argv[1]); printf("These are %d values in Fibonacci series are by PrepInsta:-\n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%d\n",next); } return 0; }

Text from page-2

2. QUESTION. WRITE A PROGRAM TO SWAP TWO NUMBERS USING COMMAND LINE PROGRAMMING #include<stdio.h> #include<math.h> #include<stdlib.h> int main(int argc, char * argv[]) { if(argc==1){ printf("No command line argument present, add them first"); return 0; } double firstNumber, secondNumber, temporaryVariable; firstNumber = atoi(argv[1]); secondNumber = atoi(argv[2]); temporaryVariable = firstNumber; firstNumber = secondNumber; secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber); printf("After swapping, secondNumber = %.2lf", secondNumber); return 0; }

Text from page-3

3. STRING REVERSAL PROGRAM WITH COMMAND LINE PROGRAMMING #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { int k; char temp; int i,j=0; int strsize = 0; for (i=1; i<argc; i++) { strsize += strlen(argv[i]); if (argc > i+1) strsize++; } char *cmdstring; cmdstring = malloc(strsize); cmdstring[0] = '\0'; for (k=1; k<argc; k++) { strcat(cmdstring, argv[k]); if (argc > k+1) strcat(cmdstring, " "); } i = 0; j = strlen(cmdstring) - 1; while (i < j) { temp = cmdstring[i]; cmdstring[i] = cmdstring[j]; cmdstring[j] = temp; i++; j--; } printf("\nReverse string is :%s", cmdstring); return(0); }

Text from page-4

4. FIND GREATEST OF TWO NUMBER USING COMMAND LINE PROGRAMMING? #include <stdio.h> int main(int argc, char *argv[]) { int c[10]; int i,temp,j,greatest; j = 0; for(i=1; i<argc; i++) { temp = atoi(argv[i]); c[j] = temp; j++; } greatest = c[0]; for (i = 0; i < 10; i++) { if (c[i] > greatest) { greatest = c[i]; } } printf("Greatest of ten numbers is %d", greatest); return 0; }

Lecture Notes