Question On C Sharp
Question On C Sharp
01 Lucky Number
In a lucky draw, all the contestants with ticket numbers having a unique property
(mentioned below) were chosen as the Lucky Winners.
Let S(n) be the sum of the digits of n. For example, S(484) = 4+8+4 = 16 and S(22) =
2+2 = 4. Any nonprime positive integer x is called a Lucky Number if S(x*x) =
S(x)*S(x). For example, 22 is a Lucky Number because S(484) = S(22)*S(22).
Given 2 integers, m and n, write a program to find the number of lucky numbers
between m and n (both m and n inclusive).
Input Format:
Input consists of 2 integers, m and n, m <= n
Output Format:
Output consists of a single integer that corresponds to the number of lucky numbers
between m and n.
Sample Input :
20
30
Sample Output :
4
/////////////////////
02 Total Marks
There are two types of questions in a Question Paper --- 1 type of question for which 'X' marks are
awarded for the correct answer and the other type of question for which 'Y' marks are awarded for
the correct answer. There are 'n1' questions of type 1 and 'n2' questions of type 2.
A question will be awarded full marks if the answer is completely correct. Otherwise 0 marks will be
awarded for that question.
Given a mark scored by a student ('M') , find whether it is a valid mark as per this scoring system.
Input Format:
Input consists of 5 integers that correspond to X, Y, N1, N2 and M respectively.
Output Format:
The first line of the output consists of a string that is either “Valid” or “Invalid”.
If the first line of the output is valid, in the next line print the marks scored by the student in Type I
questions and the marks scored by the student it Type II questions.
In cases of multiple possible answers, print the case where the student must have answered the
maximum number of Type 1 questions.
Sample Input 1:
5
8
5
4
44
Sample Output 1:
Valid
4
3
[Explanation : 4*5 + 3*8 = 44 // 4 "5 Marks" Questions and 3 "8 Marks" Questions are correct]
Sample Input 2:
5
8
5
4
46
Sample Output 2:
Invalid
//////////////////////
Mahirl's uncle Sam was teaching her addition and multiplication. She started
doing all the basic exercises in addition and multiplication correctly.
To challenge her more, Sam asked her an interesting question. He asked her to
start with number 10. The only 3 operations that Mahirl can perform repeatedly
are +2, -1 and *3. Given a target number N, Mahirl has to find the minimal
number of operations she should use to get N starting from number 10.
For example if the target number is 33, for reaching 33 from 10, 4 operation
invocations are required : one *3 and two +2 and one -1.
For example if the target number is 28, for reaching 28 from 10, 3 operation
invocations are required : one *3 and two -1.
Being a 6 year old kid, this problem was quite difficult for Mahirl to solve. Can you
please help her out in solving this problem?
Input Format:
Input consists of an integer that corresponds to N.
Output Format:
Output consists of an integer that corresponds to the minimal amount of
operations Mahirl has to use to reach N starting from 10.
Sample Input 1:
33
Sample Output 1:
4
Sample Input 2:
28
Sample Output 2:
3
//////////////////////
04 Lucky String
A string is said to be a 'Lucky String' if it consists of a substring of length 'n' that consists
of only letters 'P', 'S' and 'G' and there occurs atleast n/2 consequtive P's or S's or G's in the
substring. Given a string find out if it is a lucky string or not.
Assume that the length of the substring is always even and the maximum length of the input string is
50.
Assume that the string consists of only upper case letters.
Sample Input 1:
8
FGHPSGGGGPPSABD
Sample Output 1:
Yes
Sample Input 2:
8
FGHPSGGGSPPSABD
Sample Output 2:
No
Sample Input 3:
20
FGHPSGGGSPPSABD
Sample Output 3:
Invalid
////////////////////
Sample Output 3:
Invalid
Solution:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LuckyString
{
class Program
{
static void Main(string[] args)
{
string input;
int n;
n = Convert.ToInt16(Console.ReadLine());
input = Console.ReadLine();
if (input.Length < n)
{
Console.WriteLine("Invalid");
}
else
{
Console.WriteLine(UserMainCode.lucky_string(input, n));
}
}
}
}
UserMainCode.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lucky_String
{
class UserMainCode
{
public static string lucky_string(string input, int n)
{
string result = "";
int consecutive_length = n / 2;
int len = input.Length;
for (int k = 0; k < len - n; k++)
{
string s = input.Substring(k, n);
int outp = (from s1 in s
where s1 == 'P' || s1 == 'G' || s1 == 'S'
select s1).Count();
if (outp == n)
{
for (int i = 0; i < s.Length; i++)
{
int count = 1;
char out1 = s[i];
for (int j = i + 1; j < s.Length; j++)
{
if ((out1 == s[j]))
{
count++;
}
else
{
break;
}
}
if (count >= consecutive_length)
{
result = "Yes";
break;
}
else
{
result = "No";
}
}
}
}
return result;
}
}
}