Flipkart

Monday, 26 June 2017

Write a program to print following pattern

Write a program to print following pattern

    1 2 3 4 5 _ _ _ _ _ 11 12 13 14 15 _ _ _ _ _ 21 22 23 24 25 _ _ _ _ _ 31 32 33 3
    4 35 _ _ _ _ _ 41 42 43 44 45 _ _ _ _ _ 51 52 53 54 55 _ _ _ _ _ 61 62 63 64 65
    _ _ _ _ _ 71 72 73 74 75 _ _ _ _ _ 81 82 83 84 85 _ _ _ _ _ 91 92 93 94 95 _ _ _
    _ _

   
Problem definition:
    Programmer need to write for/while loop which will iterate 100 time. Loop will
    print five consecutive numbers and then skip next five number by placing special
    character (In above example special character is "_" underscore.) till number reaches to
    100.
   
Program with explanation :

#include <iostream>

using namespace std;

int main()
{
    int num = 100;                   // Number of time loop will iterate


    int count = 0;


    char spChar = '_';               // Special Character


    bool printSp = false;            // Bool to decide whether to print special character or number 


    for(int i = 1; i <= num; i++)
    {
           if(!printSp)                 // If printSp is false then print number
           {
                cout << i << " ";
                count++;
                if(count == 5) printSp = true;   // if count become 5 then set printSp to true so that in                                                                        // next iteration special character will be printed.
           }
           else                        // else print special character
           {
               cout << spChar << " ";
               count--;
               if(count == 0) printSp = false;
           }
    }
    return 0;
}

Description of problem :  

Main task in this program is to decide whether to print number or special character.
To decide this, I have taken bool variable (bool printSp) which will be update based on counter value(int count).
If bool printSp == true then print special character and if bool printSp == false then print current iteration number.
Counter to taken to update printSp variable. To print number will increment counter and once counter reached to 5 then
we will stop printing numbers and update printSp variable to true, Similarly for printing special character we will
decrements counter (As from number printing counter is incremented to 5) by one, And once it become 0 we will set printSp to
false (Here counter is 0 for next iteration) printSp will be false means print number.
In same way counter program will go up to 100 iterations.

To add More complexity to this program
1. Get Number of iteration from user.
2. Get Special character from user.
3. Get number of time special character and number should print (Period of printing)

You can try above exercise for more understanding of problem.

Wednesday, 10 June 2015

Data abstraction in c++

C++ practice programs

Data abstraction : Data abstraction is the reduction of a particular body of data to

a simplified representation of the whole.


Data abstraction means providing only essential information to the outside world and hiding their complex details,

provide only needed information to user don't provide any complex details.


Abstraction, in general, is the process of taking away or removing characteristics from something in order to reduce

 it to a set of essential characteristics.


We will consider real life example of Mobile Phones, which we can turn on and off, make a call,

adjust the volume, and add external components such as Headphones, Memory cards,

BUT we do not know its internal details, that is,

we do not know how it receives signals over the air,

how it translates them, and finally makes call.


Consider following code snippet.

#include <iostream>
int main()
{
  std::cout << "Hello World!"; // Data Abstraction
}


In above sample code. "cout" fuction print data to console. We dont know how cout manipulates data.(Data Abstraction)

 

What is batch File ?


Sunday, 10 May 2015

Batch file to delete temporary folder on Windows.

What is temporary folder ?

what is batch file ?   

Temporary folder : Temporary folder is used to store temporary file generated by applications.
Many operating system supports temporary folders. Some softwares automatically creates     temporary files. Those are stored in Temporary directory.
 
Default location of Temporary directory depends on operating system. On windows we can find it by using environment variable %TEMP%.
 
Batch file to delete/Clear the content of Temporary folder.

@echo off   
cd /d %temp%   
for /r %%a in (*.*) do (   
del /f /q %~a   
) >nul   
cd /d "%UserProfile%\Local Settings\Temporary Internet Files   
for /r %%b in (*.*) do (   
del /f /q %~b   
) >nul  

Wednesday, 6 May 2015

What is batch file.

What is batch file.

A batch file is a simple text file that contains a sequence of operating system's commands.
A batch file has multiple commands in single file. i.e batches or packages of commands. That is why it
is called as batch file.

A batch file is usually created for command sequences for which a user has a repeated need.
Commonly needed batch files are often delivered as part of an operating system.
You initiate the sequence of commands in the batch file by simply entering the name of the batch file
on a command line.

Windows batch file:

  The filename extension .bat is used in DOS and Windows.

  Please type these commands in text editor

@echo off
echo "Hello world"
echo %DATE%
  and save file as hello.bat

Now run this file this will print Hello world and current date

Tuesday, 14 April 2015

Zip Directory using command line

Zip Directory using command line
 
  What is zip file format ?
  Zip is an archive file format. In which data is compressed. This uses lossless data compression   techniques.
  We can zip multiple file or folders in single zip file. This file format is supported almost by all operating
  system.
 
  This article can be useful while implementing zipping code. You can use operating system commands to run
  zipping software, to create zip file.
 
  Here, I am using 7-zip on Windows 7.

  Install latest version of 7-zip you can download it from here[].
 
  Use following commands to zip folder using 7-zip
   First check whether 7z.exe is in windows Path variable if not then add it to Its path variable
   or go to 7z.exe install directory and fire the exe with arguments.
 
   1. If in path variable.
      open cmd to directory which you want to zip.
 Lets take D:/User/Digvijay/Test In this case i want to zip test folder.
 In cmd type cd D:/User/Digvijay/ this will go to Test folder.

 Now write command of 7zip.

      7z a -t<extension Type> <archive-name> <directory name>
 
      i.e

      7z a -tzip Digvijay.zip Test
     
 this command will create Digvijay.zip which contains Test folder content.

  2.  If not in path variable
     
 same step to locate Folder
 Now type in CMD

 " path of 7z.exe" a -t<extension Type> <archive-name> <directory name>

 i.e.

 "C:\Program File\7-zip\7z.exe" a -tzip Digvijay.zip Test
 
This will create zip file.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  

Thursday, 5 March 2015

Write a program to get Double input from User

//*********************************************************
//
//! Write a program to get Double input from User
//
//*********************************************************


#include <iostream> // Input Output

int main()
{
    printf("Program to demonstrate Double type input from user \n");

    double doubleInput = 0; // To store double input

    printf("Enter Double Number : ");
    scanf("%lf", &doubleInput);

    printf("You have entered : %lf", doubleInput);

}

OUTPUT:
 

Wednesday, 4 March 2015

write c++ program that read 10 numbers and print their sum, average, maximum, and minimum

//****************************************************
// Find Average , minimum and Maximum
//****************************************************
#include <iostream>

int main()
{
    printf("Enter 10 number to find average , minimum and maximum");

    int min = 1000000, max = -1;
    double sum = 0.0;
    for(int i = 0; i < 10; i++)
    {
        int num;
        scanf("%d", &num);
        if(num < min) min = num;
        if(num > max) max = num;
        sum = sum + num;
    }

    double avg = sum / 10;

    printf("Average is : %f \n", sum/10);
    printf("Minimum is : %d \n", min);
    printf("Maximum is : %d \n", max);
}