Recent

Comments

Follow us on Facebook

Monday 27 November 2017

4. Five different numbers are input in an array. Write a program to determine how many of them are positive, negative, even and odd

- No comments


Five different numbers are input in an array. Write a program to determine how many of them are positive, negative, even and odd

#include <iostream>
using namespace std;
main ()
{
int a[10],pos=0,neg=0,even=0,odd=0;
for(int i=0;i<10;i++)
{
cout<<"enter the number"<<i+1<<"   =  ";
cin>>a[i];
}
for (int i=0;i<10;i++)
{
if(a[i]>=0)
                  pos++;
            else
                  neg++;
            if(a[i]%2==0)
                  even++;
            else
                  odd++;
}
cout<<"positive number is = "<<pos<<endl;
cout<<"negative number is = "<<neg<<endl;
cout<<"even number is     = "<<even<<endl;
cout<<"odd number is      = "<<odd<<endl;
}

find sum and avg of number using array in c+= for loop

- No comments

find sum and avg of number using array in c+= for loop


#include <iostream>
using namespace std;
main ()
{
int n[10],sum,avg;
for(int i=0;i<10;i++)
{
cout<<"enter the number"<<i+1<<"   =  ";
cin>>n[i];
}
for (int i=0;i<10;i++)
{
sum+=n[i];
avg=sum/5;
}
cout<<"sum  of the number is   =  "<<sum<<endl;
cout<<"avg of the number is    = "<<avg;
}

Sunday 26 November 2017

how to find maximum and minimum number using array

- No comments

how to find maximum and minimum number using array

#include<iostream>
using namespace std;
main(){

double v[5];

for (int i=0;i<5;i++)
{
cout<<"enter the value of interger  ";
cin>>v[i];
}
 double max = v[0];
 double min = v[0];
  for (int i=1;i<5;i++)
  {
  if (v[i]< min)
  {
  min=v[i];
  }
  if (v[i]>max)
  {
  max=v[i];
  }
 
  }
  cout<<"maximun number is "<<max<<endl;
  cout<<"minimum number is "<<min<<endl;
}

how to make snake game using c++

- No comments

Image result for snake in C plus plus

make snake game in c++

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 50;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirecton dir;
void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}
void Draw()
{
    system("cls"); //system("clear");
    for (int i = 0; i < width+2; i++)
        cout << "#";
    cout << endl;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0)
                cout << "#";
            if (i == y && j == x)
                cout << "O";
            else if (i == fruitY && j == fruitX)
                cout << "F";
            else
            {
                bool print = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if (!print)
                    cout << " ";
            }
               

            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width+2; i++)
        cout << "#";
    cout << endl;
    cout << "Score:" << score << endl;
}
void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}
void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < nTail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    if (x > width || x < 0 || y > height || y < 0)
      gameOver = true;
    if (x >= width) x = 0; else if (x < 0) x = width - 1;
    if (y >= height) y = 0; else if (y < 0) y = height - 1;

    for (int i = 0; i < nTail; i++)
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;

    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }
}
int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(10); //sleep(10);
    }
    return 0;
}

Monday 20 November 2017

Arithmetic Operations *********************************************************************************** 1) Press ‘ +‘ to add two numbers 2) Press ‘-‘ to subtract two numbers 3) Press ‘/’ to divide two numbers 4) Press ‘*’ to multiply two numbers *********************************************************************************

- No comments
QNo1.
***********************************************************************************
                                                                Arithmetic  Operations                                                                                
***********************************************************************************
1)      Press  ‘ +‘  to add two  numbers
2)      Press  ‘-‘  to subtract two numbers
3)      Press ‘/’  to divide two numbers
4)      Press ‘*’ to multiply two numbers

************************************************************************************
Enter ist  Number :
Enter 2nd Number:
Select arithmetic operator from menu:
Result of Addition/Subtraction/Multiplication/Division is:

i)                    Create Menu as given above
ii)                   Take input from user for two integers
iii)                 Using switch statement, perform operation on two numbers as specified by user

*Note:  User should be restricted to enter correct operator. If operator is not valid, prompt the user to enter correct operator. Abstraction/deletion/ Multiplication/Division can be performed only when user enters correct operator.



# include <iostream>
using namespace std;

int main()
{
    char op;
    float num1, num2;
    cout<<"************************************************************************************************************************ ";
    cout<<" \n                               arthematic operation                                                                        ";
    cout<<"\n************************************************************************************************************************ ";

    cout <<"                              \n1)Press + to add number                                                                      ";
    cout<<"                               \n2)press - to subtract number                                                                 ";
    cout<<"                               \n3)press * to  multiple number                                                                ";
    cout<<"                                \n 4)press / to divided number                                                                ";
    cout<<"\n************************************************************************************************************************";
    cin >> op;
    cout << "Enter firt operands       =      : ";
    cin >> num1 ;
    cout<<"Enter second operands       =      : ";
    cin>> num2;

    switch(op)
    {
        case '+':
            cout << num1+num2;
            break;

        case '-':
            cout << num1-num2;
            break;

        case '*':
            cout << num1*num2;
            break;

        case '/':
            cout << num1/num2;
            break;

        default:
            cout << "Error! operator is not correct";
            break;
    }

    return 0;

}

Sunday 12 November 2017

Write a program to generate Fibonacci number up to 500. Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,....

- No comments

C++ Program to Generate Fibonacci Series

Fibonacci Series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series we add two previous terms/digits and get next term/number.
fibonacci

Fibonacci Series Program in C++

#include<iostream.h>
#include<conio.h>
using namespace std;
 main()
{
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
cout<<"Enter nubmer of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++)
{
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
}
getch();
}

Output

Enter nubmer of terms for Series: 7
Fibonacci series are:
0
1
1
2
3
5
8

Write a program to print the multiplication table of the number entered by the user.

- No comments
    Write a program to print the multiplication table of the number entered by the user.

#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter a positive integer: ";
    cin >> n;

    for (int i = 1; i <= 10; ++i) {
        cout << n << " * " << i << " = " << n * i << endl;
    }
    
    return 0;
}

Write a program to print all prime numbers from 1 to 300.

- No comments
int main () 
{
    for (int i=2; i<300; i++) 
        for (int j=2; j<i; j++)
        {
            if (i % j == 0) 
                break;
            else if (i == j+1)
                cout << i << " ";

        }   
    return 0;
}
A prime integer number is one that has exactly two different divisors, namely 1 and the number itself. Write, run, and test a C++ program that finds and prints all the prime numbers less than 100. (Hint: 1 is a prime number. For each number from 2 to 100, find Remainder = Number % n, where n ranges from 2 to sqrt(number). \ If n is greater than sqrt(number), the number is not equally divisible by n. Why? If any Remainder equals 0, the number is no a prime number.)

Armstrong numbers

- No comments
Write a program to print all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
  • Source Code
#include<iostream>
using namespace std;

int main()
{
 int n,digit1,digit2,digit3;

 for(int i=1;i<=500;i++)
 {
  digit1=i/100;
  digit2=i/10 - digit1*10;
  digit3=i%10;

  if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i)
   cout<<i<<endl;
 }

 
 return 0;
}

Output Dimond Star Pattern using C++

- No comments

Output Dimond Star Pattern using C++

Output Dimond of Star using c++

#include<iostream.h>
#include<conio.h>
using namespace std;
 main()
{
 int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
cout<<" ";
}
for(k=1;k<(i*2);k++)
{
cout<<"*";
}
cout<<"\n";
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
cout<<" ";
}
for(k=1;k<(i*2);k++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}

Output

         *
       * * * 
     * * * * *
   * * * * * * *
 * * * * * * * * *
   * * * * * * *
     * * * * *
       * * *
         *

last * pattern in C++

- No comments

  last * pattern in C++

Print Star Triangle

#include<iostream.h>
#include<conio.h>
using namespace std;
 main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
cout<<" ";
}
for(k=1;k<=i;k++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}

Output

* * * * *
  * * * * 
    * * * 
      * * 
        *

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix. ...

propeller ads

PropellerAds