Delayed Printing Using Threads In Java

The bellow code snippet will perform the printing of the numbers in the loop till with a time gap of 1sec. You can modify the bellow snippet and add your creativity to try something new. Basically Thread.sleep() makes the thread to wait for the specified time ! Screenshots[Demo]: The Code: class DelayPrint{ public static void main(String args[]) { try { for(int n = 5; n > 0; n–) { System.out.println(n); Thread.sleep(1000);// Time Gap of 1 sec } } catch (InterruptedException e) { System.out.println(“Main thread interrupted”); } }} Got Bugs, Drop them…

Read More

Moving Ball Using Applet and Timer Class

This is a simple program using applet and Timer class in java which shows the simulation of a moving ball. Screenshot: The Code: import java.applet.Applet;import java.awt.*;import java.util.TimerTask;import java.util.Timer;// @author Ankur Rajpublic class Timer_1 extends Applet{ Timer timer; int bx=100; int by=100; int bxv=2; int byv=2; int refresh=5; public void init() { timer =new Timer(); timer.schedule(new TimerTask() { public void run() { bx=bx+bxv; by=by+byv; repaint(); } },0,refresh); } public void paint(Graphics g) { g.fillOval(bx, by, 50, 50); }} Found bugs?Report us, we will happy to fix’em !

Read More

Prime Factors calculation

             Well, it is a very simple program to calculate the prime factors of a number using the Java programming language. The program consists of a simple logic of using prime numbers for division and then testing it as a prime factor or not. Just have a look at the code. Program Code: class PrimeFactors{ public static void main(String[] args) { int n=Integer.parseInt(args[0]); System.out.println(“The prime factors of “+n+” are:”); int i=2; while(n>1) { if(n%i==0) { System.out.print(i+” “); n=n/i; } else i++; } System.out.println(); }} Description:                The whole program is…

Read More

Matrix multiplication using two dynamically allocated array

This post will guide you to create a program which can accept two matrix according to the given number of rows and column. The arrays are dynamically initialized and then allocated memory. Screenshots [Demo] : The Code: #include<stdio.h>main(){ int **pa1,**pa2,i,j,r,c,k,s=0; printf(“Enter the number of rows: –>”); scanf (“%d”,&r); printf(“Enter the number of columns: –>”); scanf (“%d”,&c); // Dynamically allocating the rows of first array pa1 pa1=(int **)malloc(r*sizeof(int *)); // Dynamically allocating the rows of Second array pa2 pa2=(int **)malloc(r*sizeof(int *)); for(i=0;i<c;i++) { // Dynamically allocationg the columns of the first…

Read More

Hackerrank Mark and Toys Solution

This is a solution of Mark and Toys under Greedy sub-domain in Hackerrank. For question you can refer Here Output : Code : #include<bits/stdc++.h>using namespace std;int main(){ int k,s=0;int n; cin>>n>>k; //cout<<n<<” “<<k; vector<int> a; for(int i=0;i<n;i++) { int x; cin>>x; a.push_back(x); } sort(a.begin(),a.end()); int c=0; for(int i=0;i<n;i++) { if(s<=k) { s=s+a[i]; c++; } else break; } cout<<c-1; return 0;}

Read More

Hackerrank Grid Challenge Solution

This is the solution of Grid Challenge in Greedy sub-domain in Hackerrank. For the question you refer Here . Output : Code : #include<bits/stdc++.h>using namespace std;int main(){ int t; cin>>t; while(t–) { int n; cin>>n; vector<vector<char>> s; for(int j=0;j<n;j++) { vector<char> t; for(int i=0;i<n;i++) { char ch; cin>>ch; t.push_back(ch); } s.push_back(t); } for(int i=0;i<n;i++) { sort(s[i].begin(),s[i].end()); } int flag=0; for(int i=0;i<n;i++) { for(int j=0;j<n-1;j++) { if(s[j][i]>s[j+1][i]) { flag=1; break; } } } if(flag==0) cout<<“YES”<<endl; else cout<<“NO”<<endl; }}

Read More

Find out factorial of large number

The following program prints the factorial of large number. The program is written using java. Output :  Code :  import java.math.BigInteger;import java.util.Scanner;public class Factorial2 {   public static void main(String[] args) {       Scanner s = new Scanner(System.in);       System.out.print(“Enter a number: “);       int n = s.nextInt();       String fact = factorial(n);       System.out.println(“Factorial is ” + fact);   }   public static String factorial(int n) {       BigInteger fact = new BigInteger(“1”);       for (int i = 1; i <= n; i++) {   …

Read More

Find out IEEE 754 format representation of a number

The below program shows the floating point representation of a number. It is implemented using c. Output : Code : #include<stdio.h>int binary(int n, int i){ int k; for (i–; i >= 0; i–) { k = n >> i; if (k & 1) printf(“1”); else printf(“0”); }}typedef union{ float f; struct { unsigned int mantissa : 23; unsigned int exponent : 8; unsigned int sign : 1; } field;} myfloat;int main(){ myfloat var; printf(“Enter any float number: “); scanf(“%f”,&var.f); printf(“%d “,var.field.sign); binary(var.field.exponent, 8); printf(” “); binary(var.field.mantissa, 23); printf(“n”); return 0;}

Read More

Sort using vector in c++

The below program prints the input numbers in a sorted order.The program is implemented using c++. Output : Code : #include<bits/stdc++.h>using namespace std;int main(){ vector<int> s; int n; cout<<“Enter number of elements : “; cin>>n; for(int i=0;i<n;i++) { int t; cin>>t; s.push_back(t); } sort(s.begin(),s.end()); cout<<“Sorted Order :”<<endl; for(int i=0;i<n;i++) cout<<s[i]<<” “; return 0;}

Read More

Hackerrank Fibonacci Modified Recursive Solution

This is the solution for the Fibonacci Modified Problem found under the dynamic programming section at hackerrank. You need to find the (n+k)th term of the generated series, where nth and (n+1)th term will be supplied as input. The nth and (n+1)th terms, the (n+2)th can be computed by the following relation :Tn+2 = (Tn+1)2 + Tn So, if the first two terms of the series are 0 and 1 and k is 5 thenthe third term = 12 + 0 = 1 fourth term = 12 + 1 =…

Read More