C++ for beginners vol 1



INDEX
Volume 1

Chapter #1
                                 INTRODUCTION------------------------------------------------
Chapter #2
                                            SIMPLE PROGRAMING----------------------------------------
Chapter #3

                              else if statement-----------------------------------------------------
Chapter #4
                             LOOPING---------------------------------------------------
For loop------------------------------------------------------------------------------------------------------------------------------
While loop---------------------------------------------------------------------------------------------------------------------------
Do while loop--------------------------------------------------------------------------------------------------------------------------

Chapter #5
                              PATTERN-----------------------------------------------------

Volume 2

Chapter #6
                               FUNCTIONS-------------------------------------------------------------
Chapter #7
                                        ARRAYS----------------------------------------------------------------------------------------------------------------------------------
 Chapter #1

INTRODUCTION


In order to study programing we know what is program and in this book we learn who to use write code in C++. For that we also know the tools that are used in C++ programming.
ü Programing:
It is simply define as
                    Instruction given to a computer to solve some problems
ü Tools of C++
1-#include<iostream.h>
C++ input/output streams are primarily defined by iostream , a header file that is part of the C++ standard library (the name stands for Input/Output Stream). In C++and its predecessor, the C programming language, there is no special syntax for streaming data input or output..
2-int main():
The short answer, is because the C++ standard requires main() to return int . As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished.
3-Curly Bracket {}:
“{“means that program is started and} means program end or loop end.
4-cout:
It mean that the word that is written in the << and in inverted comma is printed same as it is.
5-\n:
In indicate the next line just like Enter. It is an escape sequence
6-cin:
It is used for inputting value in the program.
7-Semi column:
C++ is terminated with ; that why it is use at the end of  every C++ statement
-------------------------------------------------------------------------------------------

Chapter #2

SIMPLE PROGRAMING


In this type of programing we just use cout<< and cin>> statement to do programing. The example of this programing is as follows:
PROGRAM #1:
#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World";
}


Output:



 





This program just print the word or line inside the  inverted comma
PROGRAM #2:
#include <iostream>
using namespace std;
int main()
{
    cout<<"MY NAME IS KHALID";
}
Output:



 Program #3(Sum of two no)

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter a first value:";
cin>>a;
cout<<"Enter a second value:";
cin>>b;
c=a+b;
cout<<"Total="<<c;
}

OUTPUT

Enter a first value:1
Enter a second value:2
Total=3
--------------------------------
Process exited with return value 0
Press any key to continue . . .

Program#3(ASCII #)
#include <iostream>
using namespace std;

int main()
{
 char c;
 cout << "Enter a character: ";
 cin >> c;
 cout << "ASCII Value of " << c << " is " << int(c);
 return 0;
}

Output

Enter a character: a
ASCII Value of a is 97
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Program#4(SWAP two no)
#include <iostream>
using namespace std;

int main()
{
    int a = 5, b = 10, temp;

    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    temp = a;
    a = b;
    b = temp;

    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;

    return 0;
}

Output

Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5

--------------------------------
Process exited with return value 0
Press any key to continue . . .
-------------------------------------------------------------
Chapter #3

                              else if statement



 The general form of  this statement is as follows:


when the above if statement is executed the condition expression is evaluated. if the condition is true then the block of statement is follow else mean if the condition is not true then other block of statement follows

PROGRAM #1(how to built a calculator using else if statement)

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
float f;
cout<<"\nPress 1 for plus(+)";
cout<<"\nPress 2 for Substraction(-)";
cout<<"\nPress 3 for Division(/)";
cout<<"\nenter a first no:";
cin>>a;
cout<<"\nenter a second no:";
cin>>b;
cout<<"\ninput operator:";
cin>>c;
d=a+b;
e=a-b;
f=a/b;
 if (c<=1)
 {
 cout<<a<<"+"<<b<<"="<<d<< endl;
}
else if(c<=2)
{
 cout<<a<<"-"<<b<<"="<<e<< endl;
}
else if(c<=3)
{
cout<<a<<"/"<<b<<"="<<f<< endl;
}
else
{
cout<<"You input is wrong";
}
}

Output

Press 1 for plus(+)
Press 2 for Substraction(-)
Press 3 for Division(/)
enter a first no:3

enter a second no:3

input operator:3
3/3=1

--------------------------------
Process exited with return value 0
Press any key to continue . . .

Program#2(find that entered no is even or odd)

#include <iostream>
using namespace std;

int main()
{
    int n;

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

    if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";
}
Output
Enter an integer: 5
5 is odd.
--------------------------------
Process exited with return value 0
Press any key to continue . . .
-------------------------------------------------------------

Chapter #4
LOOPING
If you want to become a programmer you know the concept of looping. Know the question rises what is a concept of looping.
 If we want to execute one or more task or one or more c++ statement in one or more than one times we use looping.
Types of loop:

There are three types of loop in C++. These are as follow.
ü  For loop
ü  While loop
ü  Do while loop
·      For loop
The syntax of for loop is as follows

for(exp1;exp2;exp3)
exp1: It initializes the loop means the starting point of loop
 exp2: It is the main part of loop. It is the condition. The loop execute until the expression2 become false.
 exp3: It increment or decrement the loop

Control flow

 


 PROGRAM #1(How to print table)
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter a table no:";
cin>>a;
for(b=1;b<=12;b++)
{
c=a*b;
cout<< a << "*" << b << "=" << c<< endl;
}
}
 OUTPUT
enter a table no:4
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
4*11=44
4*12=48

--------------------------------
Process exited with return value 0
Press any key to continue . . .

 Program#2(print a table in reverse order)

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter a table no:";
cin>>a;
for(b=10;b>=1;b--)
{
c=a*b;
cout<< a << "*" << b << "=" << c<< endl;
}
}

Output

enter a table no:5
5*10=50
5*9=45
5*8=40
5*7=35
5*6=30
5*5=25
5*4=20
5*3=15
5*2=10
5*1=5

--------------------------------
Process exited with return value 0
Press any key to continue . . .

Program#3(factorial of a no)

#include <iostream>
using namespace std;

int main() 
{
    int i, n, factorial = 1;

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

    for (i = 1; i <= n; ++i) {
        factorial *= i;   // factorial = factorial * i;
    }

    cout<< "Factorial of "<<n<<" = "<<factorial;
    return 0;
}

OUTPUT

Enter a positive integer:3
Factorial of 3 = 6

PROGRAM#4(Sum of N natural no)

#include <iostream>
using namespace std;

int main()
{
    int n, sum = 0;

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

    for (int i = 1; i <= n; ++i) {
        sum += i;
    }

    cout << "Sum = " << sum;
    return 0;
}

OUTPUT

Enter a positive integer: 50
Sum = 1275

Program#5(Display Armstrong Number Between Intervals)

#include <iostream>
using namespace std;

int main()
{
  int num1, num2, i, num, digit, sum;

  cout << "Enter first number: ";
  cin >> num1;

  cout << "Enter second number: ";
  cin >> num2;
#include <iostream>
using namespace std;

int main()
{
  int num1, num2, i, num, digit, sum;

  cout << "Enter first number: ";
  cin >> num1;

  cout << "Enter second number: ";
  cin >> num2;

  cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
  for(i = num1; i <= num2; i++)
  {
        sum = 0;
        num = i;

        for(; num > 0; num /= 10)
        {
            digit = num % 10;
            sum = sum + digit * digit * digit;
        }

        if(sum == i)
        {
            cout << i << endl;
        }
  }

  return 0;
}

OUTPUT

Enter first number: 100
Enter second number: 400
Armstrong numbers between 100 and 400 are:
153
370
371

Program#6(Reverse a no)

#include <iostream>
using namespace std;

int main()
{
    int n, x = 0, r;

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

    for(  ;n != 0; )
    {
        r = n%10;
        x = x*10 + r;
        n /= 10;
    }

    cout << "Reversed Number = " << x;

    return 0;
}

OUTPUT

Enter an integer: 398
Reversed Number = 893
--------------------------------
Process exited with return value 0
Press any key to continue . . .

Program#7( no of even & odd no in a n no of interger  )

#include<iostream>
using namespace std;
int main()
{
char ch;
int a,b,g,even=0,odd=0;
{
cout<<"Enter a no";
cin>>g;
for(a=1;a<=g;a++)
{
if(b%2==0)
even=even+1;
else
odd=odd+1;;
a++;
}
cout<<"The user entered no:"<<endl;
cout<<"Even ="<<even<<endl;
cout<<"Odd ="<<odd<<endl;
}
return 0;
}

OUTPUT

Enter a no67
The user entered no:
Even =33
Odd =34

--------------------------------
Process exited with return value 0
Press any key to continue . . .

Program#7( Compute quotient and remainder )

#include <iostream>
using namespace std;

int main()
{    
    int divisor, dividend, quotient, remainder;

    cout << "Enter dividend: ";
    cin >> dividend;

    cout << "Enter divisor: ";
    cin >> divisor;

    quotient = dividend / divisor;
    remainder = dividend % divisor;

    cout << "Quotient = " << quotient << endl;
    cout << "Remainder = " << remainder;

    return 0;
}

OUTPUT

Enter dividend: 9
Enter divisor:
2
Quotient = 4
Remainder = 1
--------------------------------
Process exited with return value 0
Press any key to continue . . .

Comments

Popular posts from this blog

Stack

EXTRA INFORMATION ABOUT TECHNOLOGY