NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label simple programs. Show all posts
Showing posts with label simple programs. Show all posts

Sunday, October 21, 2012

Java program to illustrate method overloading


class area
{
  int l,b,r;
  float s;
  void sample(int l,int b)
  {
   s=b*l;
   System.out.println("area of rectangle="+s);
  }
  void sample(int r)
   {
    s=r*r;
    System.out.println("area of square="+s);
   }
  public static void main(String s[])
   {
     area a=new area();
     a.sample(5,3);
     a.sample(6);
   }
}

Output

Java program to find reverse of a number


import java.io.*;
class reverse
{
  int num,temp,p;
  reverse(int a)
  {num=a;
    temp=num;
    p=0;}
  void findreverse()
   { while(temp>0)
        {p=p*10+temp%10;
          temp/=10;}
    System.out.println("Reverse of "+num+" is "+p);
  }
  public static void main(String s[])throws IOException
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number to reverse");
    int n=Integer.parseInt(br.readLine());
    reverse r=new reverse(n);
    r.findreverse();
  }
}


Output

Java program to print amstrong numbers upto given number


import java.io.*;
class strangenum
{int num,temp,d,s;
  strangenum(int a)
  { num=a;}
  void gen_strange()
  { System.out.println("strnge numbers are");
       for(int i=0;i<=num;i++)
        {temp=i;
          s=0;
          while(temp>0)
          { d=temp%10;
             s+=d*d*d;
             temp/=10; }
      if(s==i)
        System.out.println(i); }
  }
  public static void main(String s[])throws IOException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the limit");
     int a=Integer.parseInt(br.readLine());
     strangenum st=new strangenum(a);
     st.gen_strange();}
}

Wednesday, October 17, 2012

Program to perform the addition of complex numbers



#include<iostream.h>
#include<conio.h>
class comp
{ private:
int r,i;
public:
void get();
void disp(comp,comp);
comp();
};
comp::comp()
{
r=0;
i=0;
}
void comp::get()

{
cout<<"\n\nEnter a complex number :-";
cout<<"\n\n\nEnter real part :\t";
cin>>r;
cout<<"\n\n\nEnter imaginary part:\t";
cin>>i;
cout<<"\n\n\nComlex number you entered :\t"<<r<<"+"<<i<<"i";
}
void comp::disp(comp a1,comp a2)
{
comp c;
c.r=a1.r+a2.r;

c.i=a1.i+a2.i;
cout<<"\n\n\n---------------------------------\n\n\nSum of the given complex numbers ="<<c.r<<"+"<<c.i<<"i";
}
void main()
{ clrscr();
comp a1,a2;
a1.get();
a2.get();
a1.disp(a1,a2);
getch();
}


Sample input and output

Enter a complex number
Enter real part:2
Enter imaginary part:3
Enter a complex number
Enter real part:1
Enter imaginary part:3
Sum of the given complex numbers =3+6i



Sunday, August 12, 2012

Java program to find sum of the elements of an array


class arraysum
       {
int a[],sum;
arraysum()
    {
a= new int[12];
sum=0;
for(int i=0;i<12;i++)
    {
a[i]=i+1;
    }
  }
void add()
 {
for(int i=0;i<12;i++)
    {
sum=sum+a[i];
     }
System.out.println("\t"+"sum of elements of array is "+sum);
 }

void disp()
{
for(int i=0;i<12;i++)
{
System.out.println(i+" ");
}
}

public static void main(String s[])
 {
arraysum as = new arraysum();
as.disp();
as.add();
 }

       }

Tuesday, October 11, 2011

Program using C to calculate Electricity Bill with if-else condition


Conditions      
                <=100 Rs.4/units
                 > 100 and <=300 Rs.4.50/units
                 >300 and <=500 Rs.4.75/units
                 >500 Rs.5/units

Program
#include<stdio.h>
#include<conio.h>
void main ()
{
  int unit, tot;
  clrscr ();
  printf("Enter Total Units:");
  scanf ("%d", &unit);
   if (unit<=100)
     {
      tot=unit*4;
     }
   else if (unit>100 && unit<=300)
     {
      tot=unit*4.5;
     }

Multiplication Table Using C


#include <stdio.h>
#include <conio.h>
 void main()
{
   Int i,n,num;
  clrscr();
  printf(“Enter the number to print multiplication table :”);
  scanf(“%d”,&num);
  printf(“how many terms :”);
  scanf(“%d”,&n);
  clrscr();
  printf(“======MULTIPLICATION TABLE=======\n             ”);
  for(i=1;i<=n;i++);
  printf(“%d*%d=%3d”,i,num,i*num);
  getch();
}

C program to display perfect,abudent,and deficiant numbers


#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<math.h>
int test(int n);
void main()
{
int i,lower,upper,temp;
clrscr();
printf("Enter the range\n");
printf("\tLower Bound?");
scanf("%d",&lower);
printf("\tUpper Bound?");
scanf("%d",&upper);
if(lower>upper)
{  
temp=lower;
lower=upper;
upper=temp;
}

Friday, September 30, 2011

Program To search a number in a set of numbers using C


#include<stdio.h>
#include<conio.h>
void main()
{
int  i,n,count=0;
float num[50],search;
clrscr();
printf("how many numbers ?");
scanf("%d",&n);
if(n<=0)
printf("invalid data");
else
{
for(i=0;i<n;i++)
{
printf("Enter the number-%d:",i+1);
scanf("%f",&num[i]);
}
printf("\n\n\nNumber to searched ? ");
scanf("%f",&search);
clrscr();

To Display The Sum Of 1!+2!+3!+.......+n! using C


#include<stdio.h>
#include<conio.h>
double fact(int n);
void main()
{
int n,i;
double sum=0.0;
clrscr();
printf("Up To Which Term ?");
scanf("%d",&n);
for(i=1;i<=n;++i)
sum+=fact(i);
printf("Sum Of The Factorials Of The Numbers Up To %d Is %g",n,sum);
getch();
}
double fact(int n)
{

Factorial of a number using c


#include<stdio.h>
void main()
{
int i,n,fact=1;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
 {
  fact=fact*i;
 }
  printf("%d",fact);
getch();
}

Saturday, September 24, 2011

Program to accept an amount in rupees and display the number of currency notes


#include <stdio.h>
#include<conio.h>
 void main()
        {
            int amt,t,n1,n2,n3,n4,n5,n6,n7,n8,n9,total;
            printf("\n Enter the amount");
            scanf("%d",&amt);
            t=amt;
            n1=amt/1000;
           amt=amt%1000;
           n2=amt/500;
           amt=amt%500;
           n3=amt/100;
           amt=amt%100;
           n4=amt/50;
           amt=amt%50;
           n5=amt/20;
           amt=amt%20;
           n6=amt/10;
           amt=amt%10;
           n7=amt/5;
           amt=amt%5;
           n8=amt/2;
          n9=amt%2;

Sunday, August 7, 2011

To generate pattern 333 22 1

#include<stdio.h>

void main()
{
int i,j,n;
clrscr();
printf("how many lines you need to print :");
scanf("%d",&n);
for(i=n;i>0;i--)
   {
   for(j=0;j<i;j++)
      {
      printf("%d",i);
      }
    printf("\n");
   }
getch();
}

To generate pattern 1 12 123

#include<stdio.h>

void main()
{
int i,j,n;
clrscr();
printf("how many lines you need to print :");
scanf("%d",&n);
for(i=0;i<n;i++)
   {
   for(j=0;j<=i;j++)
      {
      printf("%d",j+1);
      }
    printf("\n");
   }
getch();
}

To generate pattern 321 21 1


#include<stdio.h>

To generate pattern * ** ***


#include<stdio.h>

To generate pattern*** ** *


#include<stdio.h>

To generate pattern 1 12 123


#include<stdio.h>

Number in reverse


#include<stdio.h>

Sum of individual digits of a number


#include<stdio.h>