"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Saturday, June 5, 2010

WAP to input the number of units of electricity consumed, calculate and print out the charges.

The Electricity board charges according to the following rates:

For the first 100 units – 40 P per unit ( P -> Paise)
For the next 200 units – 50 P per unit
Beyond 300 units – 60 P per unit
All users are charged meter charge also which is Rs. 50/-

void main()
{
int unit;
float charge;
cout << “\n Enter the no. of units , Electricity Consumed :”;
cin >> unit;
if (unit <= 100 )
charge = 50.0 + 0.4 * unit;
else if(unit<=300)
charge = 50.0 +4.0 * 100 + 0.5 * (unit – 100);
else
charge = 50.0 + 4.0 *100 + 0.5 * 200 + 0.6 *( unit – 300) ;
cout << “n The charges is Rs. “ << charge;
getch();
}


Tuesday, April 27, 2010

WAP to input acceleration (cm/sec*sec) and time . calculate the final velocity of an object , that is initially static.

#include<iostream.h>
#include<conio.h>



void main()
{
int ac, t, v;


cout<< "Enter acceleration in cm per second square ";
cin >> ac;
cout<< " Enter time to travarse ";
cin >> t;


v = ac * t; 
// formula of final vel = initial vel + acceleration * time (initial vel = 0)


cout << " Final velocity = " << v << " cm / sec*sec ";


getch();
}

WAP to convert given inches into its equivalent yards , feet, inches. (1 yard = 36 inches, 1 foot = 12 inches)

#include<iostream.h>
#include<conio.h>

void main()
{
int in, yrd, ft, r;


cout<< " Enter distance/length in inches ";
cin>> in;


yrd = in/ 36; // calculate yard
r = in % 36; // remaining inches 
ft = r / 12; // calculate feet
r = r % 12; // remaining inches


cout<< " \n The distance in inches is " << in ;
cout << " \n The equivalent yard-feet-inches = " << yrd << " yard" << ft << "feet" << r << "inches" ;
getch();
}

Monday, April 26, 2010

WAP to input a 3 digit number. Print the digits of unit place, tenths place and hundredths place

#include<iostream.h>
#include<conio.h>

#include<process.h>


void main()
{
int n, u, t, h, r;


cout<<" Enter a number (with 3 digits) ";
cin>> n;


if( n < 100 || n > 999)
{
cout<< "\n It is not a 3 digit no. ! Abort  ";
getch();
exit(1);
}


u = n % 10; // digit of unit place 
r = n / 10; // remaining two digits
t = r % 10; // digit of tenths place
h = r / 10; // digit of hundredths place


cout<< "\n unit place Digit = " << u;
cout<< "\n ten's place Digit = " << t;
cout << "\n Hundredths place Digit = " << h;


getch();
}

Write a program to calculate simple interest and compound interest

#include<iostream.h>
#include<conio.h>

#include<math.h>


void main()
{
double p, r, t, i, am;


cout<< " Enter principal, rate , time ?";
cin>> p >> r >> t;


// Calculate Simple interest
i = (p * r* t) / 100;
am = p + i;


cout<< "\n Principal Amt = Rs. " << p;
cout<< "\n Rate = " << r << "%" ;
cout << "\n Time = " << t << "yrs.";
cout<< "\n The Interest Amt = Rs. " << i ;
cout<< "\n The Total Amt = Rs. " << am;


// Calculate Compound interest
am = pow((p* ( 1 + (r/100)), t);
i = am - p;


cout << "\n The Interest Amt = Rs. " << i;
cout<< "\n The Total Amt = Rs. " << am;


getch();
}

WAP to calculate the size of integer, character, float , double datatype


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
cout<< " \nSize of character = " << sizeof(char);
cout<< " \nSize of integer = " << sizeof(int);
cout<< "\n Size of float = " << sizeof(float);
cout<< "\n Size of double = " << sizeof(double);
getch();
}


ALTERNATIVELY,

#include(iostream.h)
#include(conio.h)

void main()
{
clrscr(); 
int n;
float f;
char c;
double d;
cout<< " \nSize of character= " << sizeof(c);
cout<< " \nSize of integer = " << sizeof(n);
cout<< "\n Size of float = " << sizeof(f);
cout<< "\n Size of double = " << sizeof(d);
getch();
}


WAP to input your height in feet and inches. Convert it into centimeter.


#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int in, feet, cm;
cout<< "\n Enter your height in feet and inches respectively :";
cout<<"\nYour height is " << feet <<"ft" << in << "inches";
cin >> feet >> in;
in = in + feet * 12;
 cm = in * 2.54;
cout << "\n Your height in  cm. is " <<  cm;
getch();
}

WAP to input time in seconds and convert it into hh-mm-ss format


#include<iostream.h>
#include<conio.h>

void main()
{
int sec, s, m, h;

cout<< "\n Enter time in seconds :";
cin >> sec;
h = sec/3600; //hours
sec = sec%3600; // remaining seconds
m = sec/60; // minutes
s = sec%60; // remaining seconds

cout << "\n HH-MM-SS  : " << h << ":" << m << ":" << s;
getch();
}

WAP to input dividend and divisor. Calculate quotient and remainder.


#include<iostream.h>
#include<conio.h>
void main()
{
int div, dis, q , r;

cout<< "Enter dividend & divisor :";
cin >> div >> dis;

q = div / dis;
r  = div % dis;

cout << " Quotient = " << q;
cout << "Remainder = " << r;

getch();
}

Write a program to input 3 sides of a triangle and calculate its area.



#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
double s1, s2, s3, ar, s;

cout<< "Enter 3 sides :";
cin >> s1 >> s2 >> s3;
s = (s1+s2+s3)/2;
ar = sqrt( s * ( s - s1) * (s  - s2) * (s - s3));
cout << "\n The Area of the triangle = " << ar;
getch();
}

Sunday, April 25, 2010

Write a class to represent a vector (1 - D Numeric array). Include member functions :

*   for vector creation.
*   for modification of a given element
*   for displaying the largest value in the vector
*   for displaying the entire vector
*   Write a program using this class.



#include<iostream.h>
#include<conio.h>

class vec
{
int arr[10];

public :

vec() // default constructor
{
for(int i = 0 ; i < 10 ; i++)
 arr[i] = 0;
}

vec(int a[])  // parameterised constructor
{
for(int i=0; i < 10 ; i++ )
{
arr[i] = a[i];
}
}

~vec()  // destructor
{
delete arr;
}

void  crea()
{
for(int i=0; i < 10 ; i++ )
{
cout << "\n Enter an element : ";
cin >> arr[i];
}
}

void modi(int pos, int ele)
{
arr[pos] = ele;
}

void disp()
{
cout<< "\n The elements of the array --> ";
for(int i = 0; i<10; i ++)
{
cout<< arr[i] << " ";
}
}

int disp_lar()
{
int lar = arr[0];
for(int i =1; i < 10 ; i++)
{
if ( lar < arr[i] )
{ lar = arr[i];
}
}
return lar;
}

}; // end of class

void main()
{
vec v1; // object v1 is created using default cons
v1.crea();
v1.disp();
int max = v1.disp_lar();
cout<< "\n The max element in the array is " << max;

cout<<"\n\n Enter 1 for modifying the array element :";
cout<<"\n Enter 2 for not modifying the array :";

int ch, ps, el;
cout<< "\n Enter your choice :";
while(ch == 1)
{
cout<< "\n Enter the position to be changed";
cin>>ps;
cout<< "\n Enter the element to be entered ";
cin >> el;
v1.modi(ps, el);
cout<< "\n Enter your choice :(1 or 2 )";
cin >> ch;
}

cout<< "\n The elements after modification ";
v1.disp();
cout<< "\n The Max element is ";
max = v1.dis_lar();
cout << max ;

getch();

}

Sunday, April 18, 2010

Write a program that simulates the rolling of two dices. The program should use rand to roll the first dice, and should read rand again to roll the second dice. The sum of the two values should then be calculated. Note: Since each dice can show an integer from 1 to 6 , then the sum of the two values will vary from 2 to 12 with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your program should roll the two dice 100 times. Use a one-dimensional array to tally the number of times each possible sum appears. Print the results in tabular format.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

const int Limit = 6;
void main()
{
int n1, n2, res;
int arr[100];

cout << "\n *** Roll 2 Dices 100 times Each & Check the sums *** \n\n ";
cout << "\n ******* Check these out ********* \n\n ";

for(int n=0; n< 100 ; n++)
{
cout << "\n YOUR TURN No. = " << n+1 << "\n";
// roll the first dice
cout << "\n Roll the first Dice ";
randomize();
n1 = random(Limit);


// roll the second dice
cout<< "\n Roll the sec dice :";
randomize();
n2 = random(Limit);


// sum of the drawn result
res = n1 + n2;

// store the sum in an array
arr[n] = res;
}

cout << "Now ,Guess the sum of the result of the rolling two dices~ "
//  Array for Guess No.
int g[100];
for( n=0 ; n< 100; n++)
{
cout<< "\n Predict The Sum For Turn : " << n+1;
cout << " Enter your number ( between 2 - 12) :";
cin >> g[n];
}
cout << " \n If the guess number and actual number matches You WON ";
cout<< "\n ***** TABLE *****\n";
cout << "\n TURN \t SUM  \t GuessNo. \t RESULT?  \n";

// Print the result in tabular format
for( n=0 ; n< 100; n++)
{

// if guess no is equal to the sum , then WON , else LOST
if(g[n] == arr[n])
cout << "\n " << n+1 << "\t" << arr[n] << "\t" << g[n] << "\t" << "WON by guessing";
else
cout << "\n " << n+1 << "\t" << arr[n] << g[n]  << "LOST by guessing";

// print 15 to 20 records at a time

if ( n == 15 || n == 35 || n == 55 || n == 75 )
{
cout << "\n Press any key to continue :";
getch();
clrscr();
}
}
getch();
}

Wednesday, April 7, 2010

Computers are playing an increasing role in education. Write a program that will help elementary school students to learn multiplication. Use rand to produce two positive one-digit integers. Your program should ask a question such as : 'How much is 6 times 7 ?' The student then type the answer. Your program checks the student's answer. If correct, print "Very Good!" and then ask another question. If the answer is wrong, print "No, Please Try Again." And then let the student try the same problem again until correct response is received.


#include<iostream.h>
#include<conio.h>
#include<Time.h>
#include<stdlib.h>

const int Limit = 9;

void main()
{
int n1, n2, pro = 1, ans = 0 ;

char ch = 'y';

cout << "\n ******* LEARN MULTIPLICATION ********* \n\n ";

while(ch=='y' || ch == 'Y')
{

randomize();
n1 = random(Limit);
randomize();
n2 = random(Limit);
pro = n1 * n2;

while(pro != ans )
{
cout << "\n How much is " << n1 << "times" << n2 << "?" ;
cout << "\n Enter your answer : ";
cin >> ans;
if(pro == ans )
     cout << "\n Very Good!!!!";
else
      cout<< "No, Please Try again .";
}
pro = 1;
ans = 0;
cout << " Do you continue (Y/N)? ";
cin >> ch;

}

getch();
}

Write a c++ function that converts a 2 digit octal number into binary number and print the binary equivalent

This program converts any octal number to its binary equivalent

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>


void main()
{
char bina[30] = " ", temp[4] = " ", binar[30] = " ";
int oct;
int r;

cout << "\n Enter an octal number :";
cin >> oct;

int o = oct; // o is the backup of octal number

while(oct)
{
r = oct % 10;
oct = oct / 10;

switch(r)
{
case 1:
           strcpy(temp, "001");
           break;
case 2:
          strcpy(temp, "010");
          break;
case 3:
         strcpy(temp, "011");
         break;
case 4:
        strcpy(temp, "100");
        break;
case 5:
        strcpy(temp, "101");
        break;
case 6:
       strcpy(temp, "110");
       break;
case 7:
       strcpy(temp, "111");
       break;
default :
        cout << endl <<  o << "  is not an octal number ";
        getch();
        exit(1);
}


strcpy( binar, bina);
strcpy( bina, temp);
strcat( bina, binar );
}
cout << "\n The binary equivalent of  octal number " << o << " is " << bina ;
getch();
}

Tuesday, April 6, 2010

An array stores details of 25 students (rollno, name, marks in 3 subjects). Write a program to create such an array and print out a list of students who have failed in more than one subject. Assume 40% as pass marks.


#include<iostream.h>
#include<conio.h>

struct stud
{
int roll;
char nm[50];
float m1, m2, m3;
};

typedef stud S;

void main()
{
S student[25];

for(int i =0 ; i < 25 ; i++)
{
cout << "\n Enter Roll no :  ";
cin >> student[i].roll;

cout << "\n Enter Name : "
cin.getline(student[i].nm, 50);

cout << "\n Enter marks of three subjects :";
cin >> student[i].m1 >> student[i].m2 >> student[i].m3 ;

}

cout<< "\n STUDENTS FAILED IN MORE THAN 1 SUBJECT \n ";
for(i =0 ; i < 25 ; i++)
{
if(( student[i].m1< 40 && student[i].m2 < 40) || (student[i].m2 < 40 && student[i].m3 < 40) || ( student[i].m1 < 40 && student[i].m3 < 40))
cout << student[i].roll  << "\t" << student[i].nm << "\n";
}

getch();

}

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.