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

Basic PRG

Java/C# is considered a pure object-oriented programming language because everything in the languages is defined within classes, and everything acts as an object. Some key concepts include: - The class keyword is used to declare a class. - The public keyword is an access modifier that makes things visible to all. - Static methods can be called without creating an object and are used for things like main methods. - Non-static methods must be called on an object. - Various data types and operations like input/output are demonstrated in code examples like calculating area, swapping values, and converting between time formats.

Uploaded by

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

Basic PRG

Java/C# is considered a pure object-oriented programming language because everything in the languages is defined within classes, and everything acts as an object. Some key concepts include: - The class keyword is used to declare a class. - The public keyword is an access modifier that makes things visible to all. - Static methods can be called without creating an object and are used for things like main methods. - Non-static methods must be called on an object. - Various data types and operations like input/output are demonstrated in code examples like calculating area, swapping values, and converting between time formats.

Uploaded by

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

why java/C# is called pure OOPS

everything in java/C# within class & everything in java/c# acts as an object

class keyword is used to declare a class in Java/C#.

public keyword is an access modifier that represents visibility.


It means it is visible to all.it is accessible for everyone.

static is a keyword. If we declare any method(function) as static, it is known as


the static method.
The core advantage of the static method is that
there is no need to create an object to invoke the static method.
The Main() method is executed by the Compiler,
so it doesn't require creating an object to invoke the Main() method(function). So,
it saves memory.
static: widhout creating an object any method directly call by compiler/Classname
non-static:method must be access with an object

void is the return type of the method. It means it doesn't return any value.

Main() represents the starting point of the program./entry point of u r program

string[] args is used for command line argument. We will discuss it in coming
section.

which namespace is automatically imported by compiler in C#


-System

namespace System
|
classes Console
|
methods()/function() WriteLine() /ReadLine()(static)
|
object

A)Static (i/p fix o/p fix)


1)Welcome
using System;//namespace

class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to C# language");//printf cout
}
}

2)Addition of 2 no(Static i/p fix & o/p fix)


using System;

public class HelloWorld


{
public static void Main(string[] args)
{
int a=12134,b=14567,c;
c=a+b;
Console.WriteLine("Addition={0} ",c);
}
}

2.1)Substraction of 2 numbers
2.2)Division of 2 numbers
2.3)Multiplication of 2 numbers
3)Addition of 5 numbers

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int a=12134,b=14567,c=67,d=234,e=674,f;
f=a+b+c+d+e;
Console.WriteLine("Addition ={0} ",f);
}
}

4)Multiplication of 4 numbers

B)Dynamic Program(0/p varies according to i/p)


1)
using System;

public class HelloWorld


{
public static void Main(string[] args)
{
int a,b,c;
Console.WriteLine("Enter 2 no");//printf
a=Convert.ToInt32(Console.ReadLine());//scanf
b=Convert.ToInt32(Console.ReadLine());
c=a+b;
Console.WriteLine("Addition={0}",c);
}
}

2)Substraction of 2 no
3)Multiplication of 2 no
4)Division
5)Addition of 5 no

using System;
public class Add5
{
public static void Main(string[] args)
{
int a,b,c,d,e,f;
Console.WriteLine("Enter 5 no");//printf
a=Convert.ToInt32(Console.ReadLine());//scanf
b=Convert.ToInt32(Console.ReadLine());
c=Convert.ToInt32(Console.ReadLine());
d=Convert.ToInt32(Console.ReadLine());
e=Convert.ToInt32(Console.ReadLine());
f=a+b+c+d+e;
Console.WriteLine("Addition={0}",f);
}
}
6)Addition of 7 no

7)Multiplication of 4 numbers.

i/p o/p related programs

1)Area of circle
A=3.14*r*r;
i/p r
o/p A

using System;

public class Areac


{
public static void Main(string[] args)
{
double r,A;
Console.WriteLine("Enter radius");//printf
r=Convert.ToDouble(Console.ReadLine());//scanf
A=3.14*r*r;
Console.WriteLine("Area={0}",A);
}
}

2)Area of rectangle
A=l*b
i/p l,b
o/p A

3)Area of triangle
A=0.5*b*h
i/p b,h
o/p A
4)
pe=m*g*h
i/p m,g,h
o/p pe
5)
ke=0.5*m*v*v
i/p m,v
o/p ke

6)
Arithmetic mean & Harmonic mean
am=(a+b)/2
hm=(a-b)/2

i/p a,b
o/p am,hm
using System;

public class Am_Hm


{
public static void Main(string[] args)
{
double a,b,am,hm;
Console.WriteLine("Enter a & b");//printf
a=Convert.ToDouble(Console.ReadLine());//scanf
b=Convert.ToDouble(Console.ReadLine());
am=(a+b)/2;
hm=(a-b)/2;
Console.WriteLine("AM={0}\nHM={1}",am,hm);
}
}
7)
8)
9)
10)
11)
using System;

public class HelloWorld


{
public static void Main(string[] args)
{
double F,c,k;
Console.WriteLine("Enter F");//printf
F=Convert.ToDouble(Console.ReadLine());//scanf
c=(9/5)*(F-32);
k=c+273.15;
Console.WriteLine("C={0}\nKelvin={1}",c,k);
}
}
12)
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int m1,m2,m3,m4,m5,total;
double per;
Console.WriteLine("Enter 5 no");//printf
m1=Convert.ToInt32(Console.ReadLine());//scanf
m2=Convert.ToInt32(Console.ReadLine());
m3=Convert.ToInt32(Console.ReadLine());
m4=Convert.ToInt32(Console.ReadLine());
m5=Convert.ToInt32(Console.ReadLine());
total=m1+m2+m3+m4+m5;
per=total/5;
Console.WriteLine("Total={0} \n Per={1}",total,per);
}
}

13)
Accept basic salary from user & calculate HRA(home rental allowance),TA(travelling
Allowance),
DA(Dinner allowance) & GS(gross salary)
i/p bs
o/p
hra
ta
da
tax
gs

hra=50% on bs hra=bs*0.50;
ta=40% on bs ta=bs*0.40;
da=60% on bs da=bs*0.60
tax=5% on bs tax=bs*0.05
gs=(bs+hra+ta+da)-tax;
using System;

public class GS
{
public static void Main(string[] args)
{
double bs,hra,ta,da,tax,gs;
Console.WriteLine("Enter bs");//printf
bs=Convert.ToDouble(Console.ReadLine());//scanf
hra=bs*0.50;
ta=bs*0.40;
da=bs*0.60;
tax=bs*0.05;
gs=(bs+hra+ta+da)-tax;
Console.WriteLine("HRA={0}\nTA={1}\nDA={2}\nTAx={3}\
nGS={4}",hra,ta,da,tax,gs);
}
}

14)
room l,b,h 2*(l*b + l*h + b*h)
door l1,h1
window l2,h2(2 window)
calculate total area with roof is painted
using System;

public class HelloWorld


{
public static void Main(string[] args)
{

double l,b,h, l1,h1,l2,h2,area;


Console.WriteLine("Enter l,b,h of room,l1,h1 of door,l2,h2 of
window");//printf
l=Convert.ToDouble(Console.ReadLine());//scanf
b=Convert.ToDouble(Console.ReadLine());
h=Convert.ToDouble(Console.ReadLine()) ;
l1=Convert.ToDouble(Console.ReadLine());
h1=Convert.ToDouble(Console.ReadLine());
l2=Convert.ToDouble(Console.ReadLine());
h2=Convert.ToDouble(Console.ReadLine());

area=(2*(l*b + l*h + b*h))-((l1*h1)-(l*b)-2*(l2*h2));

Console.WriteLine("Area={0}",area);
}
}

15)swapping using third variable


interchange value between 2 variables
before swapping - a=100 b=200
after swapping- a=200 b=100

logic
a=b;//a=200
b=a;//b=200 wrong

using System;
class HelloWorld {
static void Main()
{
int a,b,c;
Console.WriteLine("Enter 2 no");//printf
a=Convert.ToInt32(Console.ReadLine());//scanf
b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nBefore Swapping\n A={0} B={1}",a,b);
c=a;//a=100 b=200 c=100
a=b;//a=200
b=c;//b=100
Console.WriteLine("\nAfter Swapping\n A={0} B={1}",a,b);
}
}

16)swapping without using third variable 15(interview question)


using System;

public class HelloWorld


{
public static void Main(string[] args)
{
int a,b;
Console.WriteLine("Enter 2 no");//printf
a=Convert.ToInt32(Console.ReadLine());//scanf
b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nBefore Swapping\n A={0} B={1}",a,b);
a=a+b;//a=12 b=98 a=110
b=a-b;//b=110-98=12
a=a-b;//a=110-12=98

Console.WriteLine("\nAfter Swapping\n A={0} B={1}",a,b);


}
}

17)
i/p h,m,s
o/p seconds

1hr=3600
1min=60
using System;
class HelloWorld {
static void Main()
{
int h,m,s,sec;
Console.WriteLine("Enter H,M,S");
h=Convert.ToInt32(Console.ReadLine());
m=Convert.ToInt32(Console.ReadLine());
s=Convert.ToInt32(Console.ReadLine());
sec=(h*3600)+(m*60)+s;
Console.WriteLine("sec={0}",sec);
}
}

18)
i/p km
o/p m
m=km*1000;

19)
i/p liter
o/p ml
ml=liter*1000;

20)
i/p
meter 2750
o/p km
{
int meter,km;

Console.WriteLine("Enter value of meter");


meter=Convert.ToInt32(Console.ReadLine());
km=meter/1000;
meter=meter%1000;
Console.WriteLine("Km={0}Meter={1}",km,meter);
}

21)
i/p ml
o/p liter

22))Accept seconds from user & display it in h:m:s format


i/p7530
o/p 2:5:30
1hr=3600
1 min=60

/=quotient 17/3=5
%=reMainder 17%3=2

7530/3600=2
7530%3600=330
330/60=5
330%60=30
using System;

public class Sec


{

public static void Main(string[] args)


{
int sec,h,m,s;

Console.WriteLine("Enter value of Seconds");


sec=Convert.ToInt32(Console.ReadLine());
h=sec/3600;
sec=sec%3600;
m=sec/60;
s=sec%60;
Console.WriteLine("{0}:{1}:{2}",h,m,s);

23)Accept amount from user

i/p 8888
o/p
2000*4=8000 CW("2000 * {0} ={1} ",twot,(2000*towt));
500*1=500
200*1=200
100*1=100
50*1=50
20*1=20
10*1=10
5*1=5
2*1=2
1*1=1
______________
total=8888

8888/2000=4
8888%2000=888
888/500=1
888%500=388
388/200=1
388%200=188
.
.
.
.

using System;

public class Rupees


{
public static void Main(string[] args)
{
int rs, twot, fiveh, twoh, oneh, fifty, twen, ten, five, two, one, a;
Console.WriteLine("Enter value of Ruppes");
rs = Convert.ToInt32(Console.ReadLine());
a = rs;
twot = rs / 2000;
rs = rs % 2000;
fiveh = rs / 500;
rs = rs % 500;
twoh = rs / 200;
rs = rs % 200;
oneh = rs / 100;
rs = rs % 100;
fifty = rs / 50;
rs = rs % 50;
twen = rs / 20;
rs = rs % 20;
ten = rs / 10;
rs = rs % 10;
five = rs / 5;
rs = rs % 5;
two = rs / 2;
one = rs % 2;
Console.WriteLine("2000*{0}= {1}", twot, (2000 * twot));
Console.WriteLine("500*{0}= {1}", fiveh, (500 * fiveh));
Console.WriteLine("200* {0}= {1}", twoh, (200 * twoh));
Console.WriteLine("100*{0}= {1}", oneh, (100 * oneh));
Console.WriteLine("50*{0}= {1}", fifty, (50 * fifty));
Console.WriteLine("20*{0}= {1}", twen, (20 * twen));
Console.WriteLine("10*{0}= {1}", ten, (10 * ten));
Console.WriteLine("5*{0}= {1}", five, (5 * five));
Console.WriteLine("2*{0}= {1}", two, (2 * two));
Console.WriteLine("1*{0}= {1}", one, (1 * one));
Console.WriteLine("__________________________________");
Console.WriteLine("total= {0} ", a);
//Console.ReadLine();

23)Accept 4 digit no from user & reverse it


1234
4321
%10
/10

You might also like