0% found this document useful (0 votes)
475 views9 pages

Mock4 Set1

The document contains 5 programming questions and their solutions in C#. Each question provides sample inputs/outputs and business rules. The questions cover topics like string manipulation, checking if a number is a probable topper number based on a pattern in its digits, left rotating an array, printing all possible cards from a deck of 52 cards, and finding the sum of prime numbers between two limits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
475 views9 pages

Mock4 Set1

The document contains 5 programming questions and their solutions in C#. Each question provides sample inputs/outputs and business rules. The questions cover topics like string manipulation, checking if a number is a probable topper number based on a pattern in its digits, left rotating an array, printing all possible cards from a deck of 52 cards, and finding the sum of prime numbers between two limits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Q1. Write a program to toggle each word in a string.

Sample input:

Learn at Codeforwin.

Sample output:
lEARN AT cODEFORWIN.

Business Rule:
The string should be minimum 5 characters and maximum 30 in length

Solution:
using System;
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
if (str.Length < 5 || str.Length > 30)
{
Environment.Exit(0);
}
else{
string result = string.Empty;
char[] inputArray = str.ToCharArray();

foreach (char c in inputArray)


{
if (char.IsLower(c))
result += c.ToString().ToUpper();
else if (char.IsUpper(c))
result += c.ToString().ToLower();
else
result += c.ToString();
}

Console.WriteLine(result);
}
}
}

Q2. In the University Examinations conducted during the past 5 years, the toppers registration
numbers were 7126, 82417914, 7687 and 6657. Your father is an expert in data mining and he
could easily infer a pattern in the toppers registration numbers. In all the registration numbers
listed here, the sum of the odd digits is equal to the sum of the even digits in the number. He
termed the numbers that satisfy this property as Probable Topper Numbers.
Write a program to find whether a given number is a probable topper number or not.
Input Format:
Input consists of a single integer.
Output Format:
Output consists of a single line. Refer sample output for details.
Sample Input 1:
143
Sample Output 1:
yes
Sample Input 2:
344
Sample Output 2:
no
Solution:
using System;
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int a = n, osum = 0, esum = 0, d;
while (a > 0)
{
d = a % 10;
if (d % 2 == 0)
esum = esum + d;
else
osum = osum + d;
a = a / 10;
}
if (esum == osum)
Console.WriteLine("yes");
else
Console.WriteLine("no");
}
}
Q3. Write a program to left rotate an array by n position.
[Left Rotate the given array by 1 for N times. In real left rotation is shifting of array elements to
one position left and copying first element to last.]
Sample input:
Enter number of elements in array
10
Enter elements in array:
10
20
30
40
50
60
70
80
90
100
Enter times to rotate
3
Sample output:
40 50 60 70 90 100 10 20 30
Business Rule:
If the size of array is <10 or more than 50 print “Invalid Size” and terminate the program.
If any element of array is –ve print “Invalid data” and terminate the program.
If the number of rotations is <2 or >9 print “Invalid rotation” and terminate the program
If any duplicate element is present in array print “Invalid Array” and terminate the program
Solution:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number of elements in array:");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter elements in array:");
int i,j;
int[] a = new int[100];
for (i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Enter times to rotate:");
int tim = int.Parse(Console.ReadLine());

for (i = 0; i < tim; i++)


{
int temp = a[0];
for (j = 0; j < n - 1; j++)
a[j] = a[j + 1];

a[j] = temp;
}
Console.WriteLine();
for (int k = 0; k < n; k++)
{
Console.Write(a[k] + " ");
}
}
}

Q4. Write a program that generates and prints all possible cards from a standard deck of 52
cards. The cards should be printed using the classical notation (like 5♠, A♥, 9♣ and K♦). The
card faces should start from 2 to A. Print each card face in its four possible suits: clubs,
diamonds, hearts and spade

Sample output:

Solution:
using System;
class Program
{
static void Main(string[] args)
{

char club = '♣';


char spade = '♦';
char diamond = '♥';
char heart = '♠';

string card;

for (int i = 2; i <= 13; i++)


{
card = i.ToString();

switch (i)
{
case 10:
card = "J";
break;
case 11:
card = "Q";
break;
case 12:
card = "K";
break;
case 13:
card = "A";
break;

Console.Write(card+club+" ");
Console.Write(card+spade+" ");
Console.Write(card+diamond+" ");
Console.Write(card+heart+" ");
Console.WriteLine();
}

}
}

Note: Hold Alt key and press the numbers below:

♥ Heart
3


Diamond
4

Club
5

Spade
6
When you release alt the symbol will appear

Q5. Write a program to find sum of all prime numbers between 2 limits.
Business Rule:
If any of the limit is –ve print “Invalid” and terminate the program.
Sample Input output:
Enter lower limit: 10
Enter upper limit: 20
Sum of all prime numbers between 10 to 20 = 60

Solution
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter lower limit: ");
int stno = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter upepr limit: ");
int enno = Convert.ToInt32(Console.ReadLine());
int num,ctr,i,sum=0;
for(num = stno;num<=enno;num++)
{
ctr = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0){
ctr++;
break;
}
}

if (ctr == 0 && num != 1)


sum = sum + num;

}
Console.Write("Sum of all prime numbers between 10 to 20 = "+sum);
}
}

You might also like