Practical No1

download Practical No1

of 91

  • date post

    07-Apr-2018
  • Category

    Documents

  • view

    224
  • download

    0

Embed Size (px)

Transcript of Practical No1

  • 8/6/2019 Practical No1

    1/91

    Practical no1#include

    #includevoid main()

    {clrscr();

    cout

  • 8/6/2019 Practical No1

    2/91

    cout

  • 8/6/2019 Practical No1

    3/91

    cout

  • 8/6/2019 Practical No1

    4/91

    Practical no 7#include

    #include

    void main(){clrscr();

    int num1,num2;coutnum1;coutnum2;cout

  • 8/6/2019 Practical No1

    5/91

    void main(){

    clrscr();int num1,num2;

    coutnum1;coutnum2;

    cout

  • 8/6/2019 Practical No1

    6/91

    cout

  • 8/6/2019 Practical No1

    7/91

    C=(5*(F-32))/9;cout

  • 8/6/2019 Practical No1

    8/91

    cin>>vi;couta;coutt;

    vf=vi+(a*t);cout

  • 8/6/2019 Practical No1

    9/91

    Practical No 14#include#include

    void main(){

    clrscr();float vi,a,vf,s;

    coutvi;

    coutvf;

    couts;

    a=((vf*vf)-(vi*vi))/(2*s);cout

  • 8/6/2019 Practical No1

    10/91

    Practical No 15

    #include

    #include

    void main()

    {

    clrscr();

    float r,d,c,a;

    coutr;

    d=2*r;

    c=2*3.14*r;

    a=3.14*r*r;

    cout

  • 8/6/2019 Practical No1

    11/91

    Practical No 16#include

    #includevoid main()

    {clrscr();

    float km,m,ft,inch,cm;coutkm;m=km*1000;

    cm=m*100;inch=cm/2.54;

    ft=inch/12;cout

  • 8/6/2019 Practical No1

    12/91

    cout

  • 8/6/2019 Practical No1

    13/91

    Practical no 19#include#include

    void main(){

    clrscr();int num1,num2,temp;

    coutnum1;

    coutnum2;

    temp=num1;num1=num2;

    num2=temp;cout

  • 8/6/2019 Practical No1

    14/91

    }

    Practical no 20

    #include

    #includevoid main()

    {clrscr();

    int num1,num2;coutnum1;coutnum2;num1=num1+num2;

    num2=num1-num2;num1=num1-num2;

    cout

  • 8/6/2019 Practical No1

    15/91

    Practical no 21

    #include#include

    void main(){

    clrscr();int num,fstd,sd,td,frd;

    int a,b,c;coutnum;fstd=num/1000;a=num%1000;

    sd=a/100;b=a%100;

    td=b/10;c=b%10;frd=c;cout

  • 8/6/2019 Practical No1

    16/91

    Practical no 22

    #include

    #includevoid main()

    {int marks;

    coutmarks;

    if(marks>=50)cout

  • 8/6/2019 Practical No1

    17/91

    cout

  • 8/6/2019 Practical No1

    18/91

    Practical no 25

    #include#include

    void main(){

    clrscr();cout

  • 8/6/2019 Practical No1

    19/91

    if(num%2==0)cout

  • 8/6/2019 Practical No1

    20/91

    Practical no 28#include

    #includevoid main()

    {clrscr();

    int month;coutmonth;month%4==0?cout

  • 8/6/2019 Practical No1

    21/91

    if(speed>=1 && speed

  • 8/6/2019 Practical No1

    22/91

  • 8/6/2019 Practical No1

    23/91

    #include#include

    void main(){

    clrscr();

    cout

  • 8/6/2019 Practical No1

    24/91

    coutnum3;

    min=num1;if(num1>num2)

    min=num2;

    if(num1>num3)min=num3;cout

  • 8/6/2019 Practical No1

    25/91

    Practical No 35

    #include#includevoid main(){clrscr();cout

  • 8/6/2019 Practical No1

    26/91

    Practical No 36#include#includevoid main(){cout

  • 8/6/2019 Practical No1

    27/91

    Practical No 37

    #include#includevoid main()

    {cout

  • 8/6/2019 Practical No1

    28/91

    else if(num1==num2 && num2==num3 && num1==num3){

    cout

  • 8/6/2019 Practical No1

    29/91

    {clrscr();cout

  • 8/6/2019 Practical No1

    30/91

    {clrscr();cout

  • 8/6/2019 Practical No1

    31/91

    cin>>c;

    couta;coutb;

    switch(c)

    {case 1:

    {d = a+b;

    cout

  • 8/6/2019 Practical No1

    32/91

    getch();

    }

    Practical no 41#include#include

    void main(){

    clrscr();cout

  • 8/6/2019 Practical No1

    33/91

    case 'O':case 'o':

    cout

  • 8/6/2019 Practical No1

    34/91

    cout

  • 8/6/2019 Practical No1

    35/91

    cout

  • 8/6/2019 Practical No1

    36/91

    switch(ft){ case 1:cout

  • 8/6/2019 Practical No1

    37/91

    case 6:cout

  • 8/6/2019 Practical No1

    38/91

    Practical no 46#include#include

    void main(){

    int i,n;

    cout

  • 8/6/2019 Practical No1

    39/91

    Practical no 47

    #include#include

    void main()

    {int i,s,e;couts;coute;for(i=0;i

  • 8/6/2019 Practical No1

    40/91

    Practical no 48#include

    #includevoid main()

    {

    int i;for(i=0;i

  • 8/6/2019 Practical No1

    41/91

    Practical no 49

    #include#include

    void main(){

    int i,n;coutn;for(i=0;i

  • 8/6/2019 Practical No1

    42/91

    Practical no 51

    #include

    #includevoid main()

    {int i,s,e;

    couts;

    coute;

    for(i=s;i

  • 8/6/2019 Practical No1

    43/91

    getch();}

    }

    Practical no 53

    #include#include

    void main(){

    int i,s,e;couts;coute;for(i=s;i

  • 8/6/2019 Practical No1

    44/91

    Practical no 55#include

    #includevoid main()

    {clrscr();

    int i=0,j=0;for(i=1;i

  • 8/6/2019 Practical No1

    45/91

    cout

  • 8/6/2019 Practical No1

    46/91

    Practical no 58

    #include

    #includevoid main()

    {clrscr();

    int i=0,j=0;for(i=1;i=i;j--)

    cout

  • 8/6/2019 Practical No1

    47/91

    cout

  • 8/6/2019 Practical No1

    48/91

    Practical no 63

    #include

    #includevoid main()

    {

    clrscr();int num,k,fact;coutnum;for(int i =1;i

  • 8/6/2019 Practical No1

    49/91

    cout

  • 8/6/2019 Practical No1

    50/91

    Practical no 66

    #include#includevoid main()

    {clrscr();

    cout

  • 8/6/2019 Practical No1

    51/91

    Practical No.74Task: Write a program that accept a number from the user as an input and calculates the square of a given number using function.Solution:

    //This program calculates the square of a given number

    #include

    #include

    // Function declarations.

    int square(int);

    void main()

    {

    int number=0, result=0;

    clrscr();

    cout number;

    // Calling the function square(int number)

    result = square(number);

    cout

  • 8/6/2019 Practical No1

    52/91

    SOLUTION:

    #include

    #include

    void eveodd(int);

    void main()

    {

    int num ;

    clrscr();

    coutnum;

    eveodd(num);

    getch();

    }

    void eveodd(int num)

    {

    if(num%2==0)

    cout

  • 8/6/2019 Practical No1

    53/91

    clrscr();

    int num1,num2,num3;

    coutnum2;

    cout>num3;

    min(num1,num2,num3);

    getch();

    }

    void min(int num1, int num2, int num3)

    {if(num1

  • 8/6/2019 Practical No1

    54/91

    Task: Write a program that calculates the area of the Ring using a single function.

    Solution:

    #include

    #include

    void ring(int a,int b);void main()

    {

    clrscr();

    int o,i;

    couto;

    couti;

    ring(o,i);

    cout

  • 8/6/2019 Practical No1

    55/91

    base exponentFor example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is apositive, nonzero integer and that base is an integer. The function integerPowershould use for or while to control the calculation. Do not use any math libraryfunctions.

    SOLUTION:

    #include

    #include

    void raise2pow(double,int);

    void main()

    {

    double result,x;

    clrscr();

    int pow;coutx;

    coutpow;

    raise2pow(x,pow);

    getch();

    }

    void raise2pow(double x, int pow){

    double result = 1.0;

    for(int i=0;i

  • 8/6/2019 Practical No1

    56/91

    Practical No. 78

    Task: An integer number is said to be a perfect numberif the sum of its factors,including 1 (but not the number itself), is equal to the number. For example, 6 isa perfect number, because 6 = 1 + 2 + 3. Write a function perfect that determineswhether parameter number is a perfect number. Use this function in a programthat determines and prints all the perfect numbers between 1 and 1000. Print thefactors of each perfect number to confirm that the number is indeed perfect.

    SOLUTION:#include

    #include

    void perfect(int);

    void main()

    {

    clrscr();

    int num;

    cout

  • 8/6/2019 Practical No1

    57/91

    }

    }

    }

    OUTPUT:

    Practical No. 79Task: A positive integer is entered through the keyboard. Write a function toobtain the prime factors of this number.For example: Prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are

    5 and 7. Practical No. 80Task: Write a function that takes an integer value and returns the number withits digits reversed.For example, given the number 7631, the function should return 1367.

    SOLUTION:

    #include

    #include

    void rev(int);

    void main()

    {

    clrscr();

    int num;

    cout

  • 8/6/2019 Practical No1

    58/91

    c=((num/100) % 10);

    d=((num/100) / 10);

    cout

  • 8/6/2019 Practical No1

    59/91

    double roundToInteger( double );

    double roundToTenths( double );

    double roundToHundreths( double );

    double roundToThousandths( double );

    int main()

    {

    int count;

    double number;

    clrscr();

    cout > count;

    for ( int i = 0; i < count; ++i )

    {

    cout > number;

    cout

  • 8/6/2019 Practical No1

    60/91

    double roundToTenths( double n )

    {

    return floor( n * 10 + .5 ) / 10;

    }

    double roundToHundreths( double n )

    {

    return floor( n * 100 + .5 ) / 100;

    }

    double roundToThousandths( double n )

    {

    return floor( n * 1000 + .5 ) / 1000.0;}Practical No. 83

    ***Task: Write a function to evaluate the series

    to five significant digits.Practical No. 84

    Task: Write a program using function to calculate the factorial value of anyinteger entered by the user as an input.

    (1) Without using recursion.

    (2) Using recursion.(1) Without using recursion

    #include

    #include

    void fact(int);

    void main()

    {

    clrscr();

    int num;

    cout

  • 8/6/2019 Practical No1

    61/91

    void fact(int num)

    {

    int fact,i;

    fact=1;

    for(i=1;i

  • 8/6/2019 Practical No1

    62/91

    }

    OUTPUT:Practical No. 85

    Task: A 5-digit positive integer is entered through the keyboard, write a functionto calculate sum of digits of the 5-digit number:

    (1) Without using recursion.(2) Using recursion.

    SOLUTION:

    (1)Without using recursion.#include

    #include

    void a(int);

    void main()

    {

    clrscr();

    int num;

    coutnum;

    cout

  • 8/6/2019 Practical No1

    63/91

    OUTPUT

    Practical No. 86Task: Write a function to obtain the first 25 numbers of a Fibonacci sequence. In a

    Fibonacci sequence the sum of two successive terms gives the third term.

    Following are the first few terms of the Fibonacci sequence:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

    (1) Without using recursion.(2) Using recursion.

    Practical No. 87Task: Write a C++ program that uses an inline function circleArea to prompt theuser for the radius of a circle and to calculate and print the area of that circle.

    Practical No. 88

    Task: Write a C++ program that uses an inline function pound_kgto prompt theuser for the weight in pound and to calculate and print the equivalent inkilogram.

    #include

    #include

    inline pound_kg(int w)

    {

    float weight;

    weight=w/2.22;

    cout

  • 8/6/2019 Practical No1

    64/91

    #include

    #include

    int swap(int &,int &);

    void main ()

    {clrscr();

    int num1,num2;

    coutnum1;

    coutnum2;

    swap(num1,num2);

    cout

  • 8/6/2019 Practical No1

    65/91

    random in the range 1 to 1000. The program then types:I have a number between 1 and 1000.Can you guess my number?Please type your first guess.

    The player then types a first guess. The program responds with one of thefollowing:1. Excellent! You guessed the number!

    Would you like to play again (y or n)?2. Too low. Try again.

    3. Too high. Try again.

    If the player's guess is incorrect, your program should loop until the playerfinally gets the number right. Your program should keep telling the player Toohigh or Too low to help the player zero in on the correct answer.

    SOLUTION:

    #include

    #include#include

    void main()

    {

    for(int j = 1;j>0;j++)

    {

    clrscr();

    int z , b ;

    int a[1000];

    for(int i = 1;i

  • 8/6/2019 Practical No1

    66/91

    number"

  • 8/6/2019 Practical No1

    67/91

    Practical No. 92Task: The Towers of Hanoi is one of the most famous classic problems everybudding computer scientist must grapple with. Legend has it that in a temple inthe Far East, priests are attempting to move a stack of golden disks from onediamond peg to another. The initial stack has 64 disks threaded onto one peg and

    arranged from bottom to top by decreasing size. The priests are attempting tomove the stack from one peg to another under the constraints that exactly onedisk is moved at a time and at no time may a larger disk be placed above asmaller disk. Three pegs are provided, one being used for temporarily holdingdisks. Supposedly, the world will end when the priests complete their task, sothere is little incentive for us to facilitate their efforts.Let us assume that the priests are attempting to move the disks from peg 1 to peg3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers.If we were to approach this problem with conventional methods, we would

    rapidly find ourselves hopelessly knotted up in managing the disks. Instead, ifwe attack the problem with recursion in mind, it immediately becomes tractable.Moving n disks can be viewed in terms of moving only n - 1 disks (hence, the

    recursion), as follows:

    a) Move n - 1 disks from peg 1 to peg 2, using peg 3 as a temporaryholding area.b) Move the last disk (the largest) from peg 1 to peg 3.

    c) Move the n - 1 disks from peg 2 to peg 3, using peg 1 as a temporaryholdingarea.

    The process ends when the last task involves moving n = 1 disk, i.e., the base

    case. This is accomplished by trivially moving the disk without the need for atemporary holding area.Write a program to solve the Towers of Hanoi problem. Use a recursive functionwith four parameters:

  • 8/6/2019 Practical No1

    68/91

    a) The number of disks to be movedb) The peg on which these disks are initially threadedc) The peg to which this stack of disks is to be movedd) The peg to be used as a temporary holding area

    Your program should print the precise instructions it will take to move the disks

    from the starting peg to the destination peg.Solution:

    #include

    #include

    void towers( int, int, int, int );

    void main()

    {

    int nDisks;clrscr();

    cout > nDisks;

    towers( nDisks, 1, 3, 2 );

    getch();

    }

    void towers( int disks, int start, int end, int temp ){

    if ( disks == 1 ) {

    cout

  • 8/6/2019 Practical No1

    69/91

    Output:

    Practical No. 93Task: Write a C++ program that demonstrates the concept of functionoverloadingby calculating the cube of any number. The function cube calculatecube of different data type having same or different parameter.

    SOLUTION:

    #include

    #include

    int cube(int );

    double cube(double);

    void main ()

    {

    clrscr();

    cout

  • 8/6/2019 Practical No1

    70/91

    }

    OUTPUT

    Practical No. 94Task: Write a C++ program that demonstrates the concept of default argument incalculating the volume of the box. This function calculate volume of the box 4time having default values at first and changing them until having no defaultvales in last call.

    SOLUTION:

    #include

    #include

    void deffunc(int = 5 );

    void main()

    { clrscr();

    int side = 1;

    cout

  • 8/6/2019 Practical No1

    71/91

    Array: An Array is a set of contigious memory locations i.e. all the elements of an array

    are stored contigiously / continuously in memory. All the elements of array have same

    name as that of array but have different indexes or subscripts. So every element of array

    is referenced by

    1. Array Name2. Corresponding index or Subscript

    Thats why array is also known as "subscripted variable". Moreover the length of array is

    always fixed (defined at the time of declaration) and all the elements of an array have

    same data type e.g. int, float, long etc...

    Note that the index of an array starts from 0 (instead of 1) and ends at N-1 where N is

    the size of array. i.e. an array of 5 variables will have its indexes from 0 to 4 as

    a[0],a[1],a[2],a[3] and a[4].

    Practical No.95Task: Write a program that uses an array of five elements. It just accepts theelements of array from the user and displays them in the same order using forloop.

    Solution

    #include

    #include

    void main()

    {

    clrscr();

    int arr[5],i;

    for(i=0;i

  • 8/6/2019 Practical No1

    72/91

    loop.

    #include

    #include

    void main()

    {clrscr();

    int arr[5],i;

    for(i=0;i

  • 8/6/2019 Practical No1

    73/91

    {

    c+=1;

    }

    else

    d+=1;}

    cout

  • 8/6/2019 Practical No1

    74/91

    getch();

    }

    Practical No.99Task: Write a program that calculates the sum of square of numbers stored in anarray of 10 elements.

    SOLUTION:

    #include

    #include

    void main()

    {

    clrscr();

    int A[10],i;

    cout

  • 8/6/2019 Practical No1

    75/91

    overwriting the previous value.

    #include

    #include

    void main(){

    clrscr();

    int A[5],i,max=0,min=0;

    cout

  • 8/6/2019 Practical No1

    76/91

    #include

    void main()

    {

    clrscr();

    int A[5],i,p=0,num;cout

  • 8/6/2019 Practical No1

    77/91

    int a[5],i,num,s,x=0;

    cout

  • 8/6/2019 Practical No1

    78/91

    cin>>num;

    while(beg

  • 8/6/2019 Practical No1

    79/91

    {

    cout

  • 8/6/2019 Practical No1

    80/91

    cout

  • 8/6/2019 Practical No1

    81/91

    Task: Write a program that accepts two arrays (A and B) of ten elements eachfrom the user and merges them in a single array (c) of twenty elements.

    SOLUTION:

    #include

    #include

    void main ()

    {

    clrscr();

    int a[10],b[10],c[20],i,j,m,k,l;

    cout

  • 8/6/2019 Practical No1

    82/91

    (Using Insertion Algorithm).

    Solution:

    #include #include

    void main(void)

    {

    int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    int num = 0;

    int data,i;

    clrscr();

    cout

  • 8/6/2019 Practical No1

    83/91

    (Using Deletion Algorithm).

    Practical No. 110

    Task: Write a program that takes values from user to fill a two-dimensional array(Matrix) having two rows and three columns and display the values in rowcolumn format.

    SOLUTION:

    #include

    #include

    void main()

    {

    clrscr();

    int a[2][3],i,j;

    cout

  • 8/6/2019 Practical No1

    84/91

    display the flipping the same matrix (i.e. reversing the row order) usingfunctions.

    SOLUTION:

    #include

    #include

    const int maxRows = 3;

    const int maxCols = 3;

    void readMatrix(int arr[][maxCols]);

    void displayMatrix(int a[][maxCols]);

    void displayFlippedMatrix(int a[][maxCols]);

    void main(void)

    {

    int a[maxRows][maxCols];

    clrscr();readMatrix(a);

    cout

  • 8/6/2019 Practical No1

    85/91

    for (row = 0; row < maxRows; row ++)

    {

    for(col = 0; col < maxCols; col ++)

    {

    cout

  • 8/6/2019 Practical No1

    86/91

    cin>>mata[i][j];

    }

    cout

  • 8/6/2019 Practical No1

    87/91

    SOLUTION:

    #include

    #include

    void main()

    {int Mata[3][3],Matb[3][3],Matc[3][3],row,col;

    clrscr();

    // To Input Number In Matrix A

    cout

  • 8/6/2019 Practical No1

    88/91

    {

    for(col=0; col < 3; col ++)

    {

    cout

  • 8/6/2019 Practical No1

    89/91

    cout

  • 8/6/2019 Practical No1

    90/91

    cout

  • 8/6/2019 Practical No1

    91/91