"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Iteration. Show all posts
Showing posts with label Iteration. Show all posts

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();
}

Tuesday, March 2, 2010

Write a complete C++ program to do the following

(a) read an integer X.
(b) determine the number of digits n in X.
(c) form an integer Y that has the number of digits n at ten's place and the most significant digit of X at one's place.
(d) Output Y.


(For example , if X is equal to 2134, then Y should be 42 as there are 4 digits and the most significant number is 2.)


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


void main()
{
int X, Y = 0, n = 0;
int r = 0;

cout<< "\n Enter an integer :";
cin >> X;


for(int i = X; i>0 ; i = i /10)
{
r = i % 10;


n++;
}
Y = n * 10 + r;


cout << "\n The given number  is " << X ;


cout<< "\n The new required number is = " << Y;


getch();


}

Write a c++ program to do the following :

(i) read an integer X.
(ii) form an integer Y by reversing the digits of X and integer S having sum of digits of X.
(iii) Output Y and S.

     (For example , if X is equal to 5076, then Y should be     6705 and S should be 18.)

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

void main()
{
int X, Y = 0, S = 0;
int r = 0;

cout<< "\n Enter an integer :";
cin >> X;

for(int i = X; i>0 ; i = i /10)
{
r = i % 10;

S = S + r; 

Y = Y*10 + r;

}

cout << "\n The reverse of " << X << " is " << Y;
cout<< "\n The Sum of the digits = " << S;

getch();

}

Write a C++ program to print every integer between 1 and n divisible by m. Also report whether the number that is divisible by m is even or odd.


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

void main()
{
int n, m;

cout<< "\n Enter the value of n :";
cin >> n;

cout<< "\n Enter the value of m :";
cin >> m;

if(m>n)
{cout<< "\n The number " << n << "  is not divisible by "  << m;
getch();
exit(1);
}

for( int  i = n ; i > = m ; i-- )
{
if(i % m == 0)
{
   cout << "\n " << i << "  is divisible by " << m;
   if(i%2 == 0)
        cout<< "\n The number " << i << "Even";
   else
        cout<< "\n The number " << i << "Odd";
}
}

getch();
}










Thursday, February 25, 2010

Write a C++ program to print the following series :

(i) 1  4   7  10   ......... 40
(ii) 1  -4  7  -10  ........ -40


#include<iostream.h>
#include<conio.h>
void main()
{
int a = 1, b = 1, i , incre = 3;

cout<<"\n The Series 1:";

for( i = 1 ; a<=40; i++)
{
cout<< a << " " ;
a = a+incre;
}

int p;

cout<<"\n The Series 2:";



for( i = 1 ; a<=40; i++)
{
if(i%2 == 0)
{p=-a;

cout<< p << " " ;
}
else
cout<< a << " ";

a = a+incre;
}
getch();

}

WAP to print the truth table for XY + Z

Truth Table for XY+Z

X    Y    Z     XY+Z
0     0     0     0
0     0     1     1
0     1     0     0
0     1     1     1
1     0     0     0
1     0     1     1
1     1     0     1
1     1     1     1

/* Logic is
if Z is 1 then output is 1 ;
or
if X and Y both are 1 , the output is 1 . */

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

void main()
{
clrscr();
cout<< "Truth Table\n";
cout<< "X\t Y\t Z\t\t XY+Z \n\n";

for(int i = 0 ; i<=1 ; i++)
for(int j = 0; j<=1; j++)
for(int k = 0; k<=1; k++)
{
cout<< i<< "\t"<< j << "\t" << k << "\t\t";

if( (i == 1 && j == 1) || k == 1)
cout<< 0;
else
cout<< 1;

cout<<"\n";
}

getch();

}

Wednesday, February 24, 2010

WAP to print the highest and the lowest digit present in the number input by user.



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

void main()
{
int n;

cout<< "\n Enter a number ";
cin>> n;

int num = n;

int high = n%10, low = n%10, rem;

n = n/10;

for(int i = n; i>=1; i=i/10)
{
rem =  i%10;

if(rem>high)
{
high = rem;
}

if(rem < low)
{
low = rem;
}

}

cout<< "\n The highest digit of "<< num <<" is " << high;
cout<< "\n The lowest digit of "<< num <<" is " << low;

getch();

}

Thursday, January 7, 2010

Concat 2 strings into One(without strcat() func)


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


void main()
{
char s1[20], s2[20], target[40];
cout<<"\n Enter 1st String:";
cin.getline(s1,20);
cout<<"\n Enter 2nd String:";
cin.getline(s2,20);


iny k = 0;


/* First string into the target */
for(int i = 0; s1[i]!='\0';i++,k++)
target[k] = s1[i];


/* Append second string into target */
for(i = 0; s2[i]!='\0';i++,k++)
target[k] = s2[i];




clrscr();
cout<<"\n The Firstly Entered String = "<<  s 1;
cout<<"\n Next  Entered String = "<< s 2;
cout<<"\n\n The Output String = "<< target;


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.