Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners.

1 .write a cpp program to display hello world message
#include<iostream.h>
int main()
{
count<<"Hello World\n";
//prints Hello World on user screen
return 0;
}

Output:

Hello World

2. cpp program to use cout and cin

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int a,b,s;

cout<<"enter first num:";
cin>>a;
cout<<"enter second num:";
cin>>b;

s=a+b;

cout<<"\n sum= ";
cout<<s;

getch();
}

output:
Enter first Num:10
Enter second Num:20
sum=30

3. write a cpp program to use variables

#include<iostream.h>
using namespace std;

int main()
{
//defination of variables
int a,b,c;
float d;

//initialization of variables

a=5;
b=10;
c=a+b;

cout<<c<endl;

d=15.0/4.0;
cout<<d<<endl;

return 0;
}

output:
15
3.75

4 CPP program to swap two numbers using thrid variable

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();

int a,b,temp;

cout<<"\n enter two numbers :";
cin>>a>>b;

cout<<"\n a="<<a<<",b="<<b;

temp=a;
a=b;
b=temp;

cout<<"\n a=" <<a",b="<<b;

getch();
}
Output:
enter two numbers: 10 20
a=10,b=20
a=20,b=10

5.CPP program to swap two numbers without thrid variable
#include<iostream.h>
#include<conio.h>

void main()
{


int a,b;
clrscr();

cout<<"enter the value of a and b:"<<endl;
cin>>a>>b;

cout<<"before swap:\n"<<"A:"<<a<<"\n B:<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"after swap:\n"<<"A:"<<a<<"\n B:<<b<<endl;

getch();
}
Output:
Enter the value of a and b:
10 20
before swap:
A:10
B:20
after swap:
A:20
B:10


Switch case programs using C

In this post you will learn switch case programs using C programming language.The control statement that allows us to make a decision from the number of choices is called a  switch statement.

C program to find day using switch case

#include<stdio.h>
#include<conio.h>
void mian()
{
int day;
clrscr();
printf("enter day number:");
scanf("%d",&day);
switch(day)
{
case 1:

               printf("monday");
               break;
case 2:

               printf("Tuesday");
               break;
case 3:

               printf("Wednesday");
               break;
case 4:

               printf("Thursday");
               break;
case 5:

               printf("Friday");
               break;
case 6:

               printf("Saturday");
               break;
case 7:

               printf("Sunday");
               break;
default:
              printf("invalid day");
             
}
Output:
Enter day number: 4
Thursday

Example 2:

#include<stdio.h>
void main( )
{
    int a, b, c, choice;
    while(choice != 3)
    {
        /* Printing the available options */
        printf("\n 1. Press 1 for addition");
        printf("\n 2. Press 2 for subtraction");
        printf("\n Enter your choice");
        /* Taking users input */
        scanf("%d", &choice);
     
        switch(choice)
        {
            case 1:
                printf("Enter two numbers");
                scanf("%d%d", &a, &b);
                c = a + b;
                printf("%d", c);
                break;
            case 2:
                printf("Enter two numbers");
                scanf("%d%d", &a, &b);
                c = a - b;
                printf("%d", c);
                break;
            default:
                printf("you have passed a wrong key");
                printf("\n press any key to continue");
        }
    }
}
Output:
Enter your choice: 1
Enter two numbers 10 20
30


Example 3:

int main()
{
int i;
printf("enter an integer");
scanf("%d",&i);
switch(i)
{
case 1: printf("one");
            break;
case 2: printf("two");
            break;
case 3: printf("three");
            break;
case 4: printf("four");
             break;
default: printf("invalid number");
}
return 0;
}

Output:
Enter an integer: 3
three






Java Exception Handling programs

A Java Exception is an object that describes the exceptional condition that has occurred in a piece of code. These Exceptions handling is managed by try,catch,throw,throws and finally.In this article,we will learn some exception handling java programs which are frequently asked in the technical written Test  and interview room also. Exception means runtime errors,it is an unexpected unwanted event which disturbs entire flow of the program. If we are not handling exception,the program may terminate abnormally without releasing allocated resource. You must notice one thing about Exception concept, Exception handling means we are not repairing exception we are providing alternative way to continue the program normally.

1) Java Program for NullPointerException:

It is the child class or Runtime Exception and it is unchecked,thrown automatically by the JVM whenever we are performing any operation on null.
 
Program:
class Exception1
{
public static void main(String args[])
{
try{
String s=null;
System.out.println(s.length());
}
catch(NullPointerException e)
{
System.out.println("NullPointerException");
}
}
Output:
javac Exception1.java
java Exception1
NullPointerException

.Program 2: ArrayIndexOutOfBoundsException

class Exception2
{
public static void main(String args[]){
try{
int arr[]=new int[10];//here array has 10 elements only
arr[11]=8;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}
Output:

javac Excpetion2.java
java Exception2
ArrayIndexOutOfBoundException

Program 3: StringIndexOutOfBoundException

class StringException
{
public static void main(String args[])
{
try{
String s="hello lucky";
System.out.println(s.length());
char c=s.charAt(0);
c=s.charAt(30);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("StringIndexOutOfBoundException");
}
}
}
Output: 
javac StringException.java
java StringException
StringIndexOutOfBoundException

Program 4: NumberFormatException

class NumberFormat
{
public static void main(String args[])
{
try{
int num=Integer.parseInt("abc");
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println("NumberFormatException");
}
}
}
Output: 
javac NumberFormat.java
java NumberFormat
NumberFormatException

Program 5:  How to throw an already defined exception using throw keyword 
class Exception5
{
static int sum(int num1, int num2)
{
if(num1==0)
throw new ArithmeticException("First parameter is not valid");
else
System.out.println("both parameters are correct");
return num1+num2;
}
public static void main(String args[])
{
int res=sum(0,16);
System.out.println(res);
}
}
Output:






Program 6: How to throw your own Exception explicitly using throw

class MyOwnException extends Exception
{
public MyOwnException(String msg)
{
super(msg);
}
}
class Exception6
{
static void employeeAge(int age)
throws MyOwnException
{
if(age<0)
throw new MyOwnException("Age can not be less than zero");
else
System.out.println("input is valid");
}
public static void main(String args[])
{
try
{
employeeAge(-3);
}
catch(MyOwnException e)
e.printStackTrace();
}
}
}
Output:




Program 7 : Multiple catch Blocks
class Exception7
{
public static void main(String args[])
{
try{
int a[]=new int[9];
a[4]=20/0;
System.out.println("First print statement in try block");
}
catch(ArithmeticException e)
{
System.out.println("Warning ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Warning ArrayIndexOutOfBoundsException");
}
catch(Exception e)
{
System.out.println("Warning some other exception");
}
System.out.println("out of try-catch block");
}
}
Output: 




Program 8: Java finally block
class FinallyDemo
{
public static void main(String args[])
{
System.out.println(FinallyDemo.m());
}
public static int m()
{
try
{
return 99;
}
fianlly
{
System.out.println("this is finally block");
System.out.println("finally block ran even after return statement");
}
}
}

Output:




Program 9: Nested try block
class NestedTry
{
public static void main(String args[])
{
//parent try block
try{
//child try block1
try{
System.out.println("inside block1");
int a=55/0;
System.out.println(a);
}
catch(ArithmeticException e1)
{
System.out.println("Excepiton: e1");
}
//child try block2
try
{
System.out.println("inside block2");
int a=55/0;
System.out.println(a);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Excepiton: e2");
}
System.out.println("other statement");
}
catch(ArithmeticException e3)
{
System.out.println("ArithmeticException");
System.out.println("inside parent try catch block");
}
catch(ArrayIndexOutOfBoundsException e4)
{
System.out.println("ArrayIndexOutOfBoundsExcepton");
System.out.println("insdie parent try catch block");
}
catch(Exception e5)
{
System.out.println("Exception");
System.out.println("inside parent try catch block");
}
System.out.println("next statement");
}
}


Output:






Program 10: How to handle run time exceptions

class NeverCaught
{
static void m()
{
throw new RuntimeException("from m()");
}
static void n()
{
m();
}
public static void main(String args[])
{
n();
}
}
Output:

JavaScript program for Password Validation

In this post you will learn how to check Password validation using JavaScript.This type of code usually use for every Register form.

<html>
<head>
<title> javascript for password validation</title>
<script>
function password_valid()
{
var password=document.myform.password.value;
if(password==null||password=="")
{
alert("password can not be blank");
return false;
}
else if(password.length<8)
{
alert("password must be at least 8 characters long");
return false;
}
}
</script>
<body>
<form name="myform"method="post"action="#"onsubmit="return password_valid()">
Enter password:<input type="password"name="password">
                          <input type="submit"value="submit">
</form>
</body>
</html>
Output:


java program to find fibonacci series

In this post you will learn what is Fibonacci series and how to write code for that using java programming.

Fibonacci:

Fibonacci series is the series of numbers where each number is obtained by adding two previous numbers. The first two numbers in the Fibonacci series are either 1 and 1 or 0 and 1.

Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89……..

Program1:

class Fibonacci{
public static void main(String args[])
{
int prev, next, sum, n;
prev=next=1;
for(n=1;n<=10;n++){
System.out.println(prev);
sum=prev+next;
prev=next;
next=sum;
}
}
}
OUTPUT:
javac Fibonacci.java
java Fibonacci
1 1 2 3 5 8  13 21 34 55

Using Recursion:

import java.util.Scanner;

class calc{
int fibo(int n){
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Fibonacci{
public static void main(String[] args){ 
Scanner sc=new Scanner(System.in);
System.out.println("Enter fibonacci Number :");
int n=sc.nextInt();
System.out.println("Fibonacci Series is :\n"); 
calc c=new calc(); 
for(int i=0;i<n;i++){
System.out.print("   "+c.fibo(i));
}
}
}

OUTPUT:
javac Fibonacci.java
java Fibonacci
Enter fibonacci Number:
10
0 1 1 2 3 5 8 13 21 34

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...