Posts

Showing posts with the label iterative

A simple program to check anagrams in java

Image
This is a simple program in Java. This program can be used to check if the two given strings are anagram. Two given strings are said to be anagrams when they have equal length and same characters are present in both the Strings. An example of anagram strings is SILENT and LISTEN ! Screenshot The Code: import java.util.*; public class Anagram { public static boolean isAnagram(String s1,String s2) { if(s1.length()!=s2.length()) { return false; } s1=sortCharacters(s1); s2=sortCharacters(s2); return s1.equals(s2); } public static String sortCharacters(String str) { char[] c=str.toLowerCase().toCharArray(); Arrays.sort(c); return String.valueOf(c); } public static void main(String arg[]) { String s1,s2; Scanner sc=new Scanner(System.in); System.out.println("enter 2 strings"); s1=sc.nextLine(); s2=sc.nextLine(); if(isAnagram(s1,s2)) System.out.println(s1+"is an anagram"+s2); else System.out.println(s1+"is...

Shuffle the characters of a string in Java

Image
This is a program which accepts a string using the BufferdReader and InputStreamReader class, and then shuffle the characters present inside the string and generate a new string which is displayed in the console. Basically the characters of the string are stored in a Character List using the for each loop. Then the characters in that list is randomly removed and appended in a StringBuilder object output. Screenshots [Demo] The Code: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; class Brain { public static void main(String []as)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter The String: "); String input=br.readLine(); List<Character> characters = new ArrayList<Character>(); for(char c:input.toCharArray()){ ...

Infix To Postfix Conversion in C

Image
This post is all about the conversion of an infix expression to a postfix expression. This program is basically implemented with a stack. This program is done using the c language. Here the main element is the precedence of the operator and the parentheses ! Screenshots[Demo] The Code #define SIZE 50 /* Size of Stack */ #include <ctype.h> char s[SIZE]; int top=-1; /* Global declarations */ push(char elem) { /* Function for PUSH operation */ s[++top]=elem; } char pop() { /* Function for POP operation */ return(s[top--]); } int pr(char elem) { /* Function for precedence */ switch(elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } } int main() { /* Main Program */ char infx[50],pofx[50],ch,elem; int i=0,k=0; printf(...

Selection Sort Using Java

Image
This is a simple program to illustrate how to use selection sort for sorting integers using java code. Selection sort is an in-place sorting technique with time complexity of O(n2). Its time complexity remains same in all the 3 cases (i.e. best, average and worst) .This type of sorting is preferred where memory is limited. Screenshots: Program: import java.util.*; class Sorting { public static void selectionSort(int arr[], int size) { int i, j, pos=0,temp=0; for(i=0;i<size-1;i++) { pos=i; for (j = i+1; j < size; j++) { if (arr[j] < arr[pos]) pos = j; } // Swapping the elements temp = arr[i]; arr[i] = arr[pos]; arr[pos]= temp; } } public sta...

Program to find the highest repeated character in the string

Image
This post is all about a c program which finds the highest repeated character from the given string and prints it with it's frequency. This program initially takes an input from the user and store it in a character array. Then it scans each and every character and stores the frequency of the characters and compares with the next character. Basically the program runs by two loops. Screenshots[Demo]: Output showing various testcases The Code: #include<stdio.h> #include<string.h> int main() { char a[20]="",c; int i,j,b=0,count=0; printf("Enter The String: "); gets(a); for (i=0;i<=strlen(a)-1;i++) { for (j=i;j<=strlen(a)-1;j++) { if(a[i]==a[j]) { count++; } } if(b<=count) { c=a[i]; b=count; } count=0; } printf("The Maximum Repeated Charecter is %c, repeated %d times.",c,b); return 0; } Found Bugs, Feel free to report them !

Code to trim the input string with given index and position

Image
This post is about a program which accepts a string, index and postion and perfors triming. We can say it as a substring program. The index will determine the starting point and the position will determine the end point. This program replicats the substring() which is predefined in java. The bellow code is the c version of the substring function of the String class in java ! Screenshots[Demo]: The Code: #include<stdio.h> char *fs,*ss; void substring(int a,int b) { int i,j=0; ss=(char *)malloc((b-a+1)*sizeof(char)); for(i=a;i<b;i++,j++) { ss[j]=fs[i]; } ss[j]='\0'; } int main() { int a,b; fs=(char *)malloc(100*sizeof(char)); printf("Enter THe String: "); gets(fs); printf("Enter the value of limit A: "); scanf("%d",&a); printf("Enter the value of limit B: "); scanf("%d",&b); substring(a,b); printf("The Value of fs is %s and the value of substring is %s",fs,ss); return 0; } ...

Find Optimum number of coins for a given money input

Image
This is a post in which you can use the bellow program to find the optimum number of coins which can substitute the given money. You can infinite 1re, 2rs and 5 rs coin and you want to arrange the input in such a way that you will carry less number of coin with u. This program is not a knapsack implementaion. This program is also not done using the Greedy Approach. This code works fine for all input case. The below program is done using python 2.7 Screenshots/ Demo Above is the screenshot showing the various test cases  The Code: def coins(x): fi=0 tw=0 on=0 while(x>0): if(x>=5): x=x-5; fi=fi+1 elif (x>=2): x=x-2; tw=tw+1 else: x=x-1; on=on+1; print "five: "+str(fi)+"\ntwo:"+str(tw)+"\none:"+str(on) x=input() coins(x) Found Problems, Do place your comment ! ;)

A Java Program to find the single digit sum of a number

Image
This program can be used to find the single digit sum of the given number in java. In this post you can find recursive as well as iterative method. Unlike just finding the sum of digits in a given integer, if further finds the same until a single digit is achieved. Screenshots [Demo] The output of both Recursive and Iterative method are displayed above ! The Iterative Method: import java.util.Scanner; class SingleIte { public static void main(String []as) { Scanner sc=new Scanner(System.in); int n,s=0,f=0; System.out.print("Enter The digit: "); n=sc.nextInt(); while(f==0) { while(n>0) { s=s+(n%10); n=n/10; } if(s<10) f=1; else { n=s; s=0; } } System.out.println("The Sigle digit sum is: "+s); } } The Recursive Method: import java.util.Scanner; class SingleRec { public static void singleSum(int n) { int s=0; while(n>0) { s=s+(n%10); n=n/10; } if(s<10) System.ou...

Binary Search Iterative method in python

Image
This is an iterative method for the binary search algorithm. It takes a list as an input and another variable which is the data to be searched, and then passes them to the bin() function. And in bin() there is a single while loop. Screenshots/ Output : The Code def bin(x,y): l=0; h=len(x)-1; while(l<=h): mid=(l+h)/2 if(x[mid]<y): l=mid+1 elif(x[mid]>y): h=mid-1 elif(x[mid]==y): print "found " break; else: print "Not Found " x=input() y=input() bin(x,y);

Check whether a number is Armstrong

This program takes an input from the user in the console window and checks if the entered number is an armstrong number. A number is said to be an Armstrong number if and only if the sum of the cubes of each individual digit in that number equals to that number.This program is done in python. def arm(x): m=x; s=0; while (m>0): r=m%10; s=s+r*r*r; m=m/10; if(s==x): print x print " Is An Armstrong Number"; else: print x print " Is Not An Armstrong Number"; x=input(); arm(x); Description: The arm(x) performs operations on the user input x, then it displays the output according to the result achived. r is a variable which stores the last digit of x and s is a variable which stores the sum of the cubes of the digits. Then after the termanating condition, the while loop stops and a condition is used to check whether the variable s is equal to the input.

Fibonacci Series using the iterative method

The following program prints the Fibonacci series.The first program is implemented using iterative method. The Fibonacci series is a series of numbers in which the current number is the sum of two previous numbers. This program is coded using the C language. Ex: 0,1,1,2,3,5,8,13.... #include<stdio.h> int main() { int n,a=0,b=1,c,i; printf("Enter the number of terms : "); scanf("%d",&n); printf("\nThe Fibonacci series is :\n"); if(n==0) return 0; else if(n==1) printf("%d",a); else { printf("%d %d ",a,b); for (i=2;i<n;i++) { c=a+b; a=b; b=c; printf("%d ",c); } } return 0; } Description: The above program prints the number of terms in Fibonacci Series. Here n is the variable which is user input. It acts as a boundary limit for the generated Fibonacci Series. Output: Case 1: En...

Factorial of any number (0-9999999999)

This is a simple program which is used to calculate the factorial of any number. It accepts a single digit number and even a 10 digit number to calculate the factorial ! This program is designed using  python programming language. def fact(x): f=1; for i in range (1,x+1): f=f*i; print "factorial is: " print f; x=input(); fact(x); Description: The Function fact(x) takes a number a and performs calculation on that number.