Thursday, April 26, 2018

RVS_SOLUTIONS: Reverse words of a string

RVS_SOLUTIONS: Reverse words of a string: # include <stdio.h> # include <conio.h> # include <string.h> main() { char str[50]="students love java, c++&qu...

RVS_SOLUTIONS: Reverse words of a string

RVS_SOLUTIONS: Reverse words of a string: # include <stdio.h> # include <conio.h> # include <string.h> main() { char str[50]="students love java, c++&qu...

Reverse words of a string

# include <stdio.h>
# include <conio.h>
# include <string.h>
main()
{
char str[50]="students love java, c++";
char st[50];
int i,j;
clrscr();
strrev(str);
puts(str);
for(i=0,j=0;str[i]!='\0';i++)
{
st[j++]=str[i];
if(str[i]==' ')
{
st[j]=NULL;
j=0;
printf("%s",strrev(st));
}
}
st[j]=NULL;
printf(" %s",strrev(st));
}

Wednesday, April 11, 2018

Monday, April 2, 2018

NPTEL Programming Assignments [Jumping Numbers,Digits,Amit and the Taxi]


Programming Assignment 3.1 : Jumping Numbers
One day Ajit got a strange feeling of jumping from one point to another on a number line (containing positive integers). The jumping from one point to another can only be done in one dimension. He will start from point 0 from which he will do a lot of jumps. He can only jump in a specific sequence: 1-jump, 2-jump, 3-jump, 1jump, 2-jump, 3-jump, 1-jump, and so on. (1->2->3->1->2->3->1...) 1-jump means that if Ajit is at point x on the number line, he will jump to the point x+1. 2-jump means that id Ajit is at point x on the number line, he will jump to the point x+2.
3-jump means that id Ajit is at point x on the number line, he will jump to the point x+3.

Write a program in C which given the input point a outputs whether Ajit can reach this point a after some number of jumps or not.

Input: The first line contains a single integer a denoting the point Ajit asks about.
Output: Output "YES" without a quotes if Ajit can arrive at point a or "NO" without a quotes otherwise. Example1: Input: 3 Output: YES He can take jump sequence (1­>2) to reach point 3.
Input: 2 Output: NO From 0 he can take 1 jump to reach point 1 but after that he can take only 2 jumps which will lead him to point 3. Jump sequence (1­>2).
Constraints:  0<=a<=1018
# include <stdio.h>
int main()
{
  int i=0,j[3]={1,2,3};
  int n,x=0;
  scanf("%d",&n);
  while(x<=n)
  {
    x+=j[i];

    if(x==n)
    {
      printf("YES");
      break;
    }
    if(i==2)
      i=0;
      else
      i++;
    }
  if(x!=n)
      printf("NO");

}
Programming Assignment 3.3 : Digits
You are provided with a number N containing only digits "1" and "0". The idea is to make the number to have all the digits same. For that, you can only change exactly one digit, either "0" to "1" or "1" to "0". If it is possible to make all the digits same by flipping exactly one digit then print "YES" else "NO". Input: A number made up of only digits "1" and "0". Output: Print "YES" if it is possible to make all the digits same else "NO" without quotes. Example: Input: 101 Output: YES Input: 11 Output: NO
# include <stdio.h>
int main()
{
  int n,d,o=0,z=0;
  scanf("%d",&n);
   
   while(n>0)
  {
       
     if(d=n%10==0)
       z++;
    else
      o++;
    
     n/=10;
  }
  if(z==1 || o==1)
  {
    printf("YES");
  }
  else
  {
    printf("NO");
  }
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Programming Assignment 3.4: Amit and the Taxi
Amit is working in a company such that he has to travel to some restaurant every day which is at N meters distance from his home. Amit has two choices, either to take the restaurant's taxi (where he wants to visit) or he can walk to the restaurant. Whenever he walks, he walks with a velocity of V1 m/s. The taxi, on the other hand, moves with a velocity of V2 m/s. Whenever he calls for the taxi, the taxi first travels a distance of N meters from the restaurant to his home and then from home, it travels back to the restaurant. The taxi crosses a total distance of N meters while going from home to the restaurant, on the other hand, he has to cover a distance of sqrt(2)*N (because of some staircases) when he walks. Write a program in C to help Amit to decide whether he should use the taxi or he should walk so that his travel time is minimized. Input: Three space-separated integers N, V1, V2 Output: Output a string either "Taxi" or "Walk" depending on the answer Example: Input: 5 10 15 Output: Taxi Constraints: 1 <= N, V1, V2 <= 100
# include <stdio.h>
# include <math.h>
int main(int argc,char*argv[])
{
  int N,V1,V2;
  float w,c;
  scanf("%d%d%d",&N,&V1,&V2);
 
  w=((float)N/V1)+sqrt(2)*N;
  c=((float)N/V2);
    if(w>c)
    printf("Walk");
    else
    printf("Taxi");
   
}