Thursday, 19 February 2015

Write a Program in c++ to Print Number of Days in a Month using switch case.

Switch Case:- It tests a condition or control expression. According to this control expression the control is transferred to one of the several cases. The values of expression may be of type int and char but not of type float or double.

There are some points which we have to remembered when switch statement is used:-

- Always put a break statement after the case statement in a switch .

- Two case constants in a switch statement cannot have the same value, but the same statements can      be executive for two or more different case.

Here below a Program that will show you that how to print Number of Days in a Month using switch case.


#include<iostream.h>
#include<conio.h>
  main ()
  {
int n;
cout<<"enter the number of days";
cin>>n;

switch(n)
{

case 1:
case 3:
case 5:
case 7:
case 9:
case 10:
case 12: cout<<"there are 31 days ";
 break;
case 4:
case 6:
case 8:
case 11: cout<<"there are 30 days";
 break;
case 2: cout<<"there is only 28 days in this month";
break;
default: cout<<"invalid";

}
getch();
}

Output will be:-


Monday, 16 February 2015

Write a Program in C++ to calculate Sum and Multiplication of Two Numbers if first value is greater then 2nd otherwise Print 'Sorry'.

       In this Program you can see that this is the Program using if else. In which we have to Calculate the Sum of and the Multiplication of Two Numbers using if else Statement, if a is greater then b. Otherwise we have to Print Sorry if b is Greater then a.


        #include<iostream.h>
#include<conio.h>
main()
{
int a,b, sum, mul;

clrscr();

cout<<"enter the value";

cin>>a;
cout<<"enter the 2nd value";
cin>>b;
if(a>b)
{
 sum=a+b;
 mul= a*b;

 cout<<"sum is "<<sum;
 cout<<"\n mul is "<<mul;

 }

else
 {
cout<<"sorry";

 }
getch();


}


The Output will be


Saturday, 7 February 2015

Write a program in C++to Evaluate the Performance of a Student Using if- else- if statement.

Here we discussed about the Performance of a Student using if-else-if  Statement. Here the difference Conditions are evaluated from the start and when a Condition is evaluated as true, the following Statements are executed and the rest of the Statements are skipped.

#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

float marks, percentage ;


cout<<"enter the marks scored out of 700 ";

cin>>marks;

percentage = (marks/700)*100;

if(percentage>=90 && percentage<=100)
 {
 cout<<"\n Grade A";
}
else if(percentage>=80 && percentage<90)
{
cout<<"\n Grade B";
}
else if(percentage>=70 && percentage<80)
 {
cout<<"\n Grade C";
}
else if(percentage>100)
{
cout<<"\n The marks you have enterd are incorrect";
 }
else
{
cout<<"\n Grade D";

        cout<<"\n you have got Grade D  which is equivlant to fail in marks system";
}

}


OUTPUT OF THIS PROGRAM GRADE BY GRADE WILL BE :


Write a Program in C++ to Check the given Number for Even or Odd.

This is the another example for the Selection Structure. You know that performs one out of two Statements depending upon the Condition. Here this the Program to find out the number that which one is Even and which one is Odd Number.

#include<iostream.h>

#include<conio.h>

void main()


{
int num;

clrscr();

cout<<"enter the number";

cin>>num;

if(num%2 ==0)

{
cout<<"\n"<<num<<" is an even number";

}

else
{
cout<<"\n"<<num <<" is an odd number";

}

getch();
}

OUTPUT FOR ODD AND EVEN NUMBER WILL BE RESPECTIVELY :

FOR ODD NUMBER

FOR EVEN NUMBER



Write a Program in C++ to find the Greatest Number out of two number using if-else statement.

This is the Selection Structure that performs one out of two or more Statements depending upon the Condition. Mean here according to the Condition (a>b) a is Greater then it will Print the first Statement and if b is Greater then it will Print the second Statement.

 #include<iostream.h>

  #include<conio.h>

  void main()
 {
 int a,b;

 cout<<"enter the values of a and b";

 cin>>a>>b;

 if(a>b)

 {
cout<<"\n Greater no is : "<<a;


 }

 else

 {

cout<<"\n greater number is : "<< b;

 }

getch();

 }


OUTPUT WILL BE: 



Write a Program in C++to Find the Greatest one using if statement.

This is a Sequence Structure that may consist of a single statement or a Sequence of Statements with a single entry and single exit. Here is Program in which you can see that there is single entry and single exit. Mean according to the Condition  (i.e. a>b ), if a Greater found then the Result will be the Greater Number a. If b is Greater then there is no other Statement to Print.


#include<iostream.h>

#include<conio.h>

void main()

{
 int a,b;

 clrscr();

 cout<<"enter two no.";

 cin>>a>>b;

 if(a>b)

 {
cout<<"greater is : "<< a;

 }

}

OUTPUT OF THIS PROGRAM IS:





Write a Program in C++ to Find the Area of Circle of given Radius.

Here is the another Program that how to work with C++ Language. This is the Program where you have given Radius and you have to find out the Area. You can declare PI as a constant value during declaration of variables or you can use directly the value of PI i.e 3.14 in formula.

#include<iostream.h>

#include<conio.h>

void main()
{

 float r,a,PI=3.14;

 cout<<"enter the radius";

 cin>>r;

 a=PI*r*r;

 cout<<"\n the area is =";

 cout<<a;

 getch();

}

OUTPUT WILL BE: 




Write a Program in C++ for Arithmetic Operation with different Values.

Here in this Program we shall discussed about the Arithmetical Operation but with different value. In this Program we can enter the different values in execution time.

#include<iostream.h>
#include<conio.h>

void main()
{
int a,b;

int sum,difference,product,quotient;

 cout<<"enter the value of a ";

      cin>>a;

 cout<<" \n enter the value of b";

cin>>b;

sum=a+b;
difference=a-b;
product=a*b;
quotient=a/b;

cout<<"\n the sum of "<<a<<" & "<<b<<" is ="<<sum<<endl;
cout<<"\n the difference of "<<a<<" & "<<b<<" is ="<<difference<<endl;
cout<<"\n the product of "<<a<<" & "<<b<<" is ="<<product<<endl;
cout<<"\n the quotient of "<<a<<" & "<<b<<" is ="<<quotient<<endl;
getch();
}

OUTPUT WILL BE:


Write a Program in C++for Addition of Two Numbers.

Here is the Beginning of C++ Program. Here you can see that you have to Declare the Variables for any Operation. In this Program we have three Variables a,b,c. Variable Basically provide a memory location where we can store the value. Variable can vary the values in execution time.

#include<iostream.h>

#include<conio.h>

void main()

{
 int a,b,c;
 cout<<"enter the value of a";

 cin>>a;

 cout<<"\n enter the values of b";

 cin>>b;

 c=a+b;

 cout<<"\n the sum of two values is =";

 cout<<c;

}

OUTPUT OF THIS PROGRAM IS


Write a Program in C++ for Arithmetic Operation.

In this program, we shall talk about Arithmetic Operation to be performed using C++. This is a basic problem that every student is asked to solve in his preliminary classes of the Programming Course.

#include<iostream.h>
#include<conio.h>

void main()
{

int a=10;
int b=3;

int sum, difference, product, quotient;
sum=a+b;
difference=a-b;
product=a*b;
quotient=a/b;
cout<<"the sum of "<<a<<" & "<<b<<" is "<<sum<<endl;
cout<<"\n the difference of "<<a<<" & "<<b<<" is "<<difference<<endl;
cout<<"\n the product of "<<a<<" & "<<b<<" is "<<product<<endl;
cout<<"\n the quotient of "<<a<<" & "<<b<<" is "<<quotient<<endl;
getch();

}

OUTPUT WILL BE


Friday, 6 February 2015

Basic Structure Of C++ Program.

                                                               A c++ program is a collection of functions. the program also contains the list of library files included for adding the contents to the program. Here we will study c++ program concept through some example,
                                     

#include<iostream.h>    // header file
#include<conio.h>        //header file
void main()                   // main part
{


clrscr();  //function to clear the screen.

cout<<"Welcome to the world of c++ programming";


}

 OUTPUT OF THIS PROGRAM IS




// the slash represent a comment and it may start anywhere. These are ignored by the compiler.


The first line of program #include<iostream.h> is necessary. This is a header file. This is a library file that is used for the keyword cout and #include<conio.h> in 2nd line used for clrscr();. # is a preprocessor directive. It tell the compiler that what is the function of the header file.

The third line of the program shows Every c++ program must have a function main() because compiler always start execution with main(). there is a return type that is void which is very important. this mean main return nothing. The parentheses are also required.

The fourth and seven lines of the program shows the braces { and }. these braces enclosed the body of the main i.e the body of main part. The opening brace ({) shows the beginning of main part and closing brace(}) shows the end of the main part.

The fifth line of program contain clrscr() that is use to clear the screen. 

The sixth line contain the statement i.e cout<<"welcome to the world of c++ program"; basically displays the string on the output screen. cout pronounced as a "see out". There above is a output screen on which you can see that there is a string that is displayed with the help of cout statement.