Saturday, 19 March 2016

Fibonacci Roots


The first two terms in the Fibonacci sequence are 0 and 1, respectively, and each subsequent term is the sum of the previous two. Using this definition to calculate the first several terms in the sequence, we get

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Let us define the Fibonacci roots of a positive integer n to be the two smallest consecutive Fibonacci numbers whose sum is greater than or equal to n.

Input Format: 
Input consists of a single integer which corresponds to n.
Assumption: 
Assume that n is less than or equal to 2000.
Output Format: 
Output consists of integers, separated by a space.
Sample Input 1 : 
31
Sample Output 1: 
13 21
Sample Input 2 : 
89
Sample Output 2: 
34 55

Code:
  #include<stdio.h>
int main(){
  int a=0,b=1,c=0,n;
  scanf("%d",&n);
  while(1){
    c=a+b;
    if(c>=n)
      goto END;
    a=b;
    b=c;
  }
  END: printf("%d %d",a,b);
  return 0;
}

Vowel or Consonant


Write a program to determine whether the input character is a vowel or consonant.

Input and Output Format: 
Input consists of a single character. Output consists of a string --- “Vowel” / “Consonant” / “Not an alphabet”
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1: 
Enter a character
a
Vowel
Sample Input and Output 2: 
Enter a character
Z
Consonant

Sample Input and Output 3: 
Enter a character
#
Not an alphabet

Code:
  #include<stdio.h>
int main(){
  char in;
  char arr[11]={'a','e','i','o','u','A','E','O','U','I'};
    int i,flag=0,con=0;
  printf("Enter a character\n");
  scanf("%c",&in);
  if(((in>='a') && (in<='z'))||((in>='A')&&(in<='Z')))
  {
    for(i=0;i<10;i++)
    {
      if(in==arr[i]){
        flag++;
        break;
      }
      else
        con++;
    }
  }
  else 
    printf("Not an alphabet\n");
  
  if(con)
    printf("Consonant");
  if(flag)
    printf("Vowel");

  
  
  return 0;
}

Anagrams


Write a program to find whether the 2 given strings are anagrams or not. Anagrams are words or phrases made by mixing up the letters of other words or phrases,

Input and Output Format: 
Input consists of 2 string. Assume that all characters in the string are lowercase letters or spaces and the maximum length of the string is 100.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1: 
Enter the first string
anitha
Enter the second string
amphisoft
anitha and amphisoft are not anagrams

Sample Input and Output 2: 
Enter the first string
the eyes
Enter the second string
they see the eyes and they see are anagrams

Code:
  #include<stdio.h>
#include<string.h>
int main(){
  char s1[200],s2[200];
  int first[26]={0},second[26]={0},c=0,i,flag=0;
    //int l,l1;
  printf("Enter the first string\n");
  gets(s1);
  printf("Enter the second string\n");
  gets(s2);
  
    while(s1[c]!='\0')
    {
      first[s1[c]-'a']++;
      c++;
    }
    c=0;
    while(s2[c]!='\0')
    {
      second[s2[c]-'a']++;
      c++;
    }
    for(i=0;i<26;i++){
      if(first[i]!=second[i])
        flag=1;
    }
      
  if(flag==0)
    printf("%s and %s are anagrams",s1,s2);
  else
    printf("%s and %s are not anagrams",s1,s2);
  
  return 0;
}

Zero Duplicates


Write a program that will read in a list of numbers and will then print out the same list except numbers that have already been printed will be printed as a zero instead.

Input Format: 
Input consists of n+1 integers where n is the number of elements in the list. The first integer corresponds to n. The remaining n integers correspond to the elements in the list. 
Assume that the maximum value of n is 100. 

Output Format: 
Output consists of n integers on separate lines.

Sample Input : 
5
2
3
4
3
6
Sample Output: 
2
3
4
6

Code:
  #include<stdio.h>
int main(){
  int n,a[100],i,c,j;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  
  for(i=0;i<n;i++)
  {
    c=0;
    for(j=0;j<i;j++)
    {
      if(a[i]==a[j]){
        c=1;
        break;
      }
    }
    if(c==1)
      printf("0\n");
    else
      printf("%d\n",a[i]);
  }
  
  return 0;
}

The Next Palindrome


A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K, write the value of the smallest palindrome larger than K to output. 

Input 
The first line contains an integer, which corresponds to K. Assume that K is less than 200000. 

Output
Output consists of a single integer, which corresponds to the smallest palindrome larger than K.

Sample Input 1:
808

Sample Output 1:
818

Sample Input 2:
2133

Sample Output 2:
2222

Code:
  #include<stdio.h>
int main()
{
  long int num,pa=0,rem,temp;
  scanf("%ld",&num);
  while(num!=pa){
    num=num+1;
    temp=num;
    pa=0;
    while(temp!=0){
      rem=temp%10;
      pa=rem+pa*10;
      temp=temp/10;
    }
  }
  printf("%ld",pa);
  return 0;
}

TENNIS COURT


Many of the current final year students of a college are excellent tennis players. They always emerge as champions in the State Level Inter-Collegiate Tennis Meet. They want their juniors to carry on their legacy in the field of tennis and they started training their juniors in tennis. They were happy to see them play very well during their coaching sessions. They had a very comfortable win over one of the top teams in the game played at their college. But to their dismay, their juniors performed very poorly in the games conduced at other venues.

The think-tank of the College Tennis club was pondering over the reasons for their poor performance in the meets. David came up with a possible reason for the poor performance of their junior team. The tennis court in their college is a grass court but the tennis courts in most other competitive venues are synthetic courts. Their junior team has learnt playing tennis in the grass court and they found it very difficult to adapt their game to synthetic courts. The entire team agreed with this reason and they planned to request the chairman to convert the grass tennis court to synthetic tennis court.

They sent a request to the Chairman and he readily agreed but he asked to do it with minimal expenditure. He gave them square stones. 

The tennis court in the college is rectangular in shape of size n*m metres. We need to pave the court with square stones. The square stone is of size a*a. 

What is the least number of stones needed to pave the tennis court? It's allowed to cover the surface larger than the Tennis Court, but the court has to be covered. It's not allowed to break the stones. The sides of stones should be parallel to the sides of the court. 

Input Format: 
Input consists of 3 integers. The first integer corresponds to 'n', the length of the court. The second integer corresponds to 'm', the width of the court. The third integer corresponds to 'a', the side of the square stone. 

Output Format: 
Output consists of ta single integer k, where k is the least number of stones needed. 

Sample Input: 
 6 
6 
4 
Sample Output: 
4

Code:
  #include<stdio.h>
#include<math.h>
int main(){
  int l,b,la,ba,plate,a;
  scanf("%d",&l);
  scanf("%d",&b);
  scanf("%d",&a);
  la=round((float)l/a);
  ba=round((float)b/a);
  plate=la*ba;
  printf("%d",plate);
  return 0;
}
  

SMS Language


SMS language or textese (also known as txt-speak, txtese, chatspeak, txt, txtspk, txtk, txto, texting language, txt lingo, SMSish, txtslang,or txt talk) is a term for the abbreviations and slang commonly used with mobile phone text messaging. 

 Some of the abbreviations used are

  • s for yes
  • u for you
  • 2day for today
  • y for why

Many grandpa's have started sending SMSes to their grand children. But they are not familiar with the SMS lingo. 

Can you help them by writing a program that would convert a given text in proper English to SMS lingo? Consider only the 4 words listed above. 

Input Format: 
Input consists of a single string. Assume that the maximum length of the string is 200 and all letters are in lower-case. 

Output Format: 
Output consists of a single string. 

Sample Input 1: 
where were you yesterday? 

Sample Output 1: 
where were u yesterday? 

Sample Input 2: 
why is today a working day for you? 

Sample Output 2: 
y is 2day a working day for u?

Code:
  #include<stdio.h>
#include<string.h>
void replace(char *);
int main(){
  char s1[100];
  gets (s1);
  replace(s1);
  printf("\n%s",s1);
  return 0;
}
void replace(char *s1)
{
  char r[4][10]={"yes","you","today","why"};
    char rep[4][5]={"s","u","2day","y"};
      char buff[500];
  int i;
  char *ch;
  for(i=0;i<4;i++)
    if((ch=strstr(s1,r[i]))){
      strncpy(buff,s1,ch-s1);
      buff[ch-s1]=0;
      sprintf(buff+(ch - s1),"%s%s",rep[i],ch+strlen(r[i]));
      s1[0]=0;
      strcpy(s1,buff);
      return replace (s1);
    }
}