16 September 2014

C Program to find the Largest Prime number less than the given number.

Given a number (as command line argument), find the Largest Prime number less than the given number. For e.g., given 8, the program should return 7.


#include <stdio.h>
int main()
{
    int i,n=2,m,j=0;
    int a[100]={'\0'};
    printf("enter the number in the range[3-500]\n");
    scanf("%d",&m);
    while(n<m)
    {
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            goto t;
        }
        a[j++]=n;
        t : n++;
    }
    printf("largest prime number less than given number is %d\n",a[--j]);
}

10 September 2014

Java Program to remove Blank lines from a given file


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;


public class RemoveBlankLines {


    public static void main(String[] args) throws Exception{
   
        Scanner s = new Scanner(System.in);
        System.out.println("Enter source file name");
        String srcfile = s.nextLine();
       
        File src = new File(srcfile);
        FileReader fr = new FileReader(src);
        BufferedReader br = new BufferedReader(fr);
       
        File trg = new File("tempfile.txt");
        FileWriter fw = new FileWriter(trg);
       
        String line;
        line = br.readLine();
       
        while(line!=null) {
            if(line.length() > 0)
                fw.write( line + "\n");
            line = br.readLine();
        }
                fw.close();
                fr.close();
                src.delete();
                trg.renameTo(src);
    }
   
}

2 September 2014

Java Program to find your age from given date of birth


// Calculate age in years for given date of birth

import java.util.Calendar;
import java.util.Scanner;

public class AgeCalculator {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.println("Enter date of birth [dd-mm-yyyy]:");
        String dobstring = s.nextLine();

       String [] parts= dobstring.split("-");
        Calendar dob = Calendar.getInstance();
        dob.set(Integer.parseInt(parts[2]),Integer.parseInt(parts[1]),Integer.parseInt(parts[0]));

        Calendar cd = Calendar.getInstance();  //get system date
        long ms = cd.getTimeInMillis() - dob.getTimeInMillis();
        long days = ms / (1000*60*60*24);
        System.out.printf("Age in years : %d\n", days/365);

  

    }

  

}