Showing posts with label program. Show all posts
Showing posts with label program. Show all posts
Thursday, February 19, 2015
C program to copy the contents of one file into another

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2;
char ch,*f1,*f2;
clrscr();
printf("Enter source file name(ex:source.txt): ");
scanf("%s",f1);
printf("Enter destination file name(ex:destination.txt): ");
scanf("%s",f2);
fp1=fopen(f1,"r");
fp2=fopen(f2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("File could not open!!");
exit(0);
}
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
fclose(fp1);
fclose(fp2);
}
Wednesday, February 18, 2015
Java program to check whether a number is prime or not

class prime
{
public static void main(String...s)
{
int n,i;
n=Integer.parseInt(s[0]);
for(i=2;i<n;++i)
{
if(n%i==0)
break;
}
if(i==n)
System.out.println("
Number is prime");
else
System.out.println("
Number is not prime");
}
}
C program to find the sum of series 1 2 4 5 7 8

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
float sum=0,x,a=1;
printf("1/2+4/5+7/8+......");
printf("
How many terms(ex: 1,2,3...n)?");
How many terms(ex: 1,2,3...n)?");
scanf("%d",&n);
for(i=0;i<n;++i)
{
x=a/(a+1);
sum+=x;
a+=3;
}
printf("
Sum=%f",sum);
Sum=%f",sum);
getch();
}
Monday, February 16, 2015
C Program to Print following series 1 4 7 10 40
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
a*=-1;
b=i;
b*=a;
cout<<b<<" ";
}
getch();
}
C program to reverse a number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); //to clear the screen
long n,rev=0,d;
cout<<"Enter the number:";
cin>>n;
while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}
cout<<"The reversed number is "<<rev;
getch(); //to stop the screen
}
C Program to print table of any number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
cout<<"Enter a ny number:";
cin>>n;
cout<<"
";
for(i=1;i<=10;++i)
cout<<" "<<n<<"*"<<i<<"="<<n*i<<"
";
getch();
}
Sunday, February 15, 2015
C Program to Count no of alphabates digits and spaces present in a file STORY TXT
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ifstream fin("STORY.TXT");
char ch;
int i,a=0,s=0,d=0;
while(fin)
{
fin.get(ch);
i=ch;
if(i>63&&i<91||i>96&&i<123)
a++;
else
if(ch== )
s++;
else
if(i>47&&i<58)
d++;
}
cout<<"No. OF Alphabates:"<<a;
cout<<"
No. Of Digits:"<<d;
cout<<"
No. Of Spaces:"<<s;
getch();
}
Saturday, February 14, 2015
C program to count number of words in a string

#include<stdio.h>
#include<conio.h>
void main()
{
int i,words=1;
char str[100];
clrscr();
printf("Enter a string:");
gets(str);
for(i=0;str[i]!=
Java Program to Find Intersection of two Arrays
For example we have two sorted arrays a1[]={2,3,5,11} and a2[]={4,5,7,9,11} then intersection of a1 and a2 will be {5,11}. A Java program for finding intersection of two arrays is given below.
Also Read: Java Program to Find Union of two Arrays
class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("
Enter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("
Enter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("
Intersection of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
i++;
else
if(a2[j]<a1[i])
j++;
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
}
}

Also Read: Java Program to Find Union of two Arrays
Java Program to Find Intersection of two Arrays
import java.util.Scanner; //import Scanner class in our programclass demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard
System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();
int a1[]=new int[n1];
int a2[]=new int[n2];
System.out.print("
Enter elements of first array in ascending order:");
for(i=0;i<n1;++i)
a1[i]=sc.nextInt();
System.out.print("
Enter elements of second array in ascending order:");
for(i=0;i<n2;++i)
a2[i]=sc.nextInt();
i=j=0;
System.out.print("
Intersection of Arrays: ");
while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
i++;
else
if(a2[j]<a1[i])
j++;
else
{
System.out.print(a1[i]+" ");
i++;
j++;
}
}
}
}

C Program to Print an Alphabet from A to L of s in Capital Letter

#include<iostream>
using namespace std;
int main()
{
int i,j,k=1;
char ch;
cout<<"Input an Alphabet in capital letters that you want to print:";
cin>>ch;
cout<<"
";
switch(ch)
{
case A:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=0;j<=22;++j)
{
if(i==1||i==2||i==21||i==20)
cout<<"*";
else
{
if(j==0||j==20)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
case B:
cout<<" ";
while(k<=2)
{
for(i=1;i<=9;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
if(i==1||i==10)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
for(i=1;i<=10;++i)
{
if(i==1||i==10)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
for(i=9;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
++k;
}
break;
caseC:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=0;j<=22;++j)
{
if(i==1||i==2||i==39||i==40)
cout<<"*";
else
{
if(j==0)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseD:
cout<<" ";
for(i=1;i<=18;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
while(k<=4)
{
for(i=1;i<=19;++i)
{
if(i==1||i==19)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
++k;
}
for(i=18;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
break;
caseE:
cout<<" ";
for(i=1;i<=39;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2||i==20||i==21||i==38||i==39)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseF:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2||i==18||i==19)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseG:
cout<<" ";
for(i=1;i<=25;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
for(j=1;j<=20;++j)
{
if(i==1||i==2)
{ if(j==1||j==14||j==15||j==16)
cout<<"**";
else
cout<<" ";
}
else
{
if(i==9||i==10)
cout<<"*";
else
{ if(j==1||j==18)
cout<<"**";
else
cout<<" ";
}
}
}
cout<<"
";
}
break;
caseH:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=21;++j)
{
if(i==20||i==21)
cout<<"*";
else
{
if(j==1||j==19)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseI:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=30;++j)
{
if(i==1||i==2||i==39||i==40)
cout<<"*";
else
{
if(j==15)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseJ:
cout<<" ";
for(i=1;i<=30;++i)
{
for(j=1;j<=28;++j)
{
if(i==1||i==2)
cout<<"*";
else
{
if(j==14)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
for(i=1;i<=10;++i)
{
for(j=1;j<=15;++j)
{
if(i==10||i==9)
cout<<"*";
else
{
if(j==1||j==13)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
caseK:
cout<<" ";
for(i=20;i>=1;--i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
for(i=1;i<=20;++i)
{
for(j=0;j<=i;++j)
{
if(j==0||j==i)
cout<<"**";
else
cout<<" ";
}
cout<<"
";
}
break;
caseL:
cout<<" ";
for(i=1;i<=40;++i)
{
for(j=1;j<=25;++j)
{
if(i==39||i==40)
cout<<"*";
else
{
if(j==1)
cout<<"**";
else
cout<<" ";
}
}
cout<<"
";
}
break;
}
return 0;
}
C program to find largest and second largest no from a 2D array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],big1,big2,n,m,i,j;
cout<<"Enter no of rows and columns(max 5):";
cin>>m>>n;
cout<<"Enter the array:
";
for(i=0;i<m;i++)
for(j=0;j<n;++j)
cin>>a[i][j];
big1=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big1)
big1=a[i][j];
}
big2=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big2&&a[i][j]<big1)
big2=a[i][j];
}
cout<<"
Largest number:"<<big1;
cout<<"
Second largest number:"<<big2;
getch();
}
C Program for Implementation of Circular Queue Using Array
#include<stdio.h>
#define MAX 10
typedef struct Q
{
int R,F;
int data[MAX];
}Q;
void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);
void main()
{
Q q;
int op,x;
initialise(&q);
do
{
printf("
1)Insert
2)Delete
3)Print
4)Quit");
printf("
Enter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("
Enter a value:");
scanf("%d",&x);
if(!full(&q))
enqueue(&q,x);
else
printf("
Queue is full !!!!");
break;
case 2: if(!empty(&q))
{
x=dequeue(&q);
printf("Deleted Data=%d",x);
}
else
printf("
Queue is empty !!!!");
break;
case 3: print(&q);break;
}
}while(op!=4);
}
void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}
int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}
int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}
void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}
int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}
void print(Q *P)
{
int i;
if(!empty(P))
{
printf("
");
for(i=P->F;i!=P->R;i=(i+1)%MAX)
printf("%d ",P->data[i]);
printf("%d ",P->data[i]);
}
}

#define MAX 10
typedef struct Q
{
int R,F;
int data[MAX];
}Q;
void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);
void main()
{
Q q;
int op,x;
initialise(&q);
do
{
printf("
1)Insert
2)Delete
3)Print
4)Quit");
printf("
Enter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("
Enter a value:");
scanf("%d",&x);
if(!full(&q))
enqueue(&q,x);
else
printf("
Queue is full !!!!");
break;
case 2: if(!empty(&q))
{
x=dequeue(&q);
printf("Deleted Data=%d",x);
}
else
printf("
Queue is empty !!!!");
break;
case 3: print(&q);break;
}
}while(op!=4);
}
void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}
int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}
int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}
void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}
int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}
void print(Q *P)
{
int i;
if(!empty(P))
{
printf("
");
for(i=P->F;i!=P->R;i=(i+1)%MAX)
printf("%d ",P->data[i]);
printf("%d ",P->data[i]);
}
}

Friday, February 13, 2015
C Program to find Greatest number among three numbers
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y,z,max;
clrscr();
cout<<"Enter The Three Numbers ";
cin>>x>>y>>z;
max=x;
if (y>max)
max=y;
if (z>max)
max=z;
cout<<"
"<<"The Greatest Number among "<<x<<" "<<y<<" "<<z<<" is "<<max;
getch();
}
#include<conio.h>
void main()
{
int x,y,z,max;
clrscr();
cout<<"Enter The Three Numbers ";
cin>>x>>y>>z;
max=x;
if (y>max)
max=y;
if (z>max)
max=z;
cout<<"
"<<"The Greatest Number among "<<x<<" "<<y<<" "<<z<<" is "<<max;
getch();
}
C program which reads your name from the keyboard and outputs a list of ASCII codes which represent your name

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char name[50];
clrscr();
printf("Enter your name: ");
gets(name);
printf("
Character ASCII Code");
for(i=0;name[i]!=
C program to read a matrix of size mxn from the keyboard and display the same on the screen
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); //to clear the screen
int a[5][5],n,m,i,j;
cout<<"Enter value of m and n:";
cin>>m>>n;
cout<<"
Enter elemets of the matrix:
";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];
cout<<"
The Matrix is:
";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cout<<a[i][j]<<" ";
cout<<"
";
}
getch(); //to stop the screen
}
Java Program to Take Screenshots
There might be a situation where you guys want to take screen shots programmatically. So I am sharing a program that help you to take screen capture of your screen and save it in PNG (Portable network graphic) picture format.
Also Read: Java Program to Find IP Address

The below program uses Java.awt.Robot class to capture the screen pixels and uses Imageio to save it as PNG image format. Just copy and paste the program in Notepad, save it and run the program to take screen shot.
Note: The Screen shot image will be stored at the location where your program is stored.
If you liked the article please do share!!
Source: http://viralpatel.net/blogs/how-to-take-screen-shots-in-java-taking-screenshots-java/
Also Read: Java Program to Find IP Address

The below program uses Java.awt.Robot class to capture the screen pixels and uses Imageio to save it as PNG image format. Just copy and paste the program in Notepad, save it and run the program to take screen shot.
Note: The Screen shot image will be stored at the location where your program is stored.
Java Program to Take Screenshots
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
class Snap
{
public static void main(String args[]) throws Exception
{
Robot awt_robot = new Robot();
BufferedImage Entire_Screen = awt_robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(Entire_Screen, "PNG", new File("Entire_Screen.png"));
}
}
If you liked the article please do share!!
Source: http://viralpatel.net/blogs/how-to-take-screen-shots-in-java-taking-screenshots-java/
Thursday, February 12, 2015
C Program to multiply two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("
Enter first matrix:
");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("
Enter second matrix:
");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("
The new matrix is:
");
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("%d ",c[i][j]);
}
printf("
");
}
}
else
printf("
Sorry!!!! Matrix multiplication cant be done");
getch();
}
C program to input number of weeks day 1 7 and translate to its equivalent name of the day of the week
#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
clrscr(); //to clear the screen
printf("Enter number of weeks day(1-7):");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("
Sunday");
break;
case 2: printf("
Monday");
break;
case 3: printf("
Tuesday");
break;
case 4: printf("
Wednesday");
break;
case 5: printf("
Thursday");
break;
case 6: printf("
Friday");
break;
case 7: printf("
Saturday");
break;
}
getch(); //to stop the screen
}
C program to calculate simple interest
#include<conio.h>
void main()
{
float p,r,t,si;
clrscr(); //to clear the screen
printf("Enter principal,rate and time;");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("
SI=%f",si);
getch(); //to stop the screen
}
C Program to Create a Binary Tree Using Recursion Linked Representation
![C Program to Create a Binary Tree Using Recursion [Linked Representation] C Program to Create a Binary Tree Using Recursion [Linked Representation]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGLSWZDnJJqjV1qzz7_8HUKMvd34NcekdXEWE_BnrB0EMwmONwXoXSjPaKUtk9c-TNQVA2JKbUuuHOR3SYWjH2WuDnW8onVX6Ksqu12JCsexSiUQFlAuINHo2sSs4vos0ueU0mhKloA8el/s640/C+Program+to+Create+a+Binary+Tree+Using+Recursion+%255BLinked+Representation%255D+1.jpg)
What is Binary Tree?
A tree is said to be a binary tree if each node of the tree can have maximum of two children. Children of a node of binary tree are ordered. One child is called “left” child and the other is called “right” child. An example of binary tree is shown in below diagram.
![C Program to Create a Binary Tree Using Recursion [Linked Representation] C Program to Create a Binary Tree Using Recursion [Linked Representation]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-DMCVlJV8IlvtlvjHSAwFhxej9vLkzzlEGb1yzy0OJOK2ergbYUjFoowFlRFTr-7vsm5qjFMAGhuxIeOR3e2IHJ1WXWFlB66yTgPgt0xXW16ayJ9OLt-I0raOO8bPiAG14hIF0XQWXdHN/s640/C+Program+to+Create+a+Binary+Tree+Using+Recursion+%5BLinked+Representation%5D+2.jpg)
Creation of Binary Tree Using Recursion
A binary tree can be created recursively. The program will work as follow:
1. Read a data in x.
2. Allocate memory for a new node and store the address in pointer p.
3. Store the data x in the node p.
4. Recursively create the left subtree of p and make it the left child of p.
5. Recursively create the right subtree of p and make it the right child of p.
Also Read: C++ program to create Facebook Login screen
Also Read: C++ Program For Linked List Representation Of Linear Queue
The program is written in C language which allows linked representation of binary tree. Code will be as follow:
#include<stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}node;
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:
",x);
",x);
p->left=create();
printf("Enter right child of %d:
",x);
",x);
p->right=create();
return p;
}
void preorder(node *t) //address of root node is passed in t
{
if(t!=NULL)
{
printf("
%d",t->data); //visit the root
%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
}
}
int main()
{
node *root;
root=create();
printf("
The preorder traversal of tree is:
");
The preorder traversal of tree is:
");
preorder(root);
return 0;
}
In the above program I have used preorder traversal to just show that the tree is created properly or not. You can use any other traversal method here.
Subscribe to:
Posts (Atom)