Wednesday, 11 May 2016

JAVA Program for printing the triangle pattern


Input Format:

Enter N: 5

Output Format:

*
* *
* * *
* * * *
* * * * *
* * * *
* * * 
* * 
* 

Program:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter N: ");
int height = ( Integer.parseInt( s.nextLine( ) ) );
StringBuffer str = new StringBuffer( );
for(int i = 0;i < height*2; i++){
      if( i > = height )
            str.deleteCharAt( 0 );
      else
            str.append("*");
      System.out.println(str);
}
}
}


Tuesday, 10 May 2016

WhatsApp Messenger for Windows and Mac Desktop


WhatsApp was founded in 2009 by two tech guys Brian Acton and Jan Koum. Earlier they worked in Yahoo company. Later those two geek guys founded WhatsApp. Then WhatsApp acquired by Facebook on February 19, 2014.

Now a days WhatsApp Messenger is the very good popular messaging app which allows you to exchange messeges, contacts, documents, audio files,  photos and locations without having to pay for SMS.

Earlier WhatsApp Messenger is only available for Mobile like iPhone, BlackBerry, Windows Phone, Android and Nokia. Now it is available for Windows and Mac Desktop computers.

The following image shows the Desktop Application for WhatsApp Messenger.



WhatsApp Features

--> Now WhatsApp available as free to everyone in the world!

--> Now WhatsApp provides high security for messages. It provides end-to-end encryption.

The following image shows end-to-end encryption.





WhatsApp Text Features

--> _text_ for Italic text

--> *text* for Bold text

--> *_text_* for Bold and Italic text

--> ~text~ for strike text

--> ```text``` for hidden font style

The following image shows WhatsApp Text features.



for more details visit WhatsApp official Website.



Monday, 9 May 2016

JAVA program for printing the Capital letters in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to print the Capital letters in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

L U C C L U C J A V A

Program:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any Sentence");
String ss = s.nextLine();
String[ ] a = ss.split(" ");
String res = "";
for(int i = 0;i<a.length;i++)
{
String str = a[i];
for(int j = 0;j<str.length();j++)
{
int ch = str.charAt(j);
if(ch<=90 && ch>=65)
{
res+ = str.charAt(j)+" ";
}
}
}
System.out.println(res);
}
}


Source Code: download

Sunday, 8 May 2016

JAVA program for reversing the words in the given sentence


Explanation:

User give an input as a sentence. That contains set of words separated by space. We have to reverse the each word in the given input sentence.

Input format:

Enter any sentence:
Let Us Code in C Let Us Code in JAVA

Output format:

teL sU edoC ni C teL sU edoC ni AVAJ

Program:

import java.util.Scanner;
public class Test{
           public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("Enter any Sentence:");
      String ss = s.nextLine( );
      String[ ] a = ss.split(" ");
      String res="";
      for(int i=0;i<a.length;i++){
         StringBuffer s1 = new StringBuffer(a[i]);
         s1.reverse( );
         res+=s1+" ";
      }
      System.out.println(res);
    }
}


Source Code: download


Sunday, 3 April 2016

Write a program which accepts 2 strings from the user and performs string concatenation in JAVA



Write a program which accepts 2 strings from the user and performs the following operation on it.

Example1:

String1 = "JAVA"
String2 = "PYTHON"
String3 must be "JAVANOHTYP"

Example2:

String1 = "Vignesh"
String2 = "Sundeep"
String3 must be "VigneshpeednuS"

Both strings must be concatenated but after reversing the second string (as shown in string3 above).

Program:

import java.util.Scanner;

public class String_Program {
public static void main(String[ ] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two strings: ");
String s1 = s.next();
String s2 = s.next();
StringBuffer sb = new StringBuffer(s2);
sb.reverse( );
System.out.println(s1+sb);
}
}

Input/Output:

Enter any two strings:
Raju
Ramana
RajuanamaR


String Manipulation Program-1 in JAVA


Description:

10 digit number: 1234567812
10 digit number in words: ONETWOTHREEFOURFIVESIXSEVENEIGHTONETWO

Constraints:

1) remove duplicate letters in the above string.
2) final string must be in sorted order.

Final String: EFGHINORSTUVWX

Program:

import java.util.Arrays;
import java.util.Scanner;

public class String_Program{
public static boolean ISin(String str, char c) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter 10 digit number: ");
long n = s.nextLong();
String str = n + "";
String res = "";
String no[] = new String[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
if (str.length( ) < 10 || str.length( ) > 10) {
System.out.println("Invalid Input");
} else {
for (int i = 0; i < str.length(); i++) {
res = res + no[Integer.parseInt(str.charAt(i) + "")];
}
}
String temp = "";
for (int i = 0; i < res.length(); i++) {
if (!ISin(temp, res.charAt(i))) {
temp = temp + res.charAt(i);
}
}
String r[] = new String[temp.length()];
for (int i = 0; i < r.length; i++) {
r[i] = temp.charAt(i) + "";
}
String rr = "";
Arrays.sort(r);
for (int i = 0; i < r.length; i++) {
rr += r[i];
}
System.out.println(rr);

}
}

Input/Output:

Enter 10 digit number:
1234567812
EFGHINORSTUVWX



Sum of last digits of two given numbers program in JAVA



Program:

import java.util.Scanner;
public class Sum_of_last_digits_of_two_given_numbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter any two numbers: ");
int a = s.nextInt();
int b = s.nextInt();
String s1 = a+""; //converting integer to string
String s2 = b+""; //converting integer to string
int c = Integer.parseInt(s1.charAt(s1.length()-1)+"") + Integer.parseInt(s2.charAt(s2.length()-1)+"");
System.out.println("Sum of last digits of two given numbers is: "+c);
}
}

Input/Output:

Enter any two numbers:
123
145
Sum of last digits of two given numbers is: 8



Sunday, 1 November 2015

Java Program to print greatest of two numbers


Program:

import java.io.*;
import java.util.*;
class greater
{
public static void main(String[ ] args) {
System.out.println("Enter two values: ");
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
if(a>b)
System.out.println(a + " is Greater than " + b);
else
System.out.println(b + " is Greater than " + a);
}
}

Input/Output:

Enter two values:
5
6
6 is Greater than 5

Sunday, 16 August 2015

C language Control statements Tutorials

Control Statements

    These statements are useful to decide which statements are executed and which statements are not to be executed and also decide the number of times that a particular set of statements are to be executed.

--> Control statements are two types:

      1) Decision control statements

      2) Loop control statements

1) Decision control statements

These statements are useful to decide which statements are to be executed and which are not to be executed. These are of two types

      i) if statement

      ii) switch case statement

--> If statements are three types.

a) if statement:

--> It is an alternative to conditional operator

   Syntax:

         if( condition )

         {

            statements

         }

--> The condition is evaluated first, If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continnue the execution.

--> If it is FALSE, then the control transfers to the next statement and continue the execution.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n;
      clrscr( );
      printf("\nEnter your age: ");
      scanf("%d", &n);
      if(n>=18)
      {

         printf("\nYour are Major");

      }

      printf("\nYour are Minor");

}

--> When the if block contain only one statement, braces are optional.

b) if-else statement:

   Syntax:

         if( condition )
        {

             statements

        }
        else
        {

            statements

        }

--> The condition is evaluated first. If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continue the execution.

--> It it is FALSE, then the control transfers to else block and continue the execution.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int a, b;
      clrscr( );
      printf("\nEnter values of a & b: ");
      scanf("%d%d", &a, &b);
      if(a > b)
      {

               printf("%d is Bigger", a);

      }
      else
      {

               printf("%d is Bigger", b);

      }

}

--> if block should be immediately followed by else block.

--> When using if statement, specifying else block is optional.

--> It is useful when we have multiple options and we are required to select one among them.

c) else-if ladder:

   Syntax:

      if( condition 1)
      {

         statements

      }
      else if( condition 2)
      {

         statements

      }
      else if( condition 3)
      {

         statements

      }

      .

      .

      .
      else
      {

         statements

      }

--> Condition 1 is evaluated first. If it is TRUE, then the statements in the if block get executed. After completing the execution the control transfers to the next statement and continue the execution.

--> If it is FALSE, then condition 2 is evaluated and the same process is repeated.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int a, b, c;
      clrscr( );
      printf("\nEnter values of a, b & c: ");
      scanf("%d%d%d", &a, &b, &c);
      if(a > b && a > c)
      {

              printf("%d is Bigger", a);

      }
      else if(b > a && b > c)
      {

              printf("%d is Bigger", b);

      }
      else
      {

              printf("%d is Bigger", c);

      }

}

Note:

--> exit( ) is the function which is used to terminate the program as required.

--> It is available in the header file stdlib.h.

--> It accepts an integer value as an argument.

--> "0" represent successful termination and nonzero value represent that some error as occur.

ii) switch case statement:

--> If statement is useful when we know range of the control variable or exact value of the control variable .

--> switch case statement is useful only when we know exact value of control variable. So, all the program that use switch case statement can also be implimented using if statement, but the reverse is not possible.

   Syntax:

      switch(variable/expression)
      {

            case < values > : statements
                                       break;
            case < values > : statements
                                       break;

            .

            .

            .

            .

            default: statements

      }

--> By using switch statement we can specify only control variable or expression(no relational operators).

--> The value of the var/exp, will be first verified with the value of first case.

--> If it match, then the statements in the corresponding case get executed.

--> If it doesn't match, then it is verified with the next case value and the same process is repeated.

--> If no case match, then the default case get executed.

--> Every case should be ended with a break statement.

--> We can have as many number of statements as possible and required.

--> The default case is optional.

--> If the default case is present it should be present after all the case statements.

--> The given set of statements can be associated with as many number of cases as possible and required.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n;
      clrscr( );
      printf("\nEnter any number(1-7): ");
      scanf("%d", &n);
      switch(n);
      {

            case 1: printf("It is Monday");
                        break;
            case 2: printf("It is Tuesday");
                        break;
            case 3: printf("It is Wednesday");
                        break;
            case 4: printf("It is Thursday");
                        break;
            case 5: printf("It is Friday");
                        break;
            case 6: printf("It is Saturday");
                        break;
            case 7: printf("It is Sunday");
                        break;
            default: printf("Let Us Code in C");

}

Note:

--> goto statement is referred as branching statement.

--> It is useful to transfer the control of execution from one statement to the other statement either if forward direction or in reverse direction only within that function.

--> If the goto statement is associated with a condition, then it is referred as conditional branching.

--> If the goto statement is not associated with any condition, then it is referred as un-conditional branching.

   Syntax:

      goto   < label >;

--> label is the name given to the statement.

--> The rules for naming a label are same as that of naming a variable.

--> label names should be unique i.e no two labels should have same name.

--> If the program can have as many number of goto statements as possible and required.

Drawbacks of goto statement:

--> usage of too many labels and goto statements results confusion in understanding the flow of control.

--> Mis-placing a label with another label results in abnormal behaviour in the program.

--> goto statements are strictly prohibited in C language.

2) Loop control statements

These statements are useful to repeatedly execute the given set of statements as long as some condition is satisfied.

-->These statements are of three types of follows.

      a) while

      b) do while

      c) for

a) while:

   Syntax:

         while( condition )
         {
                   statements
          }

--> The condition is evaluated first. If it is TRUE, then the statements in the while block get executed. After completing the execution the control transfers to the condition again and the same process is repeated as long as the condition is TRUE

--> If it is FALSE, then the control transfers to the next statement after the while statement and continue the execution.

--> while statement use pre-condition checking, which is also referred as Entry control loop mechanism.

--> The statements in the while block will be execited zero or more number of times.

--> If the statements in the while block get executed n times, then the condition will be evaluated (n+1) times.

Program:

#include< stdio.h >
#include< conio.h >
void main( )

{

      int n,r,sum=0;
      printf("Enter any Number: ");
      scanf("%d",&n);
      while(n>0)
      {

            r = n%10;
            sum = sum + r;
            n = n/10;

      }

      printf("\nSum of digits of given number is ",sum);

      }

}

--> When the while block contain only one statement then braces are optional.

--> Sometimes there may be "a" requirement then "a" statements in the while block are to be execited atleast one.

b) do-while:

   Syntax:

      do{

            statements

      }while( condition );

--> The statements in the do-while block are executed and after completing the execution the condition is evaluated.

--> If it is TRUE, then the control transfers to be giving of the do-while block and the same process is repeated as long as the condition evaluates to TRUE.

--> If it is FALSE, then the control transfers to the next statement after the do-while block and continue the execution.

--> So, do-while statement use post-condition checking which is also referred as Exit Control loop Mechanism.

--> So, the statements in the do-while block get executed one or more number of times.

--> If the statements in the do-while block are executed n times, then the condition will be evaluated n times.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n,r,sum=0;
      printf("Enter any Number: ");
      scanf("%d",&n);
      do
      {

            r = n%10;
            sum = sum + r;
            n = n/10;

      }while(n>0);
      printf("\nSum of digits of given number is ",sum);
      }

}

--> The variable used in the condition based on whose value the decision is being made and it is referred as loop control variable.

Drawbacks with while statements:

--> The loop control variable should be compulsorily assign with some initial value before the while block fails, which behaves the program abnormal.

--> The loop control variable should be compulsorily modified to the while block fails ,it results from the formation of infinite loop.

--> To overcome these problems, while statement is modified as for statement, whose syntax is as follows.

c) for:

   Syntax:

      for(initialization;condition;increment/decrement)
      {

            statements

      }

--> The initialization is performed first and then the condition is evaluated.

--> If it is TRUE, then the statements in the for block get executed. After completing the execution the control transfer to the inc/dec and then checks condion. This process repeated until condion is TRUE.

--> If it is FALSE, then the control transfer to the next statement after the for statement and continue the execution.

--> So, while and for statements are syntactically different, but semantically same.

--> If the for block contain only one statement, braces are optional.

--> In the initialization part we can specify as many number of statements as possible and required and they should be seperated by comma(,).

--> Similarly in the inc/dec part also we can specify as many number of statements as possible and required, they should be seperated by comma(,).

Eg:-

      for(n=1,p=1;n<=k;p++,n++;)
      {

            statements

      }

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int n,i,f0=0,f1=1,f2;
      printf("\nEnter N Value: ");
      scanf("%d",&n);
      printf("%d\t%d",f0,f1);
      for(i=1;i<=n-2;i++)
      {

            f2=f0+f1;
            printf("\t%d",f2);
            f0=f1;
            f1=f2;

      }

}



C language Strings Tutorials

Strings

    A string is a group of characters enclosed in a curly braces or ends with a null character

--> The null character is present at the end of the string character array.

--> The null character is represented by "\0".

--> The ASCII value of null character is zero.

--> The purpose of null character is, it indicates that end of the string.

   Syntax:

      datatype array_name[size];

      Eg: char a[100];

--> Here array name indicates name of the character array and size indicates the number of characters it can store.

      Eg: char a[100];

Initialization:

char a[20] = "LetUsCodeinC";

char a[ ] ={'L','e','t','U','s','C','o','d','e','i','n','C'};

String library functions:

1) strlen( )

      This function is used to find the length of string i.e no. of characters present in it. This function return number of characters in the string expect null character.

   Syntax:

      strlen(string);

Eg:-

#include< stdio.h >
#include< conio.h >
void main( )

{

      char str[20];
      int l;
      printf("\nEnter String: ");
      gets(str);
      l = strlen(str);
      printf("Length of the given string: %d", l);

}

2) strcpy( )

This function is used to copy contents of one string into another string.

   Syntax:

            strcpy(string1, string2)

3) strcat( )

This function is used to concatenate two string values i.e contents of one string are attached with the contents of another string.

   Syntax:

            strcat(string1, string2)

--> Here contents of string2 are appended to the contents of string1.

4) strrev( )

This function is used to reverse the contents of given string.

   Syntax:

            strrev(string);

5) strcmp( )

This function is used to compare two string values whether they are equal, which one is bigger or smaller string.

   Syntax:

            strcmp(string1, string2);

--> It performs comparison on the two strings based on the ASCII value of the characters present in the string.

6) strlwr( )

This function is used to convert uppercase letters in a string into lowercase.

   Syntax:

            strlwr(string);

7) strupr( )

This function is used to convert lowercase letters in a string to uppercase.

   Syntax:

            strupr(string);