Monday, 21 April 2014

Pyramid - Using Nested For Loop

#include<iostream.h>
main()
{
   for(int i=0;i<3;i++)
{
for(int j=0;j<=i;j++)
cout<<" "<<j;
cout<<endl;
}
}


Output:


Saturday, 19 April 2014

Switch Case- Simple Program

#include<iostream>   //programmarsarmy.blogspot.com
using namespace std;
main(){
int n;
cout<<"Enter any number between 1-6:";
cin>>n;
cout<<"You entered:"<<n<<endl;
cout<<"Name of Students\n"
    <<"================\n"
    <<" 1- Usaid\n 2- Rahim\n 3- Rida\n 4- Aysha\n 5- Jannat\n 6- Imtiaz"
    <<"\n================"<<endl<<endl;
  if(n>0&&n<=10)
 
 switch(n)
{
 case 1:
        cout<<n<<"= usaid "<<endl;
        cout<<"================"<<endl;
break;
 case 2:
        cout<<n<<"= rahim"<<endl;
        cout<<"================"<<endl;
break;
 case 3:
        cout<<n<<"= rida "<<endl;
        cout<<"================"<<endl;
break;
 case 4:
        cout<<n<<"= ayesha "<<endl;
        cout<<"================"<<endl;
break;
 case 5:
       cout<<n<<"= Jannat "<<endl;
        cout<<"================"<<endl;
break;
 case 6:
        cout<<n<<"= Imtiaz "<<endl;
        cout<<"================"<<endl;
break;
}

}

Output:




Wednesday, 16 April 2014

Calculator Using Else if Statement - C++

#include <iostream>
using namespace std;
main()
{
    int number1,number2;
 char op;
    int answer, answer1, answer2, answer3;

    cout << "Enter value of number1: ";
    cin >> number1;

    cout << "Chose your Operator:";
 cout<<"Press1 for +, press2 for -, press3 for *, press4 for/"<<endl;
    cin >> op;

    cout << "Enter value of number2: ";
    cin >> number2;

    if (op == '1')
   {answer = number1 + number2;
   cout << "Answer: " << answer << endl;}
   
   else if (op == '2')
   {answer1 = number1 - number2;
   cout << "Answer: " << answer1 << endl;}
         
    else if(op == '3')
    {answer2 = number1 * number2;
    cout <<  "Answer: " << answer2 << endl;}
    
    else if (op == '4')
    {answer3 = number1 / number2;
     cout << "Answer: " << answer3 << endl;}
                
    return 0;
}

Output:



Nested If Statement+Logic Gates- Using C++

//Question is:
/*if gender is male and grade is 70 and above,display "goodboy";
if gender is female and grade is 70 above, display "goodgirl";
but if grade is below 70 regardless of gender,display "badchild";  */

//Source Code:

#include<iostream>
using namespace std;
main()
{
int grade;
char gender;

cout<<"Gender Is:";
cin>>gender;
cout<<"Grade Is:";
cin>>grade;

if(gender =='M'||'m')
if(grade>=70)
cout<<"goodboy";

if(gender=='F'||'f')
if(grade>=70)
cout<<"\n goodgirl";

else
cout<<"\n badchild";

return 0;
}

Output:


While Loop Basic Program1- Using C++

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

int n=99; // make sure n isnot initiallized to 0

while(n!=0)   //loop until n is 0
cin>>n;       // read a number into n
cout<<endl;
return 0;
}
Output:


While Loop- Basic Program- Using C++

#include<iostream>
using namespace std;

 int main()
{
int a=10;

while(a<20)
{
cout<<"value of a:" <<a <<endl;
a++;
}
}

Output:


Compare Two Values- Relational Operators- Using C++

#include<iostream>     // Including Header File
using namespace std;  // C++ Program Standard
int main()                              
{
   int num;  // variable declaration
   cout<<"Enter a number:";               // print enter a number :
   cin>>num;                           // run time user input
 
   cout<<"num < 10 is = "<< (num < 10) <<endl;    // comparing no' s
   cout<<"num > 10 is = "<< (num > 10) <<endl;    // comparing no' s
   cout<<"num == 10 is = "<< (num == 10) <<endl;  //comparing  no' s
 
}

Output: