Java Program to read a file

// read a file and display the number of lines and characters.

import java.io.*;
import java.util.*;
class FileRead
{
public static void main(String s[]) throws Exception
{
int nl=0,nc=0;
String str,line;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file name");
str=br.readLine();
File f=new File(str);
if(f.exists())
{
BufferedReader b=new BufferedReader(new FileReader(str));
while((line=b.readLine())!=null)
{
nl++;
nc+=line.length();
System.out.println(line);
}
System.out.println("Number of Lines: "+nl);
System.out.println("NUmber of characters: "+nc);
}
else
{
System.out.println("File does not exist!");
}
}
}

Output


java program to read file

Applet program to draw triangle

//<applet code=Triangle width=500 height=500></applet>
import java.applet.*;
import java.awt.*;
public class Triangle extends Applet
{
public void paint(Graphics g)
{
g.drawLine(177,141,177,361);
g.drawLine(177,141,438,361);
g.drawLine(177,361,438,361);
}
}

Output


java triangle applet

C++ program to find the difference of elements in a matrix

// Difference of elements in a matrix c++

#include<stdio.h>
#include<conio.h>
void main()
{
                clrscr();
                int r,c,a[50][50],b[50][50],s[50][50],i,j;
                printf("Enter the row value and cloum value\n");
                scanf("%d%d",&r,&c);
                printf("Enter the elements of first matrics\n");
                for(i=0;i<r;i++)
                {
                                for(j=0;j<c;j++)
                                {
                                scanf("%d",&a[i][j]);
                                }
                }
                printf("Enter the elements of second matrics\n");
                for(i=0;i<r;i++)
                {
                                for(j=0;j<c;j++)
                                {
                                                scanf("%d",&b[i][j]);
                                }
                }
                for(i=0;i<r;i++)
                {
                                for(j=0;j<c;j++)
                                {
                                                s[i][j]=a[i][j]-b[i][j];
                                }
                }
                printf("Difference of matrics\n");
                for(i=0;i<r;i++)
                {
                                for(j=0;j<c;j++)
                                {
                                                printf("%d ",s[i][j]);
                                }
                                printf("\n");
                }
                getch();
}

Output


C program to read alternate values from a file

// Read alternate values from a file c programming.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a,v;
clrscr();
FILE *p;
printf("enter the limit: ");
scanf("%d",&n);
p=fopen("values","wb");
printf("enter the numbers: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a);
putw(a,p);
}
fclose(p);
p=fopen("values","rb");
printf("Alternative numbers are: \n");
a=getw(p);
while(!feof(p))
{
printf("%d\t",a);
fseek(p,2,1);
a=getw(p);
}
fclose(p);
getch();
}

Output

file program in c to read alternate values
C program to read alternate values from a file

C++ program to show the working of pointer

// pointer in c++
#include<iostream.h>
#include<conio.h>
void main()
{
int a,*p1;
clrscr();
p1=&a;
cout<<"\nThe address of a:"<<p1;
p1+=2;
cout<<"\nThe address of a after increment: "<<p1;
getch();
}

Output

Demonstration of pointers in c++.
C++ pointer demonstration

C program to find the median of n numbers

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,n;
float m;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n%2==0)
{
m=(a[n-1/2]+(a[n/2]))/2;
}
else
{
m=a[n/2];
}
printf("\nMedian is %f",m);
getch();
}

Output

median of n numbers c program
Median of n numbers




Hello World C program with explanation

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nHello World");
getch();
}

Output

Hello world c program, sample c program.
Hello World C program










Explanation of the program


The program starts with 2 header files stdio.h and conio.h. Stdio is the standard input/output library, it is used to get inputs form the user and to display the output to the screen. Conio is the console input/output library. Every c program must have a main function, the execution of the program starts from the main function and all the instructions included in it will be executed. clrscr() function is derived from conio.h file and it is used to clear the output screen before anything is displayed on it.
The Printf function is derived from stdio.h file and it is used to display the output to the screen, the text included in a double quotes will be displayed and the value of a variable can be displayed by typing the variable name without double quotes Eg- printf(name); here name is a variable and the value stored in it will be displayed to the output screen. Finally the getch() function is used to get an input character from the user, inorder to see the output of a program, this function is necessary.

Bubble sort in C programming

// C program to sort an array using bubble sort.
// Bubble sort in c.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,j,temp;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the %d array elements\t",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)              // bubble sorting begins here
{
for(j=i;j<n;j++)
 {
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
 }
}
  printf("sorted array is as follows\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}

Related post: Bubble sort in c++

C++ program to demonstrate call by reference


// call by reference in c++
#include<iostream.h>
#include<conio.h>
void swap(int * ,int *);
void main()
{
          int a,b;
          cout<<"Enter the value for a and b: ";
          cin>>a>>b;
          swap(&a,&b);
          cout<<"Values after swapping A= "<<a<<" B= "<<b;
          getch();
}
void swap(int *p1,int *p2)
{
          int t;
          t=*p1;
          *p1=*p2;
          *p2=t;
}

c++ progarm to implement call by reference : sample output
C++ program to demonstrate call by reference

C++ program to demonstrate call by value


//call by value method in c++
#include<iostream.h>
#include<conio.h>
int cube(int);
void main()
{
          int n;
          cout<<"Enter the number";
          cin>>n;
          n=cube(n);
          cout<<"Cube of the number is:"<<n;
          getch();
}
int cube(int n)
{
          n=n*n*n;
          return n;
}

Output
call by value, pass by value c++ progarm: Sample output
C++ program to demonstrate call by value

C++ program to find the vowels and consonants in a string


#include<iostream.h>
#include<conio.h>
void main()
{
char a[25],*p[25],v[25],c[25];
int i,j,k=0,l=0;
cout<<"Enter the string\n\n";
cin>>a;
for(i=0;a[i]!='\0';i++)
{
p[i]=&a[i];
}
p[i]=&a[i];
for(i=0;*p[i]!='\0';i++)
{
if(*p[i]=='a'||*p[i]=='e'||*p[i]=='i'||*p[i]=='o'||*p[i]=='u')
{
v[k]=*p[i];
k++;
}
else
{
c[l]=*p[i];
l++;
}
}
v[k]='\0';
c[l]='\0';
cout<<"\n\nVowels\n\n";
for(i=0;v[i]!='\0';i++)
{
cout<<v[i]<<" ";
}
cout<<"\n\nConsonents\n\n";
for(i=0;c[i]!='\0';i++)
{
cout<<c[i]<<" ";
}
getch();
}

C++ program to find the vowels and consonants in a string

C++ program to implement insertion sort


// insertion sort c++

#include<iostream.h>
#include<conio.h>
void main()
{
          int a[20],i,j,n,k,*p,temp;
          p=a;
          clrscr();
          cout<<"Enter the value for n:";
          cin>>n;
          cout<<"\nEnter the elements:";
          for(i=0;i<n;i++)
          {
                   cin>>*(p+i);
          }
          for(k=1;k<=n-1;k++)
          {
                   temp=p[k];
                   j=k-1;
                   while((temp<*(p+j)) && (j>=0))
                   {
                             p[j+1]=p[j];
                             j=j-1;
                   }                                                                                                                                                                                                                                                                                                        p[j+1]=temp;
          }
          cout<<"\nArray after sorting\n\n";
          for(i=0;i<n;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}