Practice Basic Program Question Set -1

Welcome!

Here, you would find practice sets for some basic questions and their solutions that are solved in 5 different programming languages c, c++, java, kotlin, and python.

All programs have been tested.

Program develop tools

  • VS code - text editor
  • Linux - operating system
  • java 11
  • kotlin CLI
  • python 3.7
  • g++ for c and c++
you can find all code at Github

Practical

1.
Write a Hello world program

output :



Hello, world!
            
    class HelloWorld{
        public static void main(String[] args){
            System.out.printl("hello, world!");
        }
    }
            
            
        
            
    print("Hello world!")
            
        
            
    #include<stdio.h>
    void main(){
        printf("Hello, world!");
    }
            
    
            
    #include<iostream.h>
    using namespace std
    int main(){
        cout >> "hello";
    }
            
    
        
    fun main() {
        println("Hello World!")
    }
        
    
2.
Write a program to perform all arithmetic operations.

input :



num1 = 10
num2 = 5

output :



sum= 15
different=5
multiply=50
divide=2
moduls=0
                
    package programjava;

    public class Arithmetic {
        public static void main(String[] args) {
            int num1 = 10;
            int num2 = 5;
            int sum = num1 + num2,
                dif = num1 - num2,
                mul = num1 * num2,
                div = num1 / num2,
                mod = num1 % num2;

            System.out.println("sum="+sum);
            System.out.println(" different="+dif);
            System.out.println(" multiply="+mul);
            System.out.println(" divide="+div);
            System.out.println(" moduls="+mod);
        

        }
    }
            
            
                
            
    num1 = 10
    num2 = 5
    sum = num1 + num2
    diff = num1 - num2
    mul = num1 * num2
    div = num1 / num2
    mod = num1 % num2
    print("sum ",sum)
    print("different ",diff)
    print("multiply ",mul)
    print("divide ",div)
    print("moduls ",mod)
            
        
            
    #include<stdio.h>
    int main(){
        int num1 = 10;
        int num2 = 5,sum,dif,mul,div,mod;
        sum = num1 + num2;
        dif = num1 - num2;
        mul = num1 * num2;
        div = num1 / num2;
        mod = num1 % num2;

        printf("sum= %d\n ",sum);
        printf("different= %d\n ",dif);
        printf("multiply= %d\n ",mul);
        printf("divide= %d\n ",div);
        printf("moduls= %d\n ",mod);
        return 0;
    }
        
    
            
    #include<iostream>
    using namespace std;

    int main(){
        int num1 = 10;
        int num2 = 5,sum,dif,mul,div,mod;
        sum = num1 + num2,
        dif = num1 - num2,
        mul = num1 * num2,
        div = num1 / num2,
        mod = num1 % num2;

        cout << "sum=" << sum << " \n ";
        cout << "different=" << dif << " \n ";
        cout << "multiply=" << mul << " \n ";
        cout << "divide=" << div << " \n ";
        cout << "moduls=" << mod << " \n ";
        return 0;
    }
            
    
        
    fun main(args: Array<string>) {
        val num1 : Int= 10
        val num2 : Int= 5
        val sum : Int= num1 + num2
        val dif : Int= num1 - num2
        val mul : Int= num1 * num2
        val div : Int= num1 / num2
        val mod : Int= num1 % num2
    
        println("sum="+sum)
        println(" different="+dif)
        println(" multiply="+mul)
        println(" divide="+div)
        println(" moduls="+mod)
    }
        
    
3.
Write a program to swap two number.

input :



a = 5
b = 10

output :



a= 10
b= 5
                
    package programjava;

    public class swap {
        public static void main(String[] args) {
            int a = 5;
            int b = 10;
            
            a = a + b;
            b = a -b;
            a = a - b;
            System.out.println("a= "+ a);
            System.out.println("b= "+b);
        }
    }        
            
                
            
    a = 5
    b = 10
    (a,b) = (b,a)
    print(f'a={a}\nb={b}')
            
        
            
    #include<stdio.h>

    int main(){
        int a = 5,b = 10;   
        a = a + b;
        b = a -b;
        a = a - b;
        printf("a= %d\n",a);
        printf("b= %d",b);
        return 0;
    }
            
    
            
    #include<iostream>
    using namespace std;
    int main(){
        int a = 5,b = 10;   
        a = a + b;
        b = a -b;
        a = a - b;
        cout << "a= " << a<< endl;
        cout << "b= " << b;
        return 0;
    }
            
    
        
    fun main(args: Array<string>) {
        var a = 5
        var b = 10
        
        a = a + b
        b = a -b
        a = a - b
        println("a = "+ a)
        println("b = "+b)
    }
        
    
4.
Write a program to check whether the given number is odd or even.

input :



5

output :



odd number
                
    package programjava;
    import java.util.Scanner;
    class CheckEven {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter number: ");
            int num = scanner.nextInt();
            scanner.close();

            if(num % 2 == 0){
                System.out.println("Even number");
            
            }
            else System.out.println("Odd number");
        }
    }
            
                
            
    num = int(input("Enter number>"))
    if (num % 2 == 0):
        print("Even number")
    else:
        print("Odd number")
    
            
        
            
    #include<stdio.h>

    
    int main(){
        int num;
            printf("Enter number: ");
            scanf("%d",&num);
            if(num % 2 == 0){
                printf("Even number");
            
            }
            else printf("Odd number");
        
        return 0;
        
    }
            
    
            
    #include<iostream>
    
    using namespace std;
    
    int main(){
        int num;
        cout << "Enter number: ";
        cin >> num;
        if(num % 2 == 0){
            cout << "Even number";
        
        }
        else cout << "Odd number";
        return 0;
        
    }
            
    
        
    import java.util.*
    fun main(args: Array<string>) {
        val scanner : Scanner = Scanner(System.`in`)
        print("Enter number: >")
        val num : Int = scanner.nextInt();
        scanner.close();
        if(num % 2 == 0){
            println("Even number");
        
        }
        else println("Odd number");
    }
        
    
5.
Ask user to input a number and check whether number is divisible by 3 and 13.

input :



3939

output :



number is divisible by 3 and thirteen
                
    package programjava;

    import java.util.Scanner;
    
    public class DivideByThreeAndThirteen {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter number: ");
            int num = scanner.nextInt();
            scanner.close();
            if(num % 3 == 0 && num % 13 == 0){
                System.out.println("number is divisible by 3 and thirteen");
            }
            else{
                System.out.println("number is not divisible by 3 and thirteen");
            }
            
        }
    }
            
                
            
    num = int(input("Enter number>"))
    if(num  % 13 == 0 & num % 3 == 0):
        print("number is divisible by 3 and thirteen")
    else:
        print("number is divisible by 3 and thirteen")
    
            
        
            
    #include<stdio.h>

    int main(){
        int num;
        printf("Enter number: ");
        scanf("%d",&num);
        if(num % 3 == 0 && num % 13 == 0){
            printf("number is divisible by 3 and thirteen");
        }
        else{
            printf("number is not divisible by 3 and thirteen");
        }
        return 0;
    }
        
    
            
    #include<iostream>
    
    using namespace std;
    int main(){
        int num;
        cout << "Enter number: ";
        cin >> num;
        if(num % 3 == 0 && num % 13 == 0){
        cout << "number is divisible by 3 and thirteen";
        }
        else{
            cout << "number is not divisible by 3 and thirteen";
        }
        return 0;
}
            
    
        
    import java.util.*
    fun main(args: Array<string>) {
        val scanner = Scanner(System.`in`)
        print("Enter number: >")
        val num : Int= scanner.nextInt()
        
        if(num % 3 == 0 && num % 13 == 0){
            println("number is divisible")
        }
        else{
            println("number is not divisible")
        }
    }
        
    
6.
Ask the user to enter four digit number and check whether the sum of first and last number is sam as the sum of second no. and last number.

input :



1234

output :



Sum of first and fourth and the sum of the second and third is the same
                
    package programjava;
    import java.util.*;
    
    class NumberOp{
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter four digit number: ");
            int num = scanner.nextInt();
            scanner.close();
            if(num >999 && num < 10000){
                int first = num / 1000;
                num = (int) num % 1000;
                int second = num / 100;
                num = (int) num % 100;
                int third = num / 10;
                num = (int) num % 10;
                int fourth = num;
                if(first + fourth == second + third){
                    System.out.println("Sum of first and fourth and sum of second and third is same");
                }
                else{
                    System.out.println("Sum of first and fourth and sum of second and third is not same");
                }
            }else{
                System.out.println("Enter number greather than 999 and less than 10000");
            }       
        }
    }
            
                
            
    num = int(input(("Enter four digit number: ")))
    if(num >999 & num < 10000):
        first = int(num / 1000)
        num = int(num % 1000)
        second = int(num / 100)
        num = int(num % 100)
        third = int(num / 10)
        num = int(num % 10)
        fourth = num
        if(first + fourth == second + third):
            print("Sum of first and fourth and sum of second and third is same")
        
        else:
            print("Sum of first and fourth and sum of second and third is not same")
        
    else:
        print("Enter number greather than 999 and less than 10000")
    
            
        
            
    #include<stdio.h>

    int main(){
        int num,first,second,third,fourth;
        printf("Enter four digit number: ");
        scanf("%d",&num);
        if(num >999 && num < 10000){
            first = num / 1000;
            num = num % 1000;
            second = num / 100;
            num =  num % 100;
            third = num / 10;
            num =  num % 10;
            fourth = num;
            if(first + fourth == second + third){
                printf("Sum of first and fourth and sum of second and third is same");
            }
            else{
                printf("Sum of first and fourth and sum of second and third is not same");
            }
        }else{
            printf("Enter number greather than 999 and less than 10000");
        }       
        return 0;
    }
        
    
            
    #include<iostream>
    
    using namespace std;

    int main(){
        int num,first,second,third,fourth;
        cout << "Enter four digit number: ";
        cin >> num;
        if(num >999 && num < 10000){
            first = num / 1000;
            num = num % 1000;
            second = num / 100;
            num =  num % 100;
            third = num / 10;
            num =  num % 10;
            fourth = num;
            if(first + fourth == second + third){
                cout << "Sum of first and fourth and sum of second and third is same";
            }
            else{
                cout << "Sum of first and fourth and sum of second and third is not same";
            }
        }else{
            cout << "Enter number greather than 999 and less than 10000";
        }       
        return 0;
    }
            
    
        
    import java.util.Scanner
    fun main(args: Array<string>) {
        val scanner = Scanner(System.`in`)
        print("Enter four digit number: ")
        var num : Int = scanner.nextInt()
        if(num >999 && num < 10000){
            val first : Int= num / 1000
            num = num % 1000
            val second : Int= num / 100
            num = num % 100
            val third : Int= num / 10
            num =  num % 10
            val fourth : Int = num
            if(first + fourth == second + third){
                println("Sum of first and fourth and sum of second and third is same")
            }
            else{
                println("Sum of first and fourth and sum of second and third is not same")
            }
        }else{
            println("Enter number greather than 999 and less than 10000")
        }       
    }
        
    
7.
Ask the user to enter two number, check and print the maximum and minimum number

input :



5
2

output :



Maximum Number = 5
Minimum Number = 2
                
    package programjava;
    import java.util.Scanner;
    
    public class MaxMin {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter first number >");
            int first = scanner.nextInt();
            System.out.print("Enter second number >");
            int second = scanner.nextInt();
            scanner.close();
            if(first > second){
                System.out.println("Maximum Number "+ first);
                System.out.println("Minumum number "+ second);
            }else{
                System.out.println("Maximum Number "+ second);
                System.out.println("Minumum number "+ first);
            }
            }
    }
            
                
            
    first = int(input("Enter First number > "))
    second = int(input("Enter second number > "))
    
    if(first > second):
        print("Maximum Number = ", first)
        print("Minumum number = ", second)
    else:
        print("Maximum Number = ",second)
        print("Minumum number = ",first)
            
    
            
        
            
    #include<stdio.h>

    int main(){
        int first,second;
        printf("Enter first number >");
        scanf("%d",&first);
        printf("Enter second number >");
        scanf("%d",&second);
        
        if(first > second){
            printf("Maximum Number = %d\n",first);
            printf("Minumum number = %d",second);
        }else{
            printf("Maximum Number = %d\n",second);
            printf("Minumum number = %d",first);
        }
        return 0;
    }
        
    
            
    #include<iostream>
    
    using namespace std;
    int main(){
        int first,second;
        cout << "Enter first number >";
        cin >> first;
        cout << "Enter second number >";
        cin >> second;
        
        if(first > second){
            cout << "Maximum Number = " << first << endl;
            cout << "Minumum number = " << second << endl;
        }else{
            cout << "Maximum Number = " << second << endl;
            cout << "Minumum number = " << first << endl;
        }
        return 0;
    }
            
    
        
    import java.util.Scanner

    fun main(args: Array) {
        val scanner = Scanner(System.`in`);
        print("Enter first number >");
        val first = scanner.nextInt();
        print("Enter second number >");
        val second = scanner.nextInt();
        if(first > second){
            System.out.println("Maximum Number "+ first);
            System.out.println("Minumum number "+ second);
        }else{
            System.out.println("Maximum Number "+ second);
            System.out.println("Minumum number "+ first);
        }
    }
        
    
8.
write a program to calculate the area and circumference of the circle.

input :



5
2

output :



Area of circle= 314.0
circumference of circle= 62.800000000000004
                
    package programjava;
    /*
    * Java program to calculate area of circle and circumference of circle
    * area of circle = 3.14 * r * r
    * circumference of circle = 2 * 3.14 * r
        
        Let's Code
    
    */
    
    public class AreaOfCircle {
        public static void main(String[] args) {
            int r = 10;
            double area = 3.14 * r * r;
            double cf = 2 * 3.14 * r;
            System.out.println("Area of circle= "+area);
            System.out.println("circumference of circle= "+cf);
        }
    }
            
                
            
    r = 10
    area = 3.14 * r * r
    cf = 2 * 3.14 * r
    print("Area of circle=",area)
    print("circumference of circle=",cf)
    
            
        
            
    #include<stdio.h>

    int main() {
        int r = 10;
        float area,cf;
    
        area = 3.14 * r * r;
        cf = 2 * 3.14 * r;
    
        printf("Area of circle= %f\n",area);
        printf("circumference of circle= %f",cf);
    }
        
    
            
    #include<iostream>
    
    using namespace std;
    int main() {
        int r = 10;
        float area,cf;
    
        area = 3.14 * r * r;
        cf = 2 * 3.14 * r;
    
        cout << "Area of circle= " << area <<"\n";
        cout << "circumference of circle= " << cf;
    }
            
    
        
    import java.util.Scanner

    fun main(args: Array<string>) {
    
        val r : Int = 10
        val area :Double= 3.14 * r * r
        val cf : Double= 2 * 3.14 * r
        println("Area of circle= "+area)
        println("circumference of circle= "+cf)
    
    }
        
    
9.
write a program to find the power of number.

input :



num = 5
p = 4

output :



5^4=625
                
    package programjava;

    public class Power {
        public static void main(String[] args) {
            int num = 5, p = 4;
            int powerOf = (int) Math.pow(num, p);
            System.out.print(num + "^" + p +" ="+powerOf);
        }
    }
            
                
            
    num1 = 5
    num2 = 4
    powerOf = 5**4
    print(f'{num1}^{num2} = {powerOf}')
    
            
        
            
    #include<stdio.h>

    int main(){
        int num = 5, p = 4,i,result = 1;
        for(int i = 0; i < 4; i++){
            result *= 5;
        }
        printf("%d^%d = %d",num,p,result);
        return 0;
    }
        
    
            
    #include<iostream>
    
    using namespace std;
    
    int main(){
        int num = 5, p = 4,i,result = 1;
        for(int i = 0; i < 4; i++){
            result *= 5;
        }
        cout << num << "^"<< p <<"= " << result;
        return 0;
    }
            
    
        
    fun main(args: Array<string>) {
        val num = 5
        val p = 4
        val powerOf=  Math.pow(num.toDouble(), p.toDouble())
        print(num.toString() + "^" + p +" ="+powerOf)
    }
        
10.
write a program to check whether the number is palindrome or not.

input :



121

output :



palindrome no.
                
    package programjava;
    
    public class Power {
        public static void main(String[] args) {
            int num = 5, p = 4;
            int powerOf = (int) Math.pow(num, p);
            System.out.print(num + "^" + p +" ="+powerOf);
        }
    }
            
                
            
    num1 = 5
    num2 = 4
    powerOf = 5**4
    print(f'{num1}^{num2} = {powerOf}')
    
            
        
            
    #include<iostream>

    using namespace std;
    int main(){
        int r,sum = 0, temp,num;
        printf("Enter number: ");
        cin >> num;
        
        temp = num;
        while(num > 0){
            r = num % 10;
            sum = (sum * 10) + r;
            num = num/10;
        }
        if(temp == sum){
            cout  << "Palindrome number";
        }
        else{
            cout << "not palindrome number";
        }
    }
            
    
        
    import java.util.*

    fun main(args: Array<string>) {
        val scanner = Scanner(System.`in`)
        print("Enter number: ")
        var num = scanner.nextInt()
        scanner.close()
        var r : Int
        var sum : Int= 0
        val temp = num
        while(num > 0){
            r = num % 10
            sum = (sum * 10) + r
            num = num/10
        }
        if(temp == sum){
            println("Palindrome number")
        }
        else{
            println("not palindrome number")
        }
    }
        
    

Comments

Post a Comment