Thursday, 2 October 2014
Saturday, 23 August 2014
Odd or Even
package com.tutorial4u;
import java.util.Scanner;
public class OddorEven {
public static void main(String[] args) {
System.out.println("Enter an Integer to check if it is odd or even.");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num%2==0){
System.out.println("Entered number is even. ");
}else{
System.out.println("Entered number is odd. ");
}
}
}
Enter an
Integer to check if it is odd or even.
24
Entered
number is even.
Monday, 18 August 2014
Addition of Two Number
package com.tutorial4u;
import java.util.Scanner;
public class Addition {
public static void main(String[] args) {
int a, b, c;
Scanner sc = new Scanner(System.in);
System.out.print("Ener value of a : ");
a = sc.nextInt();
System.out.print("Enter value of b : ");
b = sc.nextInt();
c = a + b;
System.out.println("Added Value of a and b is : " + c);
}
}
Ener
value of a : 13
Enter
value of b : 34
Added
Value of a and b is : 47
Print Multiplication of a Table
package com.tutorial4u;
import java.util.Scanner;
public class MutiplicationsOfTable {
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number to prints its table : ");
num = sc.nextInt();
System.out.println("Multiplication of table " + num + " is : ");
for (int i = 1; i <= 10; i++) {
System.out.println(num + "*" + i + "=" + (num * i));
}
}
}
Enter a
Number to prints its table : 5
Multiplication
of table 5 is :
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
Monday, 11 August 2014
Reverse of string
Reverse of string
/****************************************************** Reverse of string *******************************************************/ #include< stdio.h> #include< conio.h> #include< string.h> void main() { char str[100],temp; int i,j=0; clrscr(); printf("Enter any the string :"); gets(str); // gets function for input string i=0; j=strlen(str)-1; while(i < j) { temp=str[i]; str[i]=str[j]; str[j]=temp; i++; j--; } printf("Reverse string is :%s",str); getch(); }
Output
Enter any the string : hitesh
Reverse string is : hsetih
Explanation Of Program :
Firstly find the length of the string using library function strlen().
j = strlen(str)-1;
Suppose we accepted String "hitesh" then
j = strlen(str)-1; = strlen("hitesh") - 1 = 7 - 1 = 6
As we know String is charracter array and Character array have character range between 0 to length-1. Thus we have position of last character in variable 'j'.Current Values of 'i' and 'j' are :
i = 0; j = 6;
'i' positioned on first character and 'j' positioned on last character. Now we are swapping characters at position 'i' and 'j'. After interchanging characters we are incrementing value of 'i' and decrementing value of 'j'.
while(i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; }
If i crosses j then process of interchanging character is stopped.
Sunday, 10 August 2014
Compare of Two String.
Compare Two Strings
To compare any two string first we need to find length of each string and then compare both strings. If both string have same length then comapre character by character of each string.
/******************************************************* Compare Two Strings ********************************************************/ #include< stdio.h> #include< conio.h> void main() { char str1[20],str2[20],i,j,flag=0; clrscr(); printf("Enter first string: "); gets(str1); printf("Enter Second string: "); gets(str2); i=0; j=0; while(str1[i]!='\0') { i++; } while(str2[j]!='\0') { j++; } if(i!=j) { flag=0; } else { for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++) { if(str1[i]==str2[j]) { flag=1; } } } if(flag==0) { printf("Strings are not equal"); } else { printf("Strings are equal"); } getch(); }
Output
Enter First String : rajess
Enter Second String : rajesh
Strings are not equal
Count Length of String..
Count Length of String
Count Length of String is nothing but just count number of character in the given String. For Example: String="India" in this string have 5 characters also this is the size of string.
Count length of String without using any library function.
/************************************************** Count length of String ***************************************************/ #include< stdio.h> #include< conio.h> void main() { int i,count=0; char ch[20]; clrscr(); printf("Enter Any String: "); gets(ch); for(i=0;ch[i]!='\0';i++) { count++; } printf("Length of String: %d",count); getch(); }
Output
Enter any String: hitesh Length of String: 6
Explanation of Code
Here gets() is a predefined function in "conio.h" header file, It recevie a set of character or String from keyboard.
for(i=0;ch[i]!='\0';i++) { count++; }
Here we check the condition ch[i]!='\0' its means loop perform until string is not null, when string is null loop will be terminate.
Count length of String Using Library Function
/************************************************** Count length of String ***************************************************/ #include< stdio.h> #include< string.h> int main() { char str[20]; int length; printf("Enter any string: "); gets(str); length = strlen(str); printf("Length of string: %d",length); return 0; }
Output
Enter any String: Kumar Length of String: 5
Explanation of Code:
- Here gets() is a predefined function in "conio.h" header file, It recevie a set of character or String from keyboard.
- strlen() is a predefined function in "conio.h" header file which return length of any string.
Subscribe to:
Posts (Atom)
No of Occurrence in Array
package com.tutorial4u; import java.util.HashMap; /************************************************************ No of Occurrence in Array...
-
What is JVM ? Java virtual Machine(JVM) is a virtual Machine that provides runtime environment to execute java byte code. The JVM doesn...