Sunday 12 November 2017

Code Challenge Solutions 2

1. Emily’s Lucky Awards

Madam Emily often conducts surprise tests for her ‘n’ kids. 
That day too it was a shocking surprise for the kids as Emily announced it no sooner the kids were back from their vacation. 
Evaluating the performances of the kids Emily found the overall result to be very inconsistent as there were positive as well negative scores from the kids. 

However Emily wished to give away lucky awards to the kids in a means to motivate them. She is much fond of number ‘f’. But she couldn’t find any student among ‘n’ students who has scored f marks. 

Hence she decided to give away the awards to the pair of students whose scores sum is f. 

Examples: 
Input  :  If n = 4, scores[] = {1, 5, 7, -1} and Emily’s fond number f = 6. 
Output :  2 
Pair with sum 6 are (1, 5) and (7, -1) 
Let us help Emily determine those pairs of scores.  
  
Input Format: 
The first line of the input consists of an integer, n that corresponds to the number of kids. Assume that the maximum number of input elements in the array is 50. 
The next 'n' lines in the input consists of an integer, which correspond to the scores of kids. 
The next line of the input consists of an integer f, which corresponds to Emily’s fond number. 

Output Format: 
Output is an integer – number of pairs whose sum is equal to fond number. 
Refer sample input and output for formatting specifications. 

Sample Input1: 
-1 
Sample Output1: 
  
Sample Input2: 
Sample Output2: 


Program

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


2. Special Name Problem

Arvind believes in Numerology a lot and when his first child was born, he consulted the leading numerologist in the city about selecting a name for his child. The numerologist suggested that the name that would be lucky for his child should have the following characteristics --- It should not start with a vowel and it should not end with a vowel and it should contain exactly 1 vowel.
Can you help Arvind in shortlisting the names for his child?
Assume that the maximum length of the name is 30.
Input and Output Format:
Input consists of a string that corresponds to one of the names.
The output consists of a single string that is either yes or no. Output yes if the name can be shortlisted based on the suggested criteria.
Sample Input 1:
Sam
Sample Output 1:
yes
Sample Input 2:
Bame
Sample Output 2:
no

Program


#include<string.h>
#include<stdio.h>
int main()
{
int l,i,a=0;
char name[30];
scanf("%s",name);
l=strlen(name);
for(i=1;i<l-1;i++)
{
if((name[i]=='a')||(name[i]=='e')||(name[i]=='i')||(name[i]=='o')||(name[i]=='u'))
{
a++;
}
}
if((name[0]=='a')||(name[0]=='e')||(name[0]=='i')||(name[0]=='o')||(name[0]=='u'))
printf("\nno");
else if((name[l-1]=='a')||(name[l-1]=='e')||(name[l-1]=='i')||(name[l-1]=='o')||(name[l-1]=='u'))
printf("\nno");
else
if(a==1)
printf("\nyes");
else
printf("\nno");
return 0;
}




More Code challenge Problems and solutions:


1 comment: