Saturday, 5 May 2012

Write as shell program to find the sum of n natural numbers.

Write as shell program to find the sum of n natural numbers.

echo -n "Enter number : "
read n

# store single digit
i=1
# store number of digit
sum=0
# use while loop to caclulate the sum of all digits
while [ $i -le $n ]
  do
   sum=$(( $sum + $i )) # calculate sum of digit
        i=$(( $i + 1 ))
  done
echo  "Sum of all digit  is $sum"


OUTPUT:-


[mca1@dump mca1]$ ./ar.sh
Enter number : 4
Sum of all digit  is 10

Sunday, 24 July 2011

PROGRAM IN C++ ON TOLL BOOTH

#include
#include


class booth
{
private:
int carno;
int amount;
static int count;

public:
void getdata( )
{
int I;
for (i=0;i<10;i++) { cout<< ”Enter the Car number”; cin>>carno;
cout<< ”Enter the Amount”; cin>> amount;
if (amount>0)
count++
}
}
void display( )
{
cout<<” Total Number of cars with paying amount”:< cout<” Total Number of cars without paying amount”:<<10-count;
}
};

Int booth::count;

void main( )
{
clrscr( );
booth b;
b.getdata( );
b.display( );
getch( );
}

PROGRAM IN C++ to find whether the given number is seven or odd using classes

#include
#include
Class evenodd
{
Int a;
Public:
void getdata( )
inline void call()
};
void evenodd::getdata( )
{
cout<<”Enter any number:”; cin>>a;
}
inline void evenodd::call( )
{
if(a%2==0)
cout<<”Number is even”;
else
cout<<”Number is odd”;
}

void main( )
{
evenodd e;
clrscr();
e.getdata( );
e.call( )
getch( );
}

PROGRAM IN C++ to convert the distance in feet and inches into meters and centimeters using friend function

#include
#include
class feet_inches
{
int feet,inches;
public:
feet_inches()
{
feet=inches==0;
}
feet_inches(int f,float in)
{
feet=f;
inches=in;
}
friend void convert(feet_inches);
};
void convert(feet_inches fi)
{
float distfeet=fi.feet+fi.inches/12;
float distcm=distfeet*30.28;
int distmt=distcm/100;
int distcm1=distcm-(distmt*100);
cout<>f;
cout<<"Inches : "; cin>>in;
feet_inches fi(f,in);
cout<<"\n\t\t\t Conversion\n"<

PROGRAM IN C++ to explain the concept of the default arguments

#include
#include
float interest(float p,int n,int r=15)
{
return((p*n*r)/100);
}
void main()
{
clrscr();
cout<<"\n The interest is : Rs."< getch();
}

PROGRAM IN C++ to find the area of cuboids, rectangle, circle, square using function overloading

#include
#include
int area(int,int,int);
int area(int,int);
float area(float,int);
int area(int);
int area(int l,int b,int h)
{
return(2*((l*b)+(b*h)+(l*h)));
}
int area(int l,int b)
{
return(l*b);
}
float area(float r=1.2)
{
return(3.14*r*r);
}
int area(int l)
{
return(l*l);
}
void main()
{
clrscr();
cout<<"\n The surface area of the cuboid is : "< cout<<"\n The area of the rectangle is : "< cout<<"\n The area of the circle is : "< cout<<"\n The area of the square is : "< getch();
}

PROGRAM IN C++ to find the factorial using class

#include
#include
class factorial
{
int num;
public:
void getinfo()
{
cout<<"\n Enter the number for which the factorial is to be found : "; cin>>num;
}
void facto()
{
int i,fact=1;
if(num==0||num==1)
cout<<"\n Factorial of "< else
{
for(i=1;i<=num;i++)
{fact=fact*i;
}
cout<<"\n Factorial of "< }
}
};
void main()
{
factorial f1;
clrscr();
f1.getinfo();
f1.facto();
getch();
}