Session 3: Basics of Programming
Agenda - Revision of basic concepts in C++, Java and Python
Duration - 3 hours
Conditional Statements - If, If-Else, If-Else-If ladder, Nested If-Else, Switch Case
Loops - For, While, Do-While, For-Each
Arrays - 1D and 2D
- WAP to accept a score from a student. Print a congratulatory message if the score of student is greater than 75
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score; // score = 79
/*
if(<condition>){
line 1
line 2
}
*/
if(score>75){
cout << "Congratulations!!" << endl;
cout << "Well done!" << endl;
}
return 0;
}
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.print("Enter your score: ");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt(); // scoer = 78
if(score>75){
System.out.println("Congratulations!!");
System.out.println("Partyyyyy!!");
}
}
}
score = int(input("Enter your score: "))
if(score>75):
print("Congratulations!")
print("Partyyyy")
- WAP to accept a score from a student. Print a congratulatory message if the score of student is greater than 75. If the score is ≤ 75, print some relevant message.
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score; // score = 79
if(score>75){
cout << "Congratulations!!" << endl;
cout << "Well done!" << endl;
}
else{
cout << "Better luck next time" << endl;
}
return 0;
}
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.print("Enter your score: ");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt(); // score = 78
if(score>75){
System.out.println("Congratulations!!");
System.out.println("Partyyyyy!!");
}
else{
System.out.println("Better luck next time");
System.out.println("Cheer up!");
}
}
}
score = int(input("Enter your score: "))
if(score>75):
print("Congratulations!")
print("Partyyyy")
else:
print("Sorry, you've failed")
print("You can try again next year.")
If BBD sale = Buy from Flipkart
Else if TGIF sale = Buy from Amazon
Else if ABCD sale = Buy from ABCD
Else = Buy from store
- WAP to accept an integer from a user and print the corresponding message based on the following conditions
- Divisible by both 5 and 8 ⇒ Print 58
- Divisible only by 5 ⇒ Print 5
- Divisible only by 8 ⇒ Print 8
- Divisible by neither 5 nor 8 ⇒ Print “None”
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.print("Enter your number: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); // score = 78
if(num%5==0 && num%8==0)
System.out.println(58);
else if(num%5==0)
System.out.println(5);
else if(num%8==0)
System.out.println(8);
else
System.out.println("None");
}
}
- WAP to print Gold Medal if score of student is 100.
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score; // score = 79
if(score==100)
cout << "Gold Medal" << endl;
return 0;
}
= → Assignment operator
== → Comparison operator
- WAP to find largest of 3 distinct numbers using Nested If-Else
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a = 300, b = 50, c = 70;
if(a>b){
// b is out of race
if(a>c)
System.out.println("A is the greatest");
else
System.out.println("C is the greatest");
}
else{
// b>a
// a is out of race
if(b>c)
System.out.println("B is the greatest");
else
System.out.println("C is the greatest");
}
}
}
- WAP to print multiplication table of a number
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number: ");
int num = sc.nextInt();
for(int i=1;i<=10;i++)
System.out.println(num + " * " + i + " = " + (num*i));
}
}
- WAP to print multiplication table of a number. Skip the 5th multiple always.
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number: ");
int num = sc.nextInt();
for(int i=1;i<=10;i++){
if(i==5)
continue;
System.out.println(num + " * " + i + " = " + (num*i));
}
}
}
- WAP to print multiplication table of a number. If any multiple comes up as 42, exit out of loop.
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number: ");
int num = sc.nextInt();
for(int i=1;i<=10;i++){
if(num*i==42)
break;
System.out.println(num + " * " + i + " = " + (num*i));
}
}
}
- WAP to keep taking integral input from user till they enter 42
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("Enter your number: ");
int num;
num = sc.nextInt();
if(num==42)
break;
}
}
}
- WAP to find sum of first N odd natural numbers.
Examples -
N = 5
1+3+5+7+9 = 25
N = 4
1+3+5+7= 16
public class codefile{
static int solve(int n){
int sm = 0;
for(int i=1;i<2*n;i+=2)
sm += i;
return sm;
}
}
- Primality Check: Given a number, check if it is prime or not.
A number is prime if it is divisible only by 1 and itself.
public class codefile{
static boolean check(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
}
- Concatenate the array: Given an Array of size N. Create another array of size 2*N and place the array first and then its copy in the new array.
A = [2,4,3]
ans = [2,4,3,2,4,3]
A = [6,4,1,7]
ans = [6,4,1,7,6,4,1,7]
public class codefile{
static int[] solve(int[] input){
int n = input.length;
int []ans = new int[2*n];
for(int i=0;i<n;i++){
ans[i] = input[i];
ans[i+n] = input[i];
}
return ans;
}
}
- Given an array, create running sum array from it.
arr = [2,4,3,1,0,9]
rSum =[2,6,9,10,10,19]
public class codefile{
static int[] sum(int[] input){
int n = input.length;
int sm = 0;
int []ans = new int[n];
for(int i=0;i<n;i++){
sm += input[i];
ans[i] = sm;
}
return ans;
}
}
TC: O(N)
Sum of diagonals
Time Complexity: N
public class codefile{
static int solve(List<List<Integer>> input){
int sm = 0;
int n = input.size();
for(int i=0;i<n;i++){
sm+=input.get(i).get(i);
if(i!=n-i-1)
sm+=input.get(i).get(n-i-1);
}
return sm;
}
}
Richest Customer Wealth
public class codefile{
static int solve(List<List<Integer>> input){
int ans = 0;
int n = input.size();
int m = input.get(0).size();
for(int i=0;i<n;i++){
int sm = 0;
for(int j=0;j<m;j++)
sm += input.get(i).get(j);
if(sm>ans)
ans = sm;
}
return ans;
}
}