Thursday, December 19, 2019

Java Program to Check a Leap Year

Java Program to Check a Leap Year:

public class LeapYear {
    public static void main(String[] args) {
        int year = 1900;
        boolean leap = false;
        if(year % 4 == 0)
        {
            if( year % 100 == 0)
            {
                // year is divisible by 400, hence the year is a leap year
                if ( year % 400 == 0)
                    leap = true;
                else
                    leap = false;
            }
            else
                leap = true;
        }
        else
            leap = false;
        if(leap)
            System.out.println(year + " is a leap year.");
        else
            System.out.println(year + " is not a leap year.");
    }
}

Output:
2020 is a leap year.

Python program to Count Alphabets Digits and Special Characters in a String using for loop

# Python program to Count Alphabets Digits and Special Characters in a String using for loop

string = input("Please Enter your Own String : ")
alphabets = digits = special = 0

for i in range(len(string)):
    if(string[i].isalpha()):
        alphabets = alphabets + 1
    elif(string[i].isdigit()):
        digits = digits + 1
    else:
        special = special + 1
       
print("\nTotal Number of Alphabets in this String :  ", alphabets)
print("Total Number of Digits in this String :  ", digits)
print("Total Number of Special Characters in this String :  ", special)

Python program to make a simple calculator

#  Python program to make a simple calculator
# This function adds two numbers
def add(x, y):
   return x + y
# This function subtracts two numbers
def subtract(x, y):
   return x - y
# This function multiplies two numbers
def multiply(x, y):
   return x * y
# This function divides two numbers
def divide(x, y):
   return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")


Output

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0

Write C++ program to caulate height of Binary Tree from Inorder and Levelorder Traversals

Write C++ program to caulate height of Binary Tree from Inorder and Levelorder Traversals

#include  
using namespace std;
 
/* Function to find index of value 
   in the InOrder Traversal array */
int search(int arr[], int strt, int end, int value)
{
    for (int i = strt; i <= end; i++)
        if (arr[i] == value)
            return i;
    return -1;
}
 
// Function to calculate the height of the Binary Tree
int getHeight(int in[], int level[], int start,
              int end, int& height, int n)
{
 
    // Base Case
    if (start > end)
        return 0;
 
    // Get index of current root in InOrder Traversal
    int getIndex = search(in, start, end, level[0]);
 
    if (getIndex == -1)
        return 0;
 
    // Count elements in Left Subtree
    int leftCount = getIndex - start;
 
    // Count elements in right Subtree
    int rightCount = end - getIndex;
 
    // Declare two arrays for left and
    // right subtrees
    int* newLeftLevel = new int[leftCount];
    int* newRightLevel = new int[rightCount];
 
    int lheight = 0, rheight = 0;
    int k = 0;
 
    // Extract values from level order traversal array for current left subtree
    for (int i = 0; i < n; i++) {
        for (int j = start; j < getIndex; j++) {
            if (level[i] == in[j]) {
                newLeftLevel[k] = level[i];
                k++;
                break;
            }
        }
    }
 
    k = 0;
 
    // Extract values from level order traversal array for current right subtree
    for (int i = 0; i < n; i++) {
        for (int j = getIndex + 1; j <= end; j++) {
            if (level[i] == in[j]) {
                newRightLevel[k] = level[i];
                k++;
                break;
            }
        }
    }
 
    // Recursively call to calculate height of left Subtree
    if (leftCount > 0)
        lheight = getHeight(in, newLeftLevel, start,
                            getIndex - 1, height, leftCount);
 
    // Recursively call to calculate height of right Subtree
    if (rightCount > 0)
        rheight = getHeight(in, newRightLevel,
                            getIndex + 1, end, height, rightCount);
 
    // Current height
    height = max(lheight + 1, rheight + 1);
 
    // Delete Auxiliary arrays
    delete[] newRightLevel;
    delete[] newLeftLevel;
 
    // return height
    return height;
}
 
// Driver program to test above functions
int main()
{
    int in[] = { 4, 8, 10, 12, 14, 20, 22 };
    int level[] = { 20, 8, 22, 4, 12, 10, 14 };
    int n = sizeof(in) / sizeof(in[0]);
 
    int h = 0;
 
    cout << getHeight(in, level, 0, n - 1, h, n);
 
    return 0;

Write a CPP program to find the minute at which the minute hand and hour hand coincide.

Write a CPP program to find the minute at which the minute hand and hour hand coincide.

#include
#include  
using namespace std;

void find_time(int h1)
{

  int theta = 30 * h1;
  cout << "(" << (theta * 2) << "/"
       << "11"
       << ")"
       << " minutes";
}

int main()
 {
  int h1 = 3; 
  find_time(h1); 
  return 0;
}


Output :
(180/11) minutes

Wednesday, December 18, 2019

Python program to check if year is a leap year or not

# Python program to check if year is a leap year or not
#to check the year
#year = 2020

#To get year (integer input) from the user
year = int(input("Enter a year: "))

if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print("{0} is a leap year".format(year))
       else:
           print("{0} is not a leap year".format(year))
   else:
       print("{0} is a leap year".format(year))
else:
   print("{0} is not a leap year".format(year))



Output:

Enter a year: 2020
2020
2020 is a leap year

Tuesday, December 17, 2019

The Countries of the World and their Capitals(general knowledge question)

List of countries of the World and their Capitals:

Confirmation Box - onClick in javascript program



Confirmation Box - onClick in javascript program:

<input id="confirm" type="button" value="Click me" onclick="confirm('Are you sure?');">



JavaScript program to print this page using onClick

Print this page on click in JavaScript program:

<input id="print" type="button" value="Print this Page" onclick="window.print();">



Javascript program to display alert box on click


Alert box on click in javascript program:

<input type="button" value="Click me" onclick="alert('Thanks for visiting my blog.....!');">


Featured posts

Happy Independence Day August 15th

 Here's a message for India's Independence Day (August 15th): "शुभ स्वतंत्रता दिवस! आजादी की 79वीं वर्षगांठ पर, आइए हम अपने देश...

Popular posts