simple program for the fresher asked in interview
In the previous post i give you simple program that is asked in interview frequently and here the next part of that post
in this post we will learn the use of while, repeat- while , for loop , switch and function.
the below programs are very easy and these program should be know by the every computer science student who is fresher.
To write these program i use the swift programming language and you can use any programming language with you familiar.
These program are for the basic concept and increase the idea about programming and improve the idea.
and improve the logic the program.
(1) use of repeat-while loop to print count
func count(num: Int)-> Int{
var n = num
var count = 0
repeat {
n = n / 10
count = count + 1
} while(n > 0)
return count
}
print(count(num: 145000331))
Output:- 9
(2) use of the function and for loop to write the program to print count
func counting(num: Int){
for i in 1...10{
print(i)
}
}
print(counting(num: 10))
output:-
1
2
3
4
5
6
7
8
9
10
(3) program to multiply two number with out using of the * operator
func multiplyTwoDigit(a: Int,b: Int)->Int{
var result = 0
for i in 1...b{
result = a + result
}
return result
}
print(multiplyTwoDigit(a: 34, b: 4))
output:- 136
(4) program to generate series like 1,2,4,7,11,16 till N
func series (a: Int, b: Int){
var n = a
print(n)
for i in 1...b{
n = n + i
print(n)
}
}
print(series(a: 1, b: 10))
output:-1
2
4
7
11
16
22
29
37
46
56
(5) Program to generate fibonacci series
func fiboSeries(b: Int){
var m = 0
var n = 1
var o: Int
for i in 1...b{
o = m + n
print(o)
m = n
n = o
}
}
print(fiboSeries(b: 10))
Output:-
1
2
3
5
8
13
21
34
55
89
(6) Program to find whether a number is prime or not
func primeNumber(Num: Int){
var flag: Bool = false
for i in 2...Num / 2 {
if (Num % i == 0){
flag = true
break
}
}
if flag == false {
print("\(Num) is prime Number")
}else{
print("\(Num) is not Prime Number")
}
}
print(primeNumber(Num: 23))
output:-
23 is prime Number
// learn to make extension of Integer this is the specific with the swift program and through this program we can see how can we make the extension of the int according to our use.
extension Int{
func sayHello(){
print("hello, I'm \(self)")
}
}
2.sayHello()
extension String{
func changeIntoInteger(){
print("q hilla dala na \(self)")
}
}
output:-
hello, I'm 2
(7) Learn to use the switch control flow in swift
func swicthStatement(age : Int){
switch age{
case 1:
print("i am IRON MAN")
case 2:
print(" i am from NEWYORK")
case 3:
print(" i am three year late in my life")
default:
print("but one day i will success definitly i do some thing big which is remmbered by everyone")
}
}
print(swicthStatement(age: 25 ))
output:- but one day i will success definitly i do some thing big which is remmbered by everyone
(8) program to perform arithmetic calculations on integers
func acceptASign(sign: Character) {
switch sign {
case "+":
print(30 + 40)
case "-":
print(40 - 30)
case "*":
print(40 * 30)
case "/":
print( 40 / 30)
default:
print( 40 % 30)
}
}
print(acceptASign(sign: "+"))
print(acceptASign(sign: "-"))
print(acceptASign(sign: "*"))
print(acceptASign(sign: "/"))
print(acceptASign(sign: "%"))
print(acceptASign(sign: ">"))
output:-
70
10
1200
1
10
10
(9) program to check enter charecter is vowel or constant
func enterCharecter(alphbet: Character){
switch alphbet {
case "a":
print("\(alphbet) is voewl")
case "e":
print("\(alphbet) is voewl")
case "i":
print("\(alphbet) is voewl")
case "o":
print("\(alphbet) is voewl")
case "u":
print("\(alphbet) is voewl")
default:
print("\(alphbet) is constant")
}
}
print(enterCharecter(alphbet: "j"))
print(enterCharecter(alphbet: "a"))
output:-
j is constant
a is voewl
(10) program to check date is valid or not
func enterDate(date: Int,month: Int, year: Int){
var flag = true
if(month < 0 && month > 12){
flag = false
}else if(date < 0 && date > 31){
flag = false
}else if(year < 1990 && year > 2020){
flag = false
}
if(flag == true){
print("Date is Valid")
}else{
print("Date is inValid")
}
}
print(enterDate(date: 12, month: 4, year: 1998))
output:-
Date is Valid.
these program are very easy practice these program. if you find any problem comment me.
Thank you.
Comments
Post a Comment