0% found this document useful (0 votes)
335 views87 pages

Coding Written Test Programs

The document contains information about RN Reddy IT School including contact details, address and 10 logical programming problems with solutions in C#. The problems cover basics like calculating sum, multiplication and factorial of a range of numbers, checking if a number is prime, odd or even, printing tables and swapping numbers with and without a temporary variable. Solutions provided use basic C# concepts like loops, conditional statements, data types and input/output functions.

Uploaded by

his suvarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
335 views87 pages

Coding Written Test Programs

The document contains information about RN Reddy IT School including contact details, address and 10 logical programming problems with solutions in C#. The problems cover basics like calculating sum, multiplication and factorial of a range of numbers, checking if a number is prime, odd or even, printing tables and swapping numbers with and without a temporary variable. Solutions provided use basic C# concepts like loops, conditional statements, data types and input/output functions.

Uploaded by

his suvarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

RN Reddy IT School

Coding
Written
Test
by

Mr.R.N Reddy
RN Reddy IT School
Online/Classroom Training’s
For more details
Phone: +91 9866183094, 7989886175
E-Mail: [email protected]
Visit: www.rnreddyitschool.co
FB Page: - www.facebook.com/Reddyitschool 1
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

LOGICAL
PROGRAMS

2
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

1. Write a console program to print sum of given range?

input : Enter your range: 5

Output: Sum is : 15

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter your range: ");

int n = int.Parse(Console.ReadLine());

int sum = 0;

for (int i = 1; i <= n; i++)

sum = sum + i;

Console.WriteLine("Sum is: " + sum);

Console.ReadLine();

2.Write a console program to display the multiplication of given range?

Input: Enter your range: 5

Output: Mul is : 120

solution:
3
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur range:");

int n = int.Parse(Console.ReadLine());

int mul = 1;

for (int i = 1; i <= n; i++)

mul = mul * i;

Console.WriteLine("multiplication is: " + mul);

Console.ReadLine();

3. Write a console program to display factorial of given number?

input : Enter your range: 5

output : fac is : 120

solution:

using System;

namespace RNREDDYLogicalprograms

class Program
4
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

static void Main(string[] args)

Console.WriteLine("Enter ur range:");

int n = int.Parse(Console.ReadLine());

int fac = 1;

for (int i = n; i >= 1; i--)

fac = fac * i;

Console.WriteLine("factorial is: " + fac);

Console.ReadLine();

4.Write a console program to swap two numbers and print swapped Numbers?

Input: Enter first number: 10

Enter Second number : 15

output: first number is :15

Second number is :10

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)


5
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("Enter first number:");

int a = int.Parse(Console.ReadLine());

Console.WriteLine("Enter second number:");

int b = int.Parse(Console.ReadLine());

int temp = a;

a = b;

b = temp;

Console.WriteLine("First number is: " + a);

Console.WriteLine("Second number is: " + b);

Console.ReadLine();

5.Implement above swapping without third variable.

Input: Enter first number: 10

Enter Second number : 15

output: first number is :15

Second number is :10

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

{
6
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("Enter first number: ");

int a = int.Parse(Console.ReadLine());

Console.WriteLine("Enter second number:");

int b = int.Parse(Console.ReadLine());

a = a + b;

b = a - b;

a = a - b;

Console.WriteLine("First number is: " + a);

Console.WriteLine("Second number is: " + b);

Console.ReadLine();

6.Write a console program to print 5 table based on input from the user.

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter Ur number");

int n = int.Parse(Console.ReadLine());

Console.WriteLine("\n" + n + " Table is : \n\n");

for (int i = 1; i < 11; i++)

{
7
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine(n + " * " + i + " = " + n * i + "\n");

Console.ReadLine();

output:

Enter Ur number

5 Table is :

5*1=5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

7.Write a console program to check weather given number is prime no or not?

ip: Enter your number: 5

op: It is prime number

solution:

using System;

namespace RNREDDYLogicalprograms
8
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

int ctr = 0;

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

if (n % i == 0)

ctr++;

break;

if (ctr == 0)

Console.WriteLine("It is a Prime number");

else

Console.WriteLine("Not a Prime number");

Console.ReadLine();

}
9
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

8.Write a console program to check weather given number is odd no or not?

ip: Enter your number: 3

Op: Its is odd number

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

if (n % 2 == 1)

Console.WriteLine("It is a Odd number");

else

Console.WriteLine("it is not a Odd number");

Console.ReadLine();

9.Write a console program to check weather given number is even no or not?


10
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

ip: Enter your number: 2

Op: It is a even number

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

if (n % 2 == 0)

Console.WriteLine("It is a Even number");

else

Console.WriteLine("it is not a even number");

Console.ReadLine();

10.Write a console program to check weather given number is even no’s, sum of even
numbers,

no of even numbers within given range?

11
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

ip: Enter ur range: 10

Op: List of even numbers are:

10

Sum is : 30

No of even numbers are: 5

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int sum = 0, count = 0;

Console.WriteLine("Enter Ur range:");

int n = int.Parse(Console.ReadLine());

Console.WriteLine("List of even numbers are:");

for (int i = 1; i <= n; i++)

if (i % 2 == 0)

Console.WriteLine(i);

sum = sum + i;
12
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

count++;

Console.WriteLine("Sum is: " + sum);

Console.WriteLine("No of even numbers are: " + count);

Console.ReadLine();

11.Write a console program to check weather given number is even no’s, sum of even
numbers, no of even numbers within given range?

ip: Enter ur range: 5

Op: List of even numbers are:

Sum is : 9

No of even numbers are: 3

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int sum = 0, count = 0;

13
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("Enter Ur range:");

int n = int.Parse(Console.ReadLine());

Console.WriteLine("List of odd numbers are:");

for (int i = 1; i <= n; i++)

if (i % 2 != 0)

Console.WriteLine(i);

sum = sum + i;

count++;

Console.WriteLine("Sum is: " + sum);

Console.WriteLine("No of odd numbers are: " + count);

Console.ReadLine();

12.Write a console program to display sum of squares of up to given range.

Ip: Enter ur range: 5

Op: sum is : 55

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

{
14
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

int sum = 0;

for (int i = 1; i <= n; i++)

int sqr = i * i;

sum = sum + sqr;

Console.WriteLine("Sum is:" + sum);

Console.ReadLine();

13.Write a console program to display sum of squares of factorials of up to given


range.

Ip: Enter ur range: 5

Op: sum is : 153

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

15
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

int sum = 0; int fac = 1;

for (int i = n; i >= 1; i--)

for (int j = i; j>=1; j--)

fac = fac * j;

sum = sum + fac;

fac = 1;

Console.WriteLine("Sum is:" + sum);

Console.ReadLine();

14.Write a console program to print Fibonacci series up to given range.

Ip : Enter ur range: 15

Op: 0 1 1 2 3 5 8 13

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)


16
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

int a = 0, b = 1, c = 0;

int sum = 0, count = 0;

Console.Write("Enter ur range: ");

int n = int.Parse(Console.ReadLine());

Console.Write("Fibonacci Series: ");

while (a <= n)

Console.Write(a + " ");

c = a + b;

a = b;

b = c;

Console.ReadLine();

15.Write a console program to print Fibonacci series up to given range and display
sum of Fibonacci series and no of Fibonacci series up to given range.

ip: Enter ur range: 10

Op: 0 1 1 2 3 5 8

sum is: 33

no of numbers are: 7

solution:

using System;

namespace RNREDDYLogicalprograms

17
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

class Program

static void Main(string[] args)

int a = 0, b = 1, c = 0;

int sum = 0, count = 0;

Console.Write("Enter ur range: ");

int n = int.Parse(Console.ReadLine());

Console.Write("Fibonacci Series: ");

while (a <= n)

Console.Write(a + " ");

c = a + b;

a = b;

b = c;

sum = sum + a;

count++;

Console.WriteLine("\n" + "Sum is:" + sum);

Console.WriteLine("No of numbers are:" + count);

Console.ReadLine();

16. WAP To display palindrome numbers up to n... !

Enter Number : 50

Palindrome numbers upto given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44


18
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int n, i, k, r, temp;

Console.Write("Enter Number : ");

n = int.Parse(Console.ReadLine());

Console.WriteLine("Palindrome numbers upto given range are:\n");

for (i = 1; i <= n; i++)

temp = i; r = 0;

while (temp != 0)

k = temp % 10;

r = r * 10 + k;

temp = temp / 10;

if (r == i)

Console.Write(i + " ");

Console.ReadLine();
19
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

17.WCP to check whether given number is Armstrong number or not.

Enter the Number= 371

Armstrong Number.

Enter the Number= 342

Not Armstrong Number.

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int n, r, sum = 0, temp;

Console.Write("Enter the Number= ");

n= int.Parse(Console.ReadLine());

temp=n;

while (n>0)

r=n%10;

sum=sum+(r*r*r);

n=n/10;

if (temp==sum)

Console.Write("Armstrong Number.");
20
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

else

Console.Write("Not Armstrong Number.");

Console.ReadLine();

18.Write a console program to accept a number and print in reverse.

ip: Enter ur number:321

Op: reverse number is : 123

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int num = int.Parse(Console.ReadLine());

int rem = 0;

int rev = 0;

while (num != 0)

rem = num % 10;

rev = rev * 10 + rem;

num = num / 10;


21
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("Reverse number is:" + rev);

Console.ReadLine();

19.Write a console program to check weather given number is a palindrome or not?

input: Enter ur number: 121

op: It is a palindrome

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur number:");

int n = int.Parse(Console.ReadLine());

int rem = 0, revnum = 0, temp = n;

while (n != 0)

rem = n % 10;

revnum = revnum * 10 + rem;

n = n / 10;

}
22
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

if (temp == revnum)

Console.WriteLine("It is a palindrome");

else

Console.WriteLine("It is not a palindrome");

Console.ReadLine();

20.Write a program which takes an integer number and adds all the digit in the
number, it should return a single digit in the end.

input : 5643

Hint: 5+6+4+3=18; 1+8=9

output: 9

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("Enter ur Number:");

23
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

int n = int.Parse(Console.ReadLine());

int sum = 0;

while (n > 9)

sum = 0;

while (n > 0)

int rem;

rem = n % 10;

sum = sum + rem;

n = n / 10;

n = sum;

Console.WriteLine("Output Is: "+sum);

Console.ReadLine();

21.Write a program to convert decimal to binary number

input: Enter a number : 123

output : 1111011

solution:

using System;

namespace RNREDDYLogicalprograms

class Program
24
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

static void Main(string[] args)

int num;

Console.Write("Enter a Number : ");

num = int.Parse(Console.ReadLine());

int quot;

string rem = "";

while (num >= 1)

quot = num / 2;

rem += (num % 2).ToString();

num = quot;

string bin = "";

for (int i = rem.Length - 1; i >= 0; i--)

bin = bin + rem[i];

Console.WriteLine("The Binary format for given number is {0}", bin);

Console.ReadLine();

22.WAP to display n number of prime numbers.

Enter the number of prime numbers required: 10

Prime numbers are:


25
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

2 3 5 7 11 13 17 19 23 29

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter the number of prime numbers required: ");

int n = int.Parse(Console.ReadLine());

int primecount = 0, temp = n;

Console.WriteLine("Prime numbers are:");

for (int i = 1; i <= n; i++)

int count = 0;

for (int j = 1; j <= i; j++)

if (i % j == 0)

count++;

if (count == 2)

Console.Write(i+" ");

primecount++;
26
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

n++;

if (primecount == temp)

break;

Console.ReadLine();

23.WAP to display Min and Max number in the given number

Enter number : 123456789

Maximum Number = 9

Minimum Number = 1

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter number : ");

int num = int.Parse(Console.ReadLine());

int max = num % 10, min = num % 10;

while (num > 0)

{
27
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

int a = num % 10; //see this

if (a > max)

max = a;

if (a < min)

min = a;

num = num / 10;

Console.WriteLine("Maximum Number =" + max);

Console.WriteLine("Minimum Number =" + min);

Console.ReadLine();

24.WAP to display Sum of Squares of digits

Enter Number : 1234

sum of square digits is : 30

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter Number : ");

int x = Int32.Parse(Console.ReadLine());

int sum = 0, r;
28
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

while (x != 0)

r = x % 10;

sum = sum + (r * r);

x = x / 10;

Console.WriteLine(" sum of square digits is :" + sum);

Console.ReadLine();

25.WAP to Check Whether the Entered Number is a Perfect Number or Not

Enter the Number :6

6 is a perfect number

solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int number, sum = 0, n;

Console.Write("Enter the Number :");

number = int.Parse(Console.ReadLine());

n = number;

for (int i = 1; i < number; i++)


29
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

if (number % i == 0)

sum = sum + i;

if (sum == n)

Console.WriteLine("\n"+n + " is a perfect number");

else

Console.WriteLine(n + " is not a perfect number");

Console.ReadLine();

30
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

PATTERN
PROGRAMS
(NUMBER PATTERNS &
SYMBOL PATTERNS)

31
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

1.Write a console program to print like below?

output:

22

333

4444

55555

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

for (int i = 1; i <= 5; i++)

for (int j = 1; j <= i; j++)

Console.Write(i + " ");

Console.WriteLine();

Console.ReadLine();

32
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

2.Write a console program to print like below?

output:

12

123

1234

12345

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

for (int i = 1; i <= 5; i++)

for (int j = 1; j <= i; j++)

Console.Write(j + " ");

Console.WriteLine();

Console.ReadLine();

}
33
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

3.Write a console program to print like below?

output:

55555

4444

333

22

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

for (int i = 5; i >= 1; i--)

for (int j = 1; j <= i; j++)

Console.Write(i + " ");

Console.WriteLine("\n");

Console.ReadLine();

}
34
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

4.Write a console program to print like below?

output:

12345

1234

123

12

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

for (int i = 5; i >= 1; i--)

for (int j = 1; j <= i; j++)

Console.Write(j + " ");

Console.WriteLine("\n");

Console.ReadLine();

}
35
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

5.Write a console program to print like bellow based on given range?

Ip: Enter ur range: 5

OP:

12

123

1234

12345

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("enter ur range");

int r = int.Parse(Console.ReadLine());

for (int i = 1; i <= r; i++)

for (int j = 1; j <= i; j++)

Console.Write(j + " ");

Console.WriteLine();
36
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.ReadLine();

6.Write a console program to print like bellow based on given range?

input: Enter your range: 5

output:

22

333

4444

55555

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("enter ur range");

int r = int.Parse(Console.ReadLine());

for (int i = 1; i <= r; i++)

for (int j = 1; j <= i; j++)

{
37
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write(i + " ");

Console.WriteLine();

Console.ReadLine();

7.Write a console program to print like below?

op:

23

456

7 8 9 10

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int k = 1;

for (int i = 1; i <= 4; i++)

for (int j = 1; j <= i; j++)

{
38
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write(k + " ");

k++;

Console.WriteLine("\n");

Console.ReadLine();

8.Write a console program to print like below based on given range?

input: Enter your range: 21

output:

23

456

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("enter ur range");
39
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

int r = int.Parse(Console.ReadLine());

int k = 1;

for (int i = 1; i <= r; i++)

for (int j = 1; j <= i; j++)

if (k > r)

break;

Console.Write(k + " ");

k++;

Console.WriteLine();

if (k > r)

break;

Console.ReadLine();

9.Write a console program to print like bellow based on given range?

input: Enter your range: 21

output:

21 20 19 18 17 16

15 14 13 12 11

10 9 8 7

654

32

1
40
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Solution:

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.WriteLine("enter ur range");

int n= int.Parse(Console.ReadLine());

int k = n;

for (int i = 6; i>=1; i--)

for (int j = i; j >=1; j--)

Console.Write(k + " ");

k--;

Console.WriteLine();

if (k == 0)

break;

Console.ReadLine();

41
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

10.Write a console program to print stars like below.

Output:

**

***

****

*****

******

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

for (x = 1; x <= 6; x++)

for (y = 1; y <= x; y++)

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

}
42
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

11.Write a console program to print stars like below.

Output:

**

***

****

*****

******

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y, z;

for (x = 6; x >= 1; x--)

for (y = 1; y < x; y++)

Console.Write(" ");

for (z = 6; z >= x; z--)

{
43
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

12.Write a console program to print stars like below.

Output:

*****

****

***

**

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

for (x = 5; x >= 1; x--)

for (y = 1; y <= x; y++)


44
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

13.Write a console program to print stars like below.

Output:

*****

****

***

**

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y, z;

for (x = 5; x >= 1; x--)

{
45
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

for (y = 5; y > x; y--)

Console.Write(" ");

for (z = 1; z <= x; z++)

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

14.Write a console program to print stars like below.

Output:

***

*****

*******

46
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y, z;

for (x = 1; x <= 5; x++)

for (y = x; y < 5; y++)

Console.Write(" ");

for (z = 1; z < (x * 2); z++)

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

47
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

15.Write a console program to print stars like below.

Output:

*********

*******

*****

***

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y, z;

for (x = 5; x >= 1; x--)

for (y = 5; y > x; y--)

Console.Write(" ");

for (z = 1; z < (x * 2); z++)


48
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("*");

Console.WriteLine();

Console.ReadLine();

16.Write a console program to print stars like below.

Output:

**

* *

* *

*********

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

for (x = 1; x <= 5; x++)

{
49
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

for (y = x; y < 5; y++)

Console.Write(" ");

for (y = 1; y <= (2 * x - 1); y++)

if (x == 5 || y == 1 || y == (2 * x - 1))

Console.Write("*");

else

Console.Write(" ");

Console.WriteLine();

Console.ReadLine();

50
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

17..Write a console program to print characters like below.

Output:

BB

CCC

DDDD

EEEEE

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

int z = 5;

for (x = 1; x <= z; x++)

for (y = 1; y <= x; y++)

Console.Write((char)(x + 64));

Console.WriteLine("");

Console.ReadLine();

51
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

18.Write a console program to print characters like below.

Output:

ABCDE

BCDE

CDE

DE

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

int z = 5;

for (x = 1; x <= z; x++)

for (y = x; y <= z; y++)

Console.Write((char)(y + 64));

Console.WriteLine();
52
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.ReadLine();

19.Write a console program to print characters like below.

Output:

ABA

ABCBA

ABCDCBA

ABCDEDCBA

ABCDEFEDCBA

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

int x, y;

int z = 6;

for (x = 1; x <= z; x++)

for (y = 1; y <= z - x; y++)

{
53
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write(" ");

for (y = 1; y <= x; y++)

Console.Write((char)(y + 64));

for (y = x - 1; y >= 1; y--)

Console.Write((char)(y + 64));

Console.WriteLine();

Console.ReadLine();

20.WAP to Print a BinaryTriangle

Enter the Number of Rows : 5

01

010

1010

10101

Solution:-

using System;

namespace RNREDDYLogicalprograms

{
54
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

class Program

static void Main(string[] args)

int p, lastInt = 0, input;

Console.WriteLine("Enter the Number of Rows : ");

input = int.Parse(Console.ReadLine());

for (int i = 1; i <= input; i++)

for (p = 1; p <= i; p++)

if (lastInt == 1)

Console.Write("0");

lastInt = 0;

else if (lastInt == 0)

Console.Write("1");

lastInt = 1;

Console.Write("\n");

Console.ReadLine();

}
55
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

21. WAP to display below pattern

Output:

45

345

2345

12345

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter the row no.");

int input = int.Parse(Console.ReadLine());

for (int row = 0; row < input; row++)

for (int col = input - row; col <= input; col++)

{ Console.Write(col); }

Console.Write("\n");

Console.ReadLine();

}
56
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

22.. WAP to display vowels pattern.

Enter Number : 5

eio

uaeio

uaeioua

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

char[] arr = { 'a', 'e', 'i', 'o', 'u' };

int index = 0;

Console.Write("Enter Number.: ");

int input = int.Parse(Console.ReadLine());

for (int row = 1; row <= input; row++)

for (int col = 1; col <= input + (row - 1); col++)

if (col <= input - row)

Console.Write(" ");

else

{
57
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write(arr[index]);

index++;

if (index == input)

index = 0;

Console.WriteLine();

Console.ReadLine();

23.WAP to print the box pattern.

Input:5

Output:

1 2 3 4 5

16 6

15 7

14 8

13 12 11 10 9

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)


58
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("Enter a no.:");

int n = int.Parse(Console.ReadLine());

int r = n * 4 - 4, count = 1;

for (int row = 1; row <= n; row++)

for (int col = 1; col <= n; col++)

if (row == 1 || col == n)

if (col <= 9)

Console.Write(count + " ");//two spaces

else

Console.Write(count + " ");

count++;

else if (col == 1 || row == n)

if (r <= 9)

Console.Write(r + " ");//two spaces

else

Console.Write(r + " ");

r--;

else

Console.Write(" ");//three spaces

}
59
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine(); //next line

Console.WriteLine(); //next lin

Console.ReadLine();

24.WAP to print the following pattern.

Input: 5

output:

123454321

12344321

123 321

12 21

1 1

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter a number:");

int input = int.Parse(Console.ReadLine());

Console.WriteLine("\nPattern: \n");
60
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

for (int row = input; row >= 1; row--)

for (int col = 1; col <= input; col++)

if (row >= col)

Console.Write(col);

else

Console.Write(" ");

for (int col = input - 1; col >= 1; col--)

if (row >= col)

Console.Write(col);

else

Console.Write(" ");

Console.WriteLine();

Console.ReadLine();

25.WAP to write Pascal Triangle Logical Pattern

Enter a No.:

11

121

1331
61
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Solution:-

using System;

namespace RNREDDYLogicalprograms

class Program

static void Main(string[] args)

Console.Write("Enter a No.:");

int input = int.Parse(Console.ReadLine());

for (int row = 0; row < input; row++)

for (int s = row; s < input - 1; s++)

Console.Write(" ");

int count = 1;

for (int col = 0; col <= row; col++)

Console.Write(count + " ");

count = count * (row - col) / (col + 1);

Console.WriteLine();

Console.ReadLine();

62
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

STRING
HANDLING
PROGRAMS

63
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

1.Write a Program To Find the Longest word in a given string

Input:Rama is a good boy

Output: good

Solution:

internal class Program

static void Main(string[] args)

string str = "Rama is a good boy";

string[] words = str.Split(new[] { " " }, StringSplitOptions.None);

string word = " ";

int count = 0;

foreach (string s in words)

word = s;

count = s.Length;

Console.WriteLine(word);

Console.ReadLine();

2.Write a Program To reverse each word in a given sentence.

Input:Welcome To Rnreddy IT School

Output:emocLeW oT yddernR TI loohcS

Solution:

internal class Program

static void Main(string[] args)


64
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("enter your string");

string str = Console.ReadLine();

string strrev = "";

foreach (var word in str.Split(' '))

string temp = "";

foreach (var ch in word.ToCharArray())

temp = ch + temp;

strrev = strrev + temp + " ";

Console.WriteLine(strrev);

Console.ReadLine();

3.Program to count number of words in a string.

input: Enter a string: Rn Reddy It School

output: 4

solution:

class Program

static void Main(string[] args)

string str;

int wrd, l;

Console.Write("\n\nCount the total number of words in a string :\n");


65
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("------------------------------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

l = 0;

wrd = 1;

/* loop till end of string */

while (l <= str.Length - 1)

/* check whether the current character is white space or new line or

tab character*/

if (str[l] == ' ' || str[l] == '\n' || str[l] == '\t')

wrd++;

l++;

Console.Write("Total number of words in the string is : {0}\n", wrd);

Console.ReadLine();

4.Write a function that given two string s1 and s2, it will return s1 minus s2 and it has
to use same location without using pre defined functions.

Input:enter string s1

abcd8

enter string s2

Output:acd8

66
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Solution:

class Program

static void Main(string[] args)

Console.WriteLine("Enter string1:");

string s1 = Console.ReadLine();

Console.WriteLine("Enter string s2:");

string s2 = Console.ReadLine();

string s3 = s1.Replace(s2, "");

Console.WriteLine(" string1 Minus String2: "+s3);

Console.ReadLine();

5.Given a string, write a program to find the character highest frequency

Input:aaabbcccc

Output:c

Solution:

class Program

static void Main(string[] args)

string str;

int[] ch_fre = new int[255];

int i = 0, max, l;

int ascii;

Console.Write("\n\nFind maximum occurring character in a string :\n");


67
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write("--------------------------------------------------\n");

Console.Write("Input the string : ");

str = Console.ReadLine();

l = str.Length;

for (i = 0; i < 255; i++) //Set frequency of all characters to 0

ch_fre[i] = 0;

/* Read for frequency of each characters */

i = 0;

while (i < l)

ascii = (int)str[i];

ch_fre[ascii] += 1;

i++;

// Console.Write("{0} ",(char)65);

max = 0;

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

if (i != 32)

if (ch_fre[i] > ch_fre[max])

max = i;

Console.Write("The Highest frequency of character '{0}' is appearing for number


68
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

of times : {1} \n\n", (char)max, ch_fre[max]);

Console.ReadLine();

6.Write a program to print frequency of each character in a given string when count a
character like below

input: Enter a string: rn reddy it school

output: r-2 n-1 e-1 d-2 y-1 s-1 t-1 i-1 c-1 h-1 o-2 l-1

solution:

class Program

static void Main(string[] args)

Console.Write("Enter String.");

string input = Console.ReadLine();

while (input.Length > 0)

Console.Write(input[0] + "-");

int count = 0;

for (int j = 0; j < input.Length; j++)

if (input[0] == input[j])

count++;

Console.Write(count + " ");

input = replace(input, input[0]);

69
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.ReadLine();

7.Write a Program to copy ‘n’ number of characters from one string to another at

position ‘p’

Enter string 1: IT hub

Enter String 2: Nexgile Solutions

N: 2

P: 8

Output: Nexgile IT Solutions.

Solution:

class Program

static void Main(string[] args)

Console.Write("Enter String 1\t: ");

string s1 = Console.ReadLine();

Console.Write("\nEnter String 2\t: ");

string s2 = Console.ReadLine();

char[] c = s2.ToCharArray();

Console.WriteLine("\n\nString 1\t:\t{0}\nString2 \t:\t{1}\n\n", s1,s2);

Console.WriteLine("Enter Number of Characters\t:\t");

int noc = int.Parse(Console.ReadLine());

Console.WriteLine("\nEnter position\t:\t \n");

int pos = int.Parse(Console.ReadLine());

for (int i = 0; i < s2.Length; i++)


70
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

if (i == pos - 1)

Console.Write(" ");

for (int j = 0; j < noc; j++)

Console.Write(s1[j]);

Console.Write(" ");

Console.Write(c[i]);

Console.ReadLine();

8.Swap neighbour char in string, for Example string “TAPAN” would be ATAPN

Input:TAPAN

Output:ATAPN

Solution:

class Program

static void Main(string[] args)

Console.WriteLine("Enter a string:");

string s = Console.ReadLine();

char[] c = s.ToCharArray();

int n;

char temp;
71
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

if (c.Length % 2 == 0)

n = c.Length;

else

n = c.Length - 1;

for (int i = 0; i < n; i = i + 2)

temp = c[i];

c[i] = c[i + 1];

c[i + 1] = temp;

Console.Write(c[i]);

Console.Write(c[i + 1]);

if (n < c.Length)

Console.WriteLine(c[c.Length - 1]);

Console.ReadLine();

72
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

9.How will you find the longest palindrome in a String? i.e., if the string is XMADAMY
your palindrome in a String? i.e., if the string is XMADAMY your code should print
MADAM.

Input:XMADAMY

Output:MADAM

Solution:

class Program

private static string GetLongestPalindrome(string input)

int rightIndex = 0, leftIndex = 0;

List<string> paliList = new List<string>();

string currentPalindrome = string.Empty;

string longestPalindrome = string.Empty;

for (int currentIndex = 1; currentIndex < input.Length - 1; currentIndex++)

leftIndex = currentIndex - 1;

rightIndex = currentIndex + 1;

while (leftIndex >= 0 && rightIndex < input.Length)

if (input[leftIndex] != input[rightIndex])

break;

currentPalindrome = input.Substring(leftIndex, rightIndex - leftIndex + 1);

paliList.Add(currentPalindrome);

leftIndex--;

73
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

rightIndex++;

var x = (from c in paliList

select c).OrderByDescending(w => w.Length).First();

return x;

static void Main(string[] args)

Console.WriteLine("Enter ur string: ");

var str = Console.ReadLine();

var longestPalindrome = GetLongestPalindrome(str);

Console.WriteLine(longestPalindrome);

Console.ReadLine();

10.WAP to Display no.of strings,vowels,consonants in the given string

Here is the output of the C# Program:

Enter String : seshu sanju

Vowels : 4

Consonants is : 6

No of Words is : 2

Solution:

internal class Program

static void Main(string[] args)

Console.Write("Enter String : ");


74
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

string str = Console.ReadLine();

int vowels = 0;

int consonants = 0;

int words = 1;

char[] chr = str.ToCharArray();

for (int i = 0; i < chr.Length; i++)

if (chr[i] == 32)

words++;

continue;

else

if (chr[i] == 'a' || chr[i] == 'e' || chr[i] == 'i' || chr[i] == 'o' || chr[i] == 'u' ||

chr[i] == 'A' || chr[i] == 'E' || chr[i] == 'I' || chr[i] == 'O' || chr[i] == 'U')

vowels++;

else

consonants++;

Console.WriteLine("Vowels is : " + vowels);

Console.WriteLine("Consonants is : " + consonants);


75
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("No of Words is : " + words);

Console.ReadLine();

11.Remove duplicate characters in a String?


input:csharpstar
output:csharpt

solution:
using System;
class Program
{
static void Main()
{
string value1 = RemoveDuplicateChars("Csharpstar");

Console.WriteLine(value1);
}
static string RemoveDuplicateChars(string key)
{
string table = "";
string result = "";
foreach (char value in key)
{
if (table.IndexOf(value) == -1)
{
table += value;
result += value;
}
}
return result;
}
}

12.How to check if two Strings are anagrams of each other?


example:secure-->rescue

solution:
using System;
namespace Anagram
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter first word:");
76
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

string word1 = Console.ReadLine();


Console.Write("Enter second word:");
string word2 = Console.ReadLine();

char[] char1 = word1.ToLower().ToCharArray();


char[] char2 = word2.ToLower().ToCharArray();

Array.Sort(char1);
Array.Sort(char2);

string NewWord1 = new string(char1);


string NewWord2 = new string(char2);

if (NewWord1 == NewWord2)
{
Console.WriteLine("Yes! Words \"{0}\" and \"{1}\" are Anagrams", word1, word2);
}
else
{
Console.WriteLine("No! Words \"{0}\" and \"{1}\" are not Anagrams", word1,
word2);
}
Console.ReadLine();
}
}
}

13.How to check if String is Palindrome or not?


input:madam
output: it is a palindrome

solution:
using System;
namespace consoleapp4
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter ur string");
string s=Console.ReadLine();
string rev = null;
for(int i = s.Length-1;i>=0 ; i--)
{
rev=rev+s[i];
}
Console.WriteLine("reverse string is:"+rev);
if (s==rev)
Console.WriteLine("it is a palindrome");
else
77
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.WriteLine("it is not a palindrome");


Console.ReadLine();
}
}
}

14.How to determine if the string has all unique characters?

solution:
using System;
public class GFG
{
static bool uniqueCharacters(String str)
{
for (int i = 0; i < str.Length; i++)
for (int j = i + 1; j < str.Length; j++)
if (str[i] == str[j])
return false;
return true;
}
public static void Main()
{
Console.WriteLine("enter ur input");
string input = Console.ReadLine();

if (uniqueCharacters(input) == true)
Console.WriteLine("The String " + input
+ " has all unique characters");
else
Console.WriteLine("The String " + input
+ " has duplicate characters");
Console.ReadLine();
}
}

15.Find all substring in a string?


output:abc
input:a
b
c

ab
bc
abc
solution:

using System;
class GFG
{
static void find_substrings(string input_string)
78
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

{
int j = 0;
int i = 0;
for (i = 1; i <= input_string.Length; i++)
{
for (j = 0; j <= input_string.Length - i; j++)
{
Console.WriteLine(input_string.Substring(j, i));
}
}
}
public static void Main()
{
string input_string;

Console.Write("Enter String : ");


Console.Write("\n");

input_string = Console.ReadLine();

find_substrings(input_string);
Console.ReadLine();
}
}

16.How to print first non repeated character from String?


input:rnreddyitschool
op: first non-repeating character is n

solution:
using System;

public static class GFG


{
public static void FirstNonRepeat(string s)
{

for (int i = 0; i < s.Length; i++)


{

if (s.IndexOf(s[i], s.IndexOf(s[i]) + 1)
== -1)
{
Console.Write(
"First non-repeating character is "
+ s[i]);
break;
}
}
return;
79
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

}
internal static void Main()
{
Console.WriteLine("enter ur string");
string s = Console.ReadLine();
FirstNonRepeat(s);
Console.ReadLine();
}
}
17.How to remove special characters from String?
input:$c!sh$arp&st%ar
output:csharpstar

solution:
using System;
public class Program
{
public static void Main(string[] args)
{
string str = "$c!sh$arp&st%ar";
Console.WriteLine(RemoveSpecialChars(str));
Console.ReadLine();
}
public static string RemoveSpecialChars(string str)
{
// Create a string array and add the special characters you want to remove
string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";",
"_", "(", ")", ":", "|", "[", "]" };
//Iterate the number of times based on the String array length.
for (int i = 0; i < chars.Length; i++)
{
if (str.Contains(chars[i]))
{
str = str.Replace(chars[i], "");
}
}
return str;

}
}

18.How to reverse String in C# using Iteration ?


input:rama
output:amar

solution:
using System;
class GFG
{
static void reverseStr(String str)
80
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

{
int n = str.Length;
char[] ch = str.ToCharArray();
char temp;

for (int i = 0, j = n-1; i<j; i++, j--)


{
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
}
Console.WriteLine(ch);
}
public static void Main(String[] args)
{
Console.WriteLine("enter ur string");
String s = Console.ReadLine();
reverseStr(s);
Console.ReadLine();
}
}

19.join two strings?


output:string str1: C#
string str2: Programming
Joined string: C# Programming

Solution:
using System.Text;using System;

using System.Threading.Tasks;

namespace ConsoleApp15
{
internal class Program
{
static void Main(string[] args)
{

string str1 = "C# ";


Console.WriteLine("string str1: " + str1);

// create string
string str2 = "Programming";
Console.WriteLine("string str2: " + str2);

// join two strings


string joinedString = string.Concat(str1, str2);
Console.WriteLine("Joined string: " + joinedString);
81
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.ReadLine();
}
}
}

20.compare two strings are equal are not?

output:string str1 and str2 are equal: True


string str1 and str3 are equal: False
solution:

using System;
namespace CsharpString

public static void Main(string [] args)

// create string
string str1 = "C# Programming";
string str2 = "C# Programming";
string str3 = "Programiz";

// compare str1 and str2


Boolean result1 = str1.Equals(str2);
Console.WriteLine("string str1 and str2 are equal: " + result1);

//compare str1 and str3


Boolean result2 = str1.Equals(str3);
Console.WriteLine("string str1 and str3 are equal: " + result2);

Console.ReadLine();
}
}
}
21.write the code for this series?
input:aaabbcccc
output:a3b2c4

solution:
class program
{
static void main(string[]args)
{
Console.Write("enter ur string:");
string s=Console.ReadLine();
char[]ch=s.TocharArray();
Con22sole.Write(" ");
for(int i=0;i<ch.length;i++);
{
int count=1;
82
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

if(ch[i]!='@';)
{
Console.Write(ch[i]);
for(int j=i+1;j<ch.length;j++)
{
if(s[i]==s[j])
{
count++;ch[j]='@';
}
}
Console.Write(count);
}
}
Console.ReadLine();
}
}
}
22.string in numbers pattern?
output:enter ur srting
interview
i
in
int
inte
inter
interv
intervi
intervie
interview
solution:
namespace ConsoleApp6
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter ur srting");
string str = Console.ReadLine();
for(int i=0;i<str.Length; i++)
{
for(int j=0;j<=i; j++)
{
Console.Write(str[j]+" ");

}
Console.WriteLine();
}
Console.ReadLine();
}
}
83
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

23.write length of the string?


output:string: C# Programming
Length: 14

Solution:
namespace ConsoleApp19
{
internal class Program
{
static void Main(string[] args)
{
// create string
string str = "C# Programming";
Console.WriteLine("string: " + str);

// get length of str


int length = str.Length;
Console.WriteLine("Length: " + length);

Console.ReadLine();

}
}
}
24.Write a Program to Sort a List of Names in Alphabetical Order

Input:Ram Rose Abs Edward Sita

Output:

Abs

Edward

Ram

Rose

Sita

Solution:

internal class Program

static void Main(string[] args)

List<string> names = new List<string>();


84
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

names.Add("Ram");

names.Add("Rose");

names.Add("Abs");

names.Add("Edward");

names.Add("Sita");

names.Sort();

Console.WriteLine("Output: \n");

foreach (string s in names)

Console.WriteLine(s);

Console.ReadLine();

25.check if string is rotated by two places?

input: amazon

output: mazano

solution:

internal class Program

static void Main(string[] args)

Console.Write("Please Enter a String\t: ");

string s = Console.ReadLine();

Console.WriteLine("After Changing Positions\n");

char[] c = s.ToCharArray(); char temp;

for (int i = 0; i < s.Length; i = i + 2)

if ((i + 2) >= s.Length && (s.Length % 2 != 0))

{
85
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

Console.Write(c[i]); break;

else if ((i + 2) >= s.Length && (s.Length % 2 != 0))

Console.Write(c[i] + " " + c[i + 1]);

break;

temp = c[i]; c[i] = c[i + 1];

c[i + 1] = temp;

Console.Write(c[i] + " " + c[i + 1] + " ");

Console.ReadLine();

86
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752
RN Reddy IT School

RN Reddy IT School
Online/Classroom Training’s

For more details contact


Cell: +91 9866183094, 7989886175
E-Mail:[email protected]
Website: www.rnreddyitschool.co
FaceBook Group: https://www.facebook.com/groups/rnreddytechsupports
FaceBook Page: https://www.facebook.com/groups/reddyitschool
Youtube Channel:- https://www.youtube.com/c/RNREDDYITSchool

87
ADDRESS: Flat no:102, KVR Enclave, above Bata showroom, left side of Sathyam theatre Ameerpet
Hyderabad-500016, Phone: 9866183094, 79898861752

You might also like