Posts

Showing posts from 2011

Very Simple program to create any file in java

Image
It is very simple program to create any file import java.io.*; class SysExample { public static void main(String[] args) throws Exception { int c; System.setOut(new PrintStream(new FileOutputStream(args[0]))); while((c=System.in.read()) != -1) System.out.print((char)c); } }

Program in java to create file in byte mode

import java.io.*; class ByteModeFile { public static void main (String[] args)  throws Exception {         System.out.println("Enter the name of file");              DataInputStream c=new DataInputStream(System.in);              String name=c.readLine();     FileOutputStream fout =new FileOutputStream(name); String n=" "; StringBuffer sb=new StringBuffer(1024); try {         while(n!=null)         {         n=c.readLine();         sb.append(n);     } } catch (Exception ex) {     System.err.println("Error "  );     ex.printStackTrace(); } finally {     fout.write(new String(sb).getBytes());     fout.close(); } } ...

Program in java to copy files using command line argument

import java.io.*; class Copy { public static void main (String[] args) throws Exception { FileReader fin=new FileReader(args[0]); FileWriter fout=new FileWriter(args[1]); byte[] buffer =new byte[1024]; int i=0; while(i!=-1) { i=fin.read(); fout.write(i); } fin.close(); fout.close(); System.out.println("File copied "); } }

Simple C program of an array of 5 elements without use of Array without memory location

void main() { int *ptr; int i; for(clrscr(),i=0;scanf("%d",&ptr+i),i<5 data-blogger-escaped-br="" data-blogger-escaped-i=""> for(i=0;printf("%d\n",*(&ptr+i)),i<5 data-blogger-escaped-br="" data-blogger-escaped-i=""> getch(); }

Command Line Argument in C

#include int main(int argv,char *argc[]) { clrscr(); printf("%s",argc[1]); printf("\nNo. of arguments %d",argv); return 0; } Here argv - argument vector argc - argument character for execution compile the above program now it will generate an EXE file of that program now in turboc goto dos shell(File->Dos shell) type the name of the program and some argument Example: MyProgram Kunal The output will be Kunal No. of arguments : 2

Use printf without semicolon in simple C program

void main() { if(printf("Hi Kunal")){} } Since printf return non zero value thus this statement is always true
Say Hello

Swapping of two numbers without arithmatic operator without temporary variable

#include<stdio.h> void swap(int *a,int *b) {      *a^=*b^=*a^=*b;  }  int main()  {      int a=10,b=20;      swap(&a,&b);      printf("%d %d",a,b);      getch();      return(0);  }

Microsoft's Interview question Compute(int x) function without loop , without temporary variable without goto,continue

#include<stdio.h> int compute(int x) {     printf("%d ",x);     if(x*2<20000)     printf("%d ",compute(x<<1)>>1);     return x;     } int main() {     compute(1);     getch();     return 0;     }

Loop in DOS (Programming in DOS)

Image
@Echo off For /L %%a in (1,1,10) do Echo %%a Pause

Simple Socket programming using TCP in java

Image
import java.io.*; import java.net.*; class Server { public static void main(String[] a) throws IOException {  ServerSocket ss=new ServerSocket(29);  Socket s=ss.accept();  System.out.println(s); } } class Client { public static void main(String[] a)  throws IOException {  Socket s=new Socket("192.168.8.38",29);  System.out.println(s); } } Output

Some Interactive tips to take input form GUI Dialogs

Image
import javax.swing.*; class Dialog {     public static void main(String[] args)     {         JOptionPane.showMessageDialog(null,"Enter the two numbers");         int x=Integer.parseInt(JOptionPane.showInputDialog("Enter first number"));         int y=Integer.parseInt(JOptionPane.showInputDialog("Enter second number"));         int z=x+y;         JOptionPane.showMessageDialog(null,"Sum : " + z);             } }