Logics: Create Solution For The Problem

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

Logics

PDLC:- Program Development Lifecycle


PDLC is an approach which is used to develop the programs fastly
and efficiently
1. Problem Definition
2. Creating the solution for the problem
3. Algorithm
4. Flowchart
5. PRS
6. Coding
7. Testing
Problem definition :- identify the problems
create solution for the problem
Algorithm:- Stepwise Refinement is called as Algorithm
1.problem:- consider a is 5 b is 3 print sum ,diff?
Algorithm:-
1. start
2. consider a is 5 b is 3
3. add a and b and store the result in c
4. subtract b from a and store the result in d
5. print c
6. print d
7. stop
Flowchart:- The Diagrammatical Representation of Algorithm is
Flowchart
PRS:- Program Requirement Specification
a. identify the variables and datatypes
a----->int
b----->int
c----->int
d----->int
b. identify the operators
+ , - , =
c. identify the operations
addition
subtraction
d. Expected I/P
a is 5
b is 3
e. Expected O/P
sum is 8
Diff is 2
6. coding
{
int a=5;
int b=3;
int c=a+b;
int d=a-b;
Console.WriteLine("sum is "+c);
Console.WriteLine("Diff is "+d);
}
2.problem:- consider eno is 101 ename is anil
basicsal is 20000 20% bsal is da 40% bsal is hra
display the tsal?
Algorithm:-
1. start
2. eno is 101
ename is anil
bsal is 20000
3. multiply 0.2 with bsal and store the result in da
4. multiply 0.4 with bsal and store the result in hra
5. add bsal with da and hra and store the result in tsal
6. print tsal
7. stop
PRS:-
1. identify the variables and datatypes
eno ----------->int
ename ----------> string
bsal ---------->int
da ------------>double
hra ------------>double
tsal ------------>double
2. identify the operators
= assignment
*
+
3. identify operations
calda
calhra
caltsal
4. expected i/p
eno is 101
ename is anil
bsal is 20000
5. expected o/p
tsal is 32000
coding:-
{
int eno=101;
string ename="anil";
double bsal=20000;
double da=0.2*bsal;
double hra=0.4*bsal;
double tsal=bsal+da+hra;
Console.WriteLine("Tsal is "+tsal);
}
3)consider a is 5
print square,cube?
Algorithm:-
1. start
2. a is 5
3. multiply a with a and store the result in square
4. multiply square with a and store the result in cube
5. print square
6. print cube
7. stop
PRS:-
1. variables and datatypes
a-->int
square--->int
cube ---->int
2. =,*
3.calSquare
calCube
4.expected i/p
a is 5
5.expected o/p
square is 25
cube is 125
Program
{
Int a=5;
Int square,Cube=0;
Square=a*a;
Cube=square*a;
Console.WriteLine(“Square is ”+square);
Console.WriteLine(“cube is ”+cube);
}
Operators
operators are used to perform operations on 2 or more operands
operand may be a value or variable
Different Types of Operators:-
1. Arthimetic Operators +,-,*,/,%
2. Relational Operators <,>,<=,>=,==,!=
3. Logical Operators &&,||
4. Assignment Operator =
5. increment and decrement operator ++,--
+ operator is called as Overloaded operator
it will perform addition operation between numbers and concadination
operation between characters
2+3=5 int+int=int
2+3.4=5.4 int+double=double
2+3.4f=5.4f int+float=float
"ab"+"cd"="abcd" string+string=string
"2"+"3"="23" string+string=string
2+"ab"="2ab" int+string=string
"ab"+2="ab2"
"ab"+2+3="ab23"

/ will gives us quotient


% will gives us remainder

if we want to identify the units place digit from a given no then we have to
use no%10
if we want to remove the units place digit from a given no then we have to
use no/10
Q) consider a is 1234
display units place digit?
Algorithm:-
1. start
2. consider a is 1234
3. modulo divide 1234 with 10 and store the result in r
4. print r
5. stop
PRS:-
1. variables and datatypes
a ----- > int
r -------> int
2. identify operators
=
%
3. identify operations
identify unitsplace digit
4. expected i/p
a is 1234
5. expected o/p
4
coding:-
{
int a=1234;
int r=a%10;
Console.WriteLine(r);
}
Q) consider a is 73
display the sum of the digits for a given 2 Digit no?
Algorithm:-
1. start
2. consider no is 73 r is 0 sum is 0
3. modulo divide no with 10 and store the result in r
4. add r to sum and store the result in sum
5. divide no with 10 and store the result in no
6. add sum to no and store in sum
7. print sum
8. stop
coding:-
{
int no=73; inrt r=0; int sum=0;
r=no%10;
sum=sum+r;
no=no/10;
sum=sum+no;
Console.WriteLine(sum);
}
Q)consider a is 5
b is 3
swap a and b
and display them?
int a=5; int b=3; int c=0;
c=a; a=b;
b=c;
Q)consider 500 days
Display years,months,weeks,days?
1year=365 days 500/365

1month=30 days
1 week =7 days
135 days left 135/30

15 days left 15/7

1 day
Q)consider 1 00 000 seconds
display days,hours,minutes,seconds?
1day=24 hrs 86400 seconds
1 hour=60 minutes 3600 seconds
1minute=60 seconds
Expected o/p:-
1 day 3 hours 46 minutes 40 seconds
Q)what is condition?
A condition is used to compare 2 values or variables
or Expressions and return a boolean value either true or false
conditional statements:-The statements that will gets executed based on the
condition
conditional operators:-
< lessthan
> greaterthan
== Equal
<= Lessthan equal
>= Greaterthan equal
!= NotEqual
conditional statements

if switch-case
only if
if-else
else if
multiple if
nested if
only if:- The statement will gets executed when the condition is true
if(true)
{

}
if-else:- it is used to check the condition and return a
Boolean value either true or false
if the condition is true then the statements that was written inside if will
gets executed otherwise the statements that was written inside else will gets
executed
if(true)
{
}
else
{
}
else if:-it is used to check multiple conditions and execute only one
condition
if()
{ }
else if()
{ }
else if()
{ }
multiple if:-it is used to check multiple conditions and execute multiple
conditions
if()
{}
if()
{}
if()
{}
nested if:- The inner condition will gets executed when
the outer condition is true
if() if() if() if() if()
{ { { {
if() if() if() if() if()
{} {} {} {} {}
else else if() if() if()
{} {} {} { }
else if() if()
{} {}
Q)wap to check whether the given no is Even no or odd no?
Algorithm:-
1. start
2. consider no is 5
3. check whether no is modulo divide by 2 then
goto step4 otherwise goto step 5
4. print even no goto step6
5. print odd no
6. stop
coding:-
static void Main()
{
int no=5;
if(no%2==0)
Console.WriteLine("Even no");
else
Console.WriteLine("Odd no");
}
Q)wap to accept 2 nos from console and print the greatest no?
Algorithm:-
1. start
2. read 2 nos from console and store in a,b
3. check a greater b then goto step4 otherwise goto
step5
4. print a is big goto step 8
5. check b is greater than a then goto step6 otherwise
goto step7
6. print b is big goto step 8
7. print both are equal goto step 8
8. stop
Ex:-
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter First no");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int b = int.Parse(Console.ReadLine());
if(a>b)
Console.WriteLine(a+" is Big ");
else if(b>a)
Console.WriteLine(b+" is Big");
else
Console.WriteLine("Both are Equal");
}
}
Q)wap to accept 3 nos from console and print the greatest no?
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter First no");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second no");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Third no");
int c = int.Parse(Console.ReadLine());
if (a > b)
{
if(a>c)
Console.WriteLine(a+" is Big");
else
Console.WriteLine(c+"is Big");
}
else if(b>c)
Console.WriteLine(b+"is Big");
else
Console.WriteLine(c+"is big");
}
}
Loops
Loops:- Loop is a Repetition process to perform a specfic operation
Loop is a Mechanism which is used to execute some set
of statements repeatedly until the condition become false
Loops are of 2 Types:-
1. Range based Loop (for loop)
2. Condition based Loop (while,do-while)
Advantage of Loops:- Reducing the no of lines of code
Every Loop will have 3 sections:-
1. initialization:- Assigning the value to the variable at the
time of declaring the variable
int x=5;
Assignment:- Assigning the value to the variable after
declaring the variable
int x; declaration
x=5; assignment
2. condition :- a condition is used to compare 2 values or
variables or Expressions and return a boolean value
either true or false
2>3 false
-3<-17 false
int x=5;
int y=3;
bool b=x>y;
3. increment and decrement :-
++ is called as increment operator
-- is called as decrement
int x=5; int x=5;
C.WL(x++); C.WL(++x);

int x=5; int x=5;


C.WL(x--); C.WL(--x);

int x=5;
C.WL(x++ + --x + ++x +x--);
o/p:- 22

int x=5;
C.WL(--x + x-- + x++ + ++x);

int x=3;
int y=5;
C.WL(x++ + --y + x-- + ++y);

int x=5;
C.WL(x++ + --x);
C.WL(x);

syn for loop:-


for(initialization;condition;inc/dec)
{
statements
}
Rotate the loop 5 times:-
1 101 6 0
2 102 7 1
3 103 8 2
4 104 9 3
5 105 10 4
Execution flow of for loop:-
1. initialization
2. condition true
3. statements
4. inc/dec
5. condition true
6. statements
7. inc/dec
8. condition false
9. Exit for loop
Q)wap to print sathyatechnologies 5 times?
1. decide the loop
2. decide no of times we have to rotate the loop
3. decide the initial value and final value
4. decide condition
5. decide the increment or decrement
for(int i=1;i<=5;i++)
{
Console.WriteLine("sathya Technologies");
}
for(int i=5;i>=1;i--)
{
Console.WriteLine("sathya Technologies");
}

Q)what is variable?
variable is the name given for a particular memory
location
poinst to remebber:-
1. we can store only value in variable
int x=5; valid
int x=5,6; invalid
int x=3+4-2; valid
2. we can store value,variable or expression in a variable
int x=5; assign value to variable
int y=x; assign x to y
int z=2+3-4; assign an expression to z
Q)what will happen when we assign an expression
to a variable?
the result of the expression was stored in variable
Q) what will hapen when we assign a variable to
another variable?
value of Rightside variable will store in Left side
variable
int x=5;
int y=x;
3 . whenever we modify variable then the previous value will be erased
int x=5;
x=3;
x=2;
Ex:-
1.
int x=5+3-2;
C.WL(x);
2. int x=5;
x=x+3;
x=x+2;
3.
int x=5;
for(int i=1;i<=5;i++) for(int i=1;i<=5;i++)
{ {
x=x+1; int x=5;
} x=x+3;
Console.WriteLine(x); Console.WriteLine(x);
}
o/p:- 10
4.
int sum=0;
for(int i=1;i<=5;i++)
{
sum=sum+i;
}
Console.WriteLine(sum);
1. What is the output of below code snippet?
static void Main()
{
for (int i = 1; i <12 ; i=i+2)
{
Console.WriteLine(i);
}
}
o/p:- 1 3 5 7 9 11
2.
for (int i=2; i<12; i=i+2)
{
Console.WriteLine(i);
}
o/p:- 2 4 6 8 10
3.
for (int i=1; i<15; i=i+3)
{
Console.WriteLine(i);
}

o/p:- 1,4,7,10,13
4.
for(int i=2; i<=18; i=i+3)
{
Console.WriteLine(i);
}
o/p:- 2,5,8,11,14,17
5.
for(int i=2; i<=7;++i)
{
Console.WriteLine(i);
}
o/p:- 2,3,4,5,6,7
6.
for (int i=0; i<=10;i=i+4)
{
if (i==8)
{
Console.WriteLine("Welcome to sathya");
}
}
o/p:- Welcome to sathya
7.
classProgram
{
staticvoidMain(string[] args)
{
int i, j;
i=0;
j=0;
for(i=1; i<=5; i++)
{
Console.Write(i+3);
}
Console.ReadLine();
}
}
o/p:- 4, 5,6,7,8
8.
static void Main(string[] args)
{
int myInt;
Console.WriteLine(myInt);
}
o/p:- Error
9.
static void Main()
{
if (10>5)
{
int x = 5;
}
Console.WriteLine(x);
}
o/p:- Error
10.
static void Main()
{
int x = 5;
for (int i = 0; i <=5 ; i++)
{
Console.WriteLine(x);
}
}
o/p:- 5 5 5 5 5 5
11.
static void Main()
{
int sum = 0;

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


{
sum = sum + 5;
}
Console.WriteLine(sum);
}
o/p:- 30
12.
int sum = 0;
for (int i = 0; i <= 5; i++)
{
sum = sum + i;
}
Console.WriteLine(sum);
o/p:- 15
14.
14. static void Main()
{
int ec = 0; int oc = 0;
for (int i = 11; i <=20; i++)
{
if (i % 2 == 0)
ec++;
else
oc++;
}
Console.WriteLine(ec);
Console.WriteLine(oc);
}

number:- number is a numeric value it may be integer or floatingpoint


Single Digit no 4,5,6
Double Digit no 45,56
Triple Digit no 345,567
2345 Two Thousand Three Hundred Forty Five
23 4 5
note:-whenever we want to perform operations on individual digits in a
given no we need to identify the units
place digit
Q) How to identify the units place digit in a given number?
Any number modulo divide by 10 then the result is units
place digit 1234
1234%10

Q)How to seperate units place digit from a given no?


Any number divide by 10 then then units place digit is removed from
the given no
1234/10=123

Q)wap to find the sum of the digits for a given 4 digit no?
consider no is 2341
1+4+3+2=10
using System;
class A
{
static void Main()
{
int no = 2341;
int r = 0; int sum = 0;
for(int i = 1; i<=4; i++)
{
r = no % 10;
sum = sum + r;
no = no / 10;
}
Console.WriteLine(sum);
}
}
Algorithm:-
1. start
2. consider no is 2341
r is 0
sum is 0
3. modulo divide no with 10 and store the result in r
4. add sum to r and store the result in sum
5. divide no with 10 and store the result in no
6. repeat step3 to step5 4 times
7. stop
Q)wap to find the sum of the squares of the digits for a given 4 digit no?
4231
Algorithm:-
1. start
2. consider no is 4231
r is 0
square is 0
sum is 0
3. modulo divide no with 10 and store the result in r
4. multiply r with r and store the result in square
5. add square to sum and store the result in sum
6. divide no with 10 and store the result in no
7. repeat step3 to step6 4 times
8. stop
1)wap to check whether the given no is Armstrong no or not?
Armstrong no:- sum of the cubes of digits is eqaul to
Given no 3 3 3
Ex:- 153 1 +5 +3
1+125+27=153

2) wap to find the sum of the middle digits for a given


4 digit no?
1234
2+3=5
3) wap to find the sum of the first digit and last digit for a given 4 digit
no?
1237
1+7=8
4)waq to display Even digits for a given 5 digit no?
12345
Even Digits are:- 2 ,4
5)waq to display the sum of Even digits for a given 5 digit no?
12345
sum is 6

Q)sum of first digit and last digit 1245?

int no=1245;
int r=0;
int sum=0;
for(int i=1;i<=4;i++)
{
r=no%10;
if(i==1 || i==4)
{
sum=sum+r;
}
no=no/10;
}
Console.WriteLine(sum);
Q)wap to print even digits from the given 4 digit no?
int no=1234;
int r=0;
for(int i=1;i<=4;i++)
{
r=no%10;
if(r%2==0)
{
Console.Write(r+" ");
}
no=no/10;
}
Q)wap to check whether the given no is palindrome or not?
palindrome means given no is equal to reverse no
EX:-
1221 palindrome
5432 not a palindrome
using System;
class A
{
static void Main()
{
int no = 1221;
int r = 0;
int t = no;
int revno = 0;
for(int i = 1; i<=4; i++)
{
r = no % 10;
revno = (revno * 10) + r;
no = no / 10;
}
if(t==revno)
Console.WriteLine("Palindrome");
else
Console.WriteLine("Not a Palindrome");
}
}
Q)wap to accept a no from console and print the multiplication table?
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
for (int i = 1; i <=10; i++)
{
Console.WriteLine(no+"*"+i+"="+(no*i));
}
}
}
Fibonacci:-
using System;
class A
{
static void Main()
{
int f1 = 0; int f2 = 1; int f3;
Console.Write(f1+" ");
Console.Write(f2+" ");
for(int i=1; i<=7; i++)
{
f3 = f1 + f2;
Console.Write(f3+" ");
f1 = f2;
f2 = f3;
}

}
}
Q)wap to find the factorial of the given no?
5!=5*4*3*2*1
7!=7*6*5*4*3*2*1
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int fact = 1;
for(int i = 1; i <=no; i++)
{
fact = fact * i;
}
Console.WriteLine(fact);
}
}
Q)wap to accept a no from console and check whether the
given no is prime no or not?
prime number means any number divisible by 1 and itself
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int count = 0;
for (int i = 1; i <=no; i++)
{
if(no%i==0)
{ count++; }
}
if (count==2)
{ Console.WriteLine("Prime no"); }
else
{ Console.WriteLine("Not a Prime no"); }
}
}
Ex:-
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int count = 0;
for (int i = 1; i <=no; i++)
{
if(no%i==0)
{ count++; }
if (count>2)
{ break; }
}
if (count==2)
Console.WriteLine("Prime no");
else
Console.WriteLine("Not a Prime no");
} }
Q)wap to accept a no from console and check whether the given no is
perfect no or not?
sum of Proper Divisors is equal to given no
6 ----> 1+2+3 =6
6 is perfect no
28 ------> 1+2+4+7+14 =28Q)wap to
accept a no form
Ex:-
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <no; i++)
{
if(no%i==0)
{
sum = sum + i;
}
}
if(no==sum)
Console.WriteLine("Perfect no");
else
Console.WriteLine("Not a Perfect no");
}
}
console and check whether the
given no is strong no or not?
strong no means sum of factorial of the individual
digits in the given no
145
1!+4!+5!
1! is 1
4!= 4*3*2*1 is 24
5! = 5*4*3*2*1 120
1+24+120=145
Ex:-
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a 3 digit no");
int no = int.Parse(Console.ReadLine());
int r = 0; int fact = 1; int sum = 0; int t = no;
for (int i = 1; i<=3; i++)
{
r = no % 10;
for (int j = 1; j<=r; j++)
{ fact = fact * j; }
sum = sum + fact;
fact = 1;
no = no / 10;
}
if(t==sum)
Console.WriteLine("strong no");
else
Console.WriteLine("not a strong no");
}
}
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
==========================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
=======================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
======================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(i+j);
}
Console.WriteLine();
}
}
}
========================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(i-j);
}
Console.WriteLine();
}
}
}
========================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(j-i);
}
Console.WriteLine();
}
}
}
=======================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++)
{
Console.Write(i*j);
}
Console.WriteLine();
}
}
}
=========================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <=5; j++) *****
{ #####
if (i%2==0) *****
Console.Write("#"); #####
else *****
Console.Write("*");
}
Console.WriteLine();
}
}
}
=======================
for(int i = 1; i <=5; i++)
{ O/P
for (int j = 1; j <=5; j++) *#*#*
{
if (j%2==0)
Console.Write("#");
else
Console.Write("*");
}
Console.WriteLine();
}
======================================
using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++) *#*#*
{ #*#*#
for (int j = 1; j <=5; j++) *#*#*
{ #*#*#
if ((i+j)%2==0) *#*#*
Console.Write("*");
else
Console.Write("#");
}
Console.WriteLine();
}
}
}
Q)what will happen when we convert char to int?
whenever we convert char to int then the ASCII value of
the character will store in integere variable
a-z 97-122
A-Z 65-90
0-9 48 to 57
char ch = 'a';
int x = Convert.ToInt32(ch);
Console.WriteLine(x);
Q)what will happen when we increment character?
when we increment character then the ASCII value of the
character will increment
char ch = 'a';
ch++;
Console.WriteLine(ch); o/p:- 98

ABCDE ABCDE AAAAA


FGHIJ ABCDE BBBBB
KLMNO ABCDE CCCCC
PQRST ABCDE DDDDD
UVWXY ABCDE EEEEE

1)for(int i=1;i<=5;i++)
{ *
for(int j=1;j<=i;j++) **
{ ***
Console.Write("*"); ****
} *****
Console.WriteLine();
}
2)using System;
class A
{
static void Main()
{
int x=1; 1
for (int i = 1; i <=5; i++) 23
{ 456
for (int j = 1; j <= i; j++) 78910
{ 11 12 13 14 15
Console.Write(x);
x++;
}
Console.WriteLine();
}
}
}
3)using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++) 1
{ 12
for (int j = 1; j <= i; j++) 123
{ 1234
Console.Write(j); 12345
}
Console.WriteLine();
}
}
}
4)using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++) 1
{ 22
for (int j = 1; j <= i; j++) 333
{ 4444
Console.Write(i); 55555
}
Console.WriteLine();
}
}
}
5)static void Main()
{
char ch = 'A';
for (int i = 1; i <=5; i++) A
{ BC
for (int j = 1; j <= i; j++) DEF
{ GHIJ
Console.Write(ch++); KLMNO
}
Console.WriteLine();
}
}
7)using System;
class A
{
static void Main()
{ A
for (int i = 1; i <=5; i++) AB
{ ABC
char ch = 'A'; ABCD
for (int j = 1; j <= i; j++) ABCDE
{
Console.Write(ch++);
}
Console.WriteLine();
}
}
}
8)using System;
class A
{
static void Main()
{
char ch = 'A';
for (int i = 1; i <=5; i++) A
{ BB
for (int j = 1; j <= i; j++) CCC
{ DDDD
Console.Write(ch); EEEEE
}
ch++;
Console.WriteLine();
}
}
}

*
##
***
####
*****
9)using System;
class A
{
static void Main()
{
for (int i = 1; i <=5; i++) *
{ **
for (int j = 4; j>=i; j--) ***
{ ****
Console.Write(" "); *****
}
for (int k = 1; k<=i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
1 A
12 BB
123 CCC
1234 DDDD
12345 EEEEE
Q)wap to Display even nos between 1 tom 100?
Q)wap to Display prime nos between 1 tom 100?
Q)wap to print the sum of prime nos between 1 tom 100?
while loop
while loop is condition based loop
syn:-
intiailization;
while(condition)
{
statements
inc/dec;
}
Q)wap to print sathyatechnologies 5 times by using while loop?
using System;
class A
{
static void Main()
{
int i=1;
while(i<=5)
{
C.WL("sathyatechnologies");
i++;
}
}
}
====================================
Q)wap to accept a no from console and find the sum of digits ?
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int r = 0; int sum = 0;
while (no!=0)
{
r = no % 10;
sum = sum + r;
no = no / 10;
}
Console.WriteLine(sum);
}
}
Q)what is the Difference between while loop and do- while loop?
whileloop:- it will check the condition first and based on the
condition the statements will gets
executed
do-while :- it will execute the statements and then
check the condition
syn:-
do
{
---------
}
while();
1)wap to find the divisibility rule for 2 for the given no based on the
below condition?
The last digit is even(0,2,4,6 or 8)
Ex:- Given no is 1294
4 is even
so 1294 is Divisible by 2
Alogorithm:-
1. start
2. store the no in x
3. modulo divide x with 10 and store the result in r
4. check modulo divide r with 2 is equal to 0 then print
Even no goto step6 otherwise goto step5
5. no is dnot divisible by 2
6. stop
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int x = int.Parse(Console.ReadLine());
int r =x % 10;
if (r%2==0)
Console.WriteLine("Given no is Divisible by 2");
else
Console.WriteLine("Given no not divisible by 2");
}
}
2)wap to find the divisibility rule for 3 for the given no based on the
below condition?
Sum the digits . the result must be divisible by 3
249----> 2+4+9=15--->1+5=6
6 is clearly divisible by 3
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int r = 0; int sum = 0;
do
{
while (no != 0)
{
r = no % 10;
sum = sum + r;
no = no / 10;
}
no = sum;
sum = 0;
}
while (no>9);
if(no % 3 == 0)
Console.WriteLine("Given no is Divisible by 3");
else
C.WL("Given no is not Divisible by 3");
}}
3)wap to find the divisibility rule for 4 for the given no based on the
below condition?
If the tens digit is even ,then one’s digit must be 0,4, or 8.
Ex:- 40
If the tens digit is odd, the one’s digit must be 2
or 6.
Ex:- 832
3 is odd and last digit is 2 or 6
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a no");
int no = int.Parse(Console.ReadLine());
int td = no % 100;
int ud = no % 10;
if((td%2==0) && (ud==0 || ud==4 || ud==8))
{ Console.WriteLine("Divisibility by 4"); }
else
{
if(ud==2 || ud==6)
Console.WriteLine("Divisibility by 4");
else
Console.WriteLine("Not Divisible by 4");
}

}
}
4)wap to find the divisibility rule for 5 for the given no based on the
below condition?
The last digit is 0 or 5.
Ex:-495: the last digit is 5
5)wap to find the divisibility rule for 6 for the given no based on the
below condition?
It is divisible by 2 and by 3
Ex:-
1458: 1+4+5+8=18, so it is divisible by 3 and the last digit is even,
hence the number is
divisible by 6.
6)wap to find the divisibility rule for 8 for the given no based on the
below condition?
If the hundred digit is even, the number formed by the last two digits
must be divisible by 8.if the hundreds digit is odd, the number obtained
by the last two digits plus 4 must be divisible by 8.
Ex:-
624->24,
24 is divisible by eight
Ex:-
720->20+4->24,
7)wap to find the divisibility rule for 9 for the given no based on the
below condition?
Sum digits repeatedly, the final result must be 9.
729->7+2+9=18->1+8=9.
Array
Arrays:-Array is used to store multiple values of same datatype in a single
variable
Array is used to store Homogeneous values
Array is used to store multiple values Temporarily until the program is
running
Array is a subscript variable
syn to declare an Array:-
Datatype[] Arrayname=new Datatype[somesize];
in Array each and every element can be identified by using index
number
Always the index no starts from 0 to size-1
Ex:- if size is 5 range is 0 to 4
if size is 7 range is 0 to 6
Arrays are classified into3 types
1. Single Dimensional Array
2. MultiDimensional Array
3. Jagged Array
Single Dimensional Array :- it is used to store multiple values of same
datatype in a single variable

int[] Ar=new int[5];


syn to store the value in SDA:-
Arrayname[index]=value;

Q)wap to print the values from the given Array?


2 3 5 7 8
using System;
class A
{
static void Main()
{
int[] Ar = new int[5];
Ar[0] = 2;
Ar[1] = 3;
Ar[2] = 5;
Ar[3] = 7;
Ar[4] = 8;
for (int i = 0; i <= 4; i++)
{ Console.WriteLine(Ar[i]+" "); }
}
}
note:- Array will allocate the memory in a continous memory location
we can perform searching and sorting operations easily
syn 2 to declare SDA:-

2 5 3 6 7 4

int[] Ar=new int[6]{2,5,3,6,7,4};


Q)wap to find the sum of the Elements in the given Array?
using System;
class A
{
static void Main()
{
int[] Ar = new int[6] { 2, 5, 3, 6, 7, 4 };
int sum = 0;
for(int i=0; i <=5; i++)
{
sum = sum + Ar[i];
}
Console.WriteLine(sum);
}
}
Q)wap to find the sum of thEven Elements and Odd Elements in the
given Array?
2, 5, 3, 6, 7, 4
using System;
class A
{
static void Main()
{
int[] Ar = new int[6] { 2, 5, 3, 6, 7, 4 };
int es = 0;
int os = 0;
for(int i=0; i<=5; i++)
{
if (Ar[i] % 2 == 0)
es = es + Ar[i];
else
os = os + Ar[i];
}
Console.WriteLine("Even sum is"+es);
Console.WriteLine("Odd sum is" + os);
}
}
Q)waq to count the no of Even Elements and odd Elements
in the given Array?
2 3 4 5 6 7 1
no of Even Elements are:- 3
no of Odd Elements are:- 4
using System;
class A
{
static void Main()
{
int[] Ar = new int[7] { 2, 3, 4,5, 6, 7,1 };
int ec = 0;
int oc = 0;
for(int i=0; i<=6; i++)
{
if (Ar[i] % 2 == 0)
ec = ec + 1;
else
oc = oc + 1;
}
Console.WriteLine("No of Even Elements are"+ec);
Console.WriteLine("No of Odd Elements are" + oc);
}
}
Q)wap to fidn the min value and max value in the given Array?
2 3 4 1 6 7
using System;
class A
{
static void Main()
{
int[] Ar=new int[6]{ 2,3,4,1,6,7};
int min = 2;
int max = 2;
for (int i = 0; i <=5 ; i++)
{
if (min > Ar[i])
min = Ar[i];
if (max < Ar[i])
max = Ar[i];
}
Console.WriteLine("Min value is"+min);
Console.WriteLine("Max value is" + max);
}
}
Q)wap to find the 2nd max value in the Given Array?
2 3 4 1 6 7
o/p:- 2nd max value is 6
Q)wap to find the sum of minvalue and max value in the given Array?
sum is 1+7=8
Q)what is string?
string is group of characters
string is character Array
using System;
class A
{
static void Main()
{
char[] ch = new char[6] { 's', 'a', 't', 'h', 'y', 'a' };
for (int i = 0; i <=5 ; i++)
{
Console.Write(ch[i]);
}
Console.WriteLine();
}
}
in C#.net string is a predefined class
Q)wap to find the length of the given string?
using System;
class A
{
static void Main()
{
string s = "satyam";
Console.WriteLine(s.Length);
}
}
Q)wap to find the length of the given string without using predefined
property or function?
using System;
class A
{
static void Main()
{
string s = "satyam";
int l = 0;
foreach (char c in s)
{
l++;
}
Console.WriteLine(l);
}
}
Q)wap to reverse the given string?
using System;
class A
{
static void Main()
{
string s = "satyam";
for(int i=s.Length-1; i>=0;i--)
{
Console.Write(s[i]);
}
Console.WriteLine();
}
}
Q)wap to check whether the given string is palindrome
or not?
Palindrome:- given string is equal to reverse string
i/p:- madam
o/p:- madam
Q)wap to check whether the given string is Anagram
or not?
i/p :- silent post
o/p:- listen stop
Every character in one string must exist in another
string

Q)wap to check whether the given string is palindrome or not?


palindrome means given string is equal to reverse string
Q)How to convert string into charArray?
class string
{
Char[] ToCharArray()
{

}
}
string s="sathya";
char[] ch=s.ToCharArray();
using System;
class A
{
static void Main()
{
Console.WriteLine("Enter a string");
string s = Console.ReadLine();
string rs = "";
for (int i =s.Length-1;i>=0;i--)
{ rs=rs + s[i]; }
if(s==rs)
Console.WriteLine("Palindrome");
else
Console.WriteLine("Not a Palindrome");
}
}
Q)wap to count no of times a is appeared in the given string?
sathyatech
using System;
class A
{
static void Main()
{
string s = "sathyatech";
int count = 0;
for (int i = 0; i<=s.Length-1; i++)
{
if(s[i]=='a')
{
count++;
}
}
Console.WriteLine("no of a's are"+count);
}
}
Q)wap to find the no of lowercase,uppercase characters,
digits in the given string?
sAtHya12Te4cH

Expected o/p:-
Lowercase characters :- 6
Uppercase characters :- 4
Digits :- 3
ASCII:-
A-Z 65-90
a-z 97-122
0-9 48-57
using System;
class A
{
static void Main()
{ int uc = 0; int lc = 0; int dc = 0;
string s = "sAtHya12Te4cH";
for (int i = 0; i <=s.Length-1 ; i++)
{
int x=Convert.ToInt32(s[i]);
if (x >= 65 && x <= 90)
uc++;
else if (x >= 97 && x <= 122)
lc++;
else if (x >= 48 && x <= 57)
dc++;
}
Console.WriteLine("no of Lowercase are "+lc);
Console.WriteLine("no of Uppercase are " + uc);
Console.WriteLine("no of digits are"+dc);
}
}
Q)wap to convert lowercase characters to uppercase?
using System;
class A
{
static void Main()
{
string s = "sathya";
Console.WriteLine("given string is"+s);
for (int i = 0; i <=s.Length-1 ; i++)
{
int x = Convert.ToInt32(s[i]);
x = x - 32;
char ch = Convert.ToChar(x);
Console.Write(ch);
}
Console.WriteLine();
}
}
Q)wap to convert uppercase character characters to lowercase?
using System;
class A
{
static void Main()
{
string s = "SATHYA";
Console.WriteLine("given string is"+s);
for (int i = 0; i <=s.Length-1 ; i++)
{
int x = Convert.ToInt32(s[i]);
x = x + 32;
char ch = Convert.ToChar(x);
Console.Write(ch);
}
Console.WriteLine();
}
}
Q)wap to convert lower case character to upper and vice versa?
using System;
class A
{
static void Main()
{
string s = "sAtHYaT";
Console.WriteLine("given string is"+s);
for (int i = 0; i <=s.Length-1 ; i++)
{
int x = Convert.ToInt32(s[i]);
if (x >= 65 && x <= 90)
{ x = x + 32; }
else if (x >= 97 && x <= 122)
{ x = x - 32; }
char ch = Convert.ToChar(x);
Console.Write(ch);
}
Console.WriteLine();
} }

Q) i/p string is
sa23t6h7ya
o/p:- sathya
using System;
class A
{
static void Main()
{
string s="sa23t6h7ya";
for(int i=0;i<=s.Length-1; i++)
{
int x = Convert.ToInt32(s[i]);
if(x>=48 && x<=57)
{ }
else
{
Console.Write(s[i]);
}
}
Console.WriteLine();
}
}
Q) i/p string is
sa23t6h7ya
o/p:- 2+3+6+7
18
1+8=9
using System;
class A
{
static void Main()
{
int sum = 0;
string s1 = "";
string s = "sa23t6h7ya";
int no; int r=0;
for(int i=0; i<=s.Length-1; i++)
{
int x = Convert.ToInt32(s[i]);
if(x>=48 && x<=57)
{
s1 = s1 + s[i];
}
}
no=int.Parse(s1);
do
{
while (no != 0)
{

r = no % 10;
sum = sum + r;
no = no / 10;
}
no = sum;
sum = 0;
}
while (no > 9);
Console.WriteLine(no);
}
}
Q)wap to check whether the given string is Anagram or not?
Anagram means each character in one string must exist in another
string
listen silent
using System;
class A
{
static void Main()
{ string s1 = "listen";
string s2 = "silent";
int count = 0;
for(int i = 0; i<=s1.Length-1; i++)
{
for(int j = 0; j <=s2.Length-1; j++)
{
if(s1[i]==s2[j])
{ count++; }
}
}
if(count==6)
{ Console.WriteLine("Anagram"); }
else
{ Console.WriteLine("Not Anagram"); }
}
}
Q)given string
there is a cat
reverse string is
cat a is there
using System;
class A
{
static void Main()
{
string s = "there is a cat";
char[] ch=s.ToCharArray();
string[] Ar = new string[4];
int x = 0;
for (int i = 0; i <=ch.Length-1 ; i++)
{
if(ch[i]!=' ')
{
Ar[x] = Ar[x] + s[i];
}
else
{
x++;
}
}
for(int j =3; j>=0; j--)
{
Console.Write(Ar[j]+" ");
}
Console.WriteLine();
}
}
MultiDimensional Array:-it is used to store values in matix format
syn:-
datatype[,] Arrayname=new datatype[noofrows,noofcols];
using System;
class A
{
static void Main()
{
int[,] Ar=new int[3,2]{{10,20},{30,40},{50,60}};
for(int i = 0; i <=2 ; i++)
{
for(int j=0; j<=1 ; j++)
{
Console.Write(Ar[i,j]+" ");
}
Console.WriteLine();
}
}
}
Ex:-

1 2 2 3 3 5

+ =

3 4 4 1 7 5
using System;
class A
{
static void Main()
{
int[,] Ar1=new int[2,2]{{1,2},{3,4}};
int[,] Ar2 = new int[2, 2] { { 2,3 }, { 4,1 } };
int[,] Ar = new int[2, 2];
for (int i = 0; i <=1 ; i++)
{
for (int j = 0; j <=1 ; j++)
{
Ar[i, j] = Ar1[i, j] + Ar2[i, j];
Console.Write(Ar[i,j]+" ");
}
Console.WriteLine();
}

}
}
Jagged Array:- Array inside another array is jagged array
datatype[][] Arrayname=new datatype[noofrows][];
Ex:-
10
20 30
40
50 60 70
using System;
class A
{
static void Main()
{
int[][] Ar = new int[4][];
Ar[0] = new int[1] { 10 };
Ar[1] = new int[2] { 20,30 };
Ar[2] = new int[1] { 40 };
Ar[3] = new int[3] { 50, 60, 70 };
for (int i = 0; i <=3 ; i++)
{
for (int j = 0; j<=Ar[i].Length-1; j++)
{
Console.Write(Ar[i][j]+" ");
}
Console.WriteLine();
}
}
}

1)wap to find the divisibility rule for 2 for the given no based on the
below condition?
The last digit is even(0,2,4,6 or 8)
Ex:- Given no is 1294
4 is even
so 1294 is Divisible by 2
2)wap to find the divisibility rule for 3 for the given no based on the
below condition?
Sum the digits . the result must be divisible by 3
249----> 2+4+9=15--->1+5=6
6 is clearly divisible by 3
3)wap to find the divisibility rule for 4 for the given no based on the
below condition?
If the tens digit is even ,the one’s digit must be 0,4, or 8.
Ex:- 40
If the tens digit is odd, the one’s digit must be 2
or 6.
Ex:- 832
3 is odd abd last digit is 2
4)wap to find the divisibility rule for 5 for the given no based on the
below condition?
The last digit is 0 or 5.
Ex:-495: the last digit is 5
5)wap to find the divisibility rule for 6 for the given no based on the
below condition?
It is divisible by 2 and by 3
Ex:-
1458: 1+4+5+8=18, so it is divisible by 3 and the last digit is even, hence
the number is
divisible by 6.
6)wap to find the divisibility rule for 8 for the given no based on the
below condition?
If the hundred digit is even, the number formed by the last two digits
must be divisible by 8.if the hundreds digit is odd, the number obtained
by the last two digits plus 4 must be divisible by 8.
Ex:-
624->24,
24 is divisible by eight
Ex:-
720->20+4->24,
7)wap to find the divisibility rule for 9 for the given no based on the
below condition?
Sum digits repeatedly, the final result must be 9.
729->7+2+9=18->1+8=9.

1. prime numbers between 1 to 100?


2. program to check whether the given string is palindrome or not?
3. find the duplicate words in given sentence?
i/p:- This is string and This is not an integer
o/p:- This,is
4. check if any coinsecutive numbers are same in an Array?
i/p:- [1,2,3,4,4,5,6,7,7,8]
5.wap to convert decimal to octal
Ex:-
Enter a decimal no
50
Equivalent octal no is:- 62

You might also like