Showing posts with label Comparison. Show all posts
Showing posts with label Comparison. Show all posts

Monday, 1 October 2018

Checking whether a year is a leap year or not in C++

Leap year


The Earth takes 365 days and 6 hours for a complete revolution around the Sun. The 6 hours becomes 24 hours after four years. This 24 hours or a day is added to a year in every four years. The year which contains an additional day(i.e. leap day) in a calendar year is called leap year. The additional day is added to February. So a leap year has 366 days and 29 days in the month February instead of 28. According to the Gregorian calendar, the following statement identifies a leap year.
A year that is exactly divisible by four is a leap year, except for the years that are exactly divisible by 100, but these centennials(years ending with 00) are leap years if they are exactly divisible by 400.
1600 and 2000 are leap years but 1700, 2100, 2300 are common years.


Write a program in C++ to input a year from the user and check whether it is a leap year or not.


In this program, we input a year(integer data type) from the user. Then we check all the condition required to be a leap year. If all the condition satisfies then it is a leap year, else it is a common year.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int year;
clrscr();
cout<<"Enter a year=";
cin>>year;
cout<<endl;
if((year%4)!=0)
cout<<year<<" is a common year";
else if((year%100)!=0)
cout<<year<<" is a leap year";
else if((year%400)!=0)
cout<<year<<" is a common year";
else
cout<<year<<" is a leap year";
getch();

}



Output


Checking leap year
PROGRAM TO CHECK LEAP YEAR











Explanation


In this program, the entered year is 1600(i.e. year=1600). 

Condition                            Result                           
(1600%4)!=0                        False as 1600%4=0
(1600%100)!=0                    False as 1600%100=0
(1600%400)!=0                    False as 1600%400=0
else                                      True

So, 1600 is a leap year message was printed on the screen.








Saturday, 29 September 2018

Finding out the maximum number among an array of n numbers in C++

Static and dynamic data structure


A data structure whose size cannot be changed at the run-time is called static data structure. The size of a static data structure can neither be increased nor decreased at run-time. It may lead to the wastage or shortage of memory locations.

An array is an example of a static data structure. Once the size of an array is declared it can't be changed. If we declare an array of size 5 then we can't increase or decrease its size during run-time. If we utilize only 2 locations then the remaining memory is wasted. This problem is addressed by the introduction of the dynamic data structure.

A data structure whose size can be changed at the run-time is called dynamic data structure. The size of a dynamic data structure can either be increased or decreased at run-time. It prevents the wastage or shortage of memory locations.


Write a program in C++ to input an array of numbers and find the maximum number among it.



In the program, we input n number from the user and store it in the form of an array. Then we assign an extra variable with value 0. The extra variable is compared with all elements of the array. If any number in the array is greater than the extra variable then the greater value is assigned to the extra variable. This process is executed for all values of the array and we get the maximum number.  


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],n,i,max=0;
clrscr();
cout<<"How many numbers do you want to input=";
cin>>n;
cout<<endl;
if (n>50)
{
cout<<"Invalid input";
goto x;
}
for(i=1;i<=n;i++)
{
cout<<"Enter the number in a["<<i<<"]= ";
cin>>a[i];
if (a[i]>max)
max=a[i];
}
cout<<endl<<"The maximum number= "<<max;
x:getch();
}



Output


Searching maximum number in an array
FINDING THE MAXIMUM NUMBER












Explanation  

In the above program, we have entered 5(i.e. n=5) numbers. The for loop is executed as follows:

i(<=5)         max          a[i]                a[i]>max          Action              
1                 0               a[1]=67          True                 max=a[1]=67
2                 67             a[2]=88          True                 max=a[2]=88
3                 88             a[3]=110        True                 max=a[3]=110
4                 110           a[4]=98          False           
5                 110           a[5]=52          False   


In this way, the maximum value is stored in max variable and displayed on the screen. 





Saturday, 22 September 2018

Checking vowel and consonant in C++

Vowels and consonants

In the English alphabet, a,e,i,o,u are considered as vowels. All the other alphabets are called consonants.
Among 26 English alphabets, 5 are vowels and 21 are consonants.


Write a program in C++ to input a character from the user and check whether it is a vowel or a consonant using the switch statement.


We have to input a character from the user. We compare the entered character with the vowels. If it matches with any of the vowels, then we will print it is a vowel message. Else we will print it is a consonant message. We are using the switch statement which is conditional control statement. It is used when we have multiple cases for a single input.


The syntax of Switch statement:

switch (conditional variable)
{
case value 1:
statement;
break;
case value 2:
statement;
break;
.
.
.
default:
statement;
}

Break is a jump statement which transfers the control of execution out of the loop. It reduces the extra time which is consumed in checking remaining false condition, even after the true statement is already found.


Program  

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"PROGRAM TO CHECK A VOWEL OR CONSONANT";
cout<<"\nEnter a character=";
cin>>ch;
switch(ch)
{
case 'a':
cout<<"It is a vowel";
break;
case 'e':
cout<<"It is a vowel";
break;
case 'i':
cout<<"It is a vowel";
break;
case 'o':
cout<<"It is a vowel";
break;
case 'u':
cout<<"It is a vowel";
break;
default:
cout<<"It is a consonant";
break;
}
getch();
}


Output

Checking vowel or consonant in C++
CHECKING VOWEL AND CONSONANT







Explanation


We have entered the character i which is a vowel and matches the third case of the switch case. Then the break statement brings the control of execution out of the switch loop. This reduces the program execution time. If we enter any of the character other than vowel then the default case becomes true and it is a consonant message will be displayed on the screen.



Fact about C++

The advantage in C++ over C is that variables can be declared anywhere within the program. It must be declared just before the using the variable for the first time.





Friday, 21 September 2018

Comparing three numbers and displaying the maximum number in C++

Comparing three numbers


Comparing simply means finding out the greatest among input numbers. A number becomes the greatest, only if it is greater than the other two numbers.



Write a program in C++ to find out the maximum number out of three input numbers and display it.


Here we will input and store the three values in three different integer type variables. Then we will compare one number with the other two. A greatest among three will be displayed only if it is greater than the other two. If all are equal then they all are equal message will be displayed. If none of
the condition satisfies then error message will be displayed.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter 1st number=";
cin>>a;
cout<<"\nEnter 2nd number=";
cin>>b;
cout<<"\nEnter 3rd number=";
cin>>c;
if ((a>b)&&(a>c))
cout<<"\nThe greatest number is= "<<a;
else if ((b>c)&&(b>a))
cout<<"\nThe greatest number is= "<<b;
else if ((c>b)&&(c>a))
cout<<"\nThe greatest number is= "<<c;
else if((a==b)&&(a==c))
cout<<"\nAll three numbers are equal";
else
cout<<"\nPlease input valid numbers";
getch();
}


Output


Finding the greatest of the three numbers
DISPLAYING THE GREATEST NUMBER OUT OF THREE











Explanation


In the program, we stored the entered value in a,b and c integer type variables. Then we compared
one with other two for all cases. After comparison, we get the greatest number which is displayed on the output screen. The third condition ((c>b)&&(c>a)) of if......else if statement is satisfied for our input (c=67), so the third number was displayed on the screen. 






Swapping of two values without using the third variable in C++

Swapping of values stored in two variables 


Swapping is the process of exchanging or interchanging the values stored in two variables. Swapping of two number is done by two or three variable methods. This program is done by two variable method.  E.g. a=4 and b=9 then after swapping the values, we get a=9 and b=4.


Write a program in C++ to swap the value of two integer variables by using two variable method.


Firstly we will take two variables as input from the user and store it in a and b. Then we will perform some addition and subtraction to swap the values in the variables as in the example.
E.g.  a=9; b=4;

Action          Result
a=a+b;          a=9+4=13;
b=a-b;           b=13-4=9;
a=a-b;           a=13-9=4;

Final result a=4;  b=9;


Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter first number in a=";
cin>>a;
cout<<"Enter second number in b=";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping:"<<endl;
cout<<"The value in a="<<a<<endl;
cout<<"The value in b="<<b;
getch();
}



Output


Interchanging the value of two variables without using the third variable
SWAPPING OF TWO VALUES USING TWO VARIABLES











Explanation

In this program, we swapped the values of a and b without using any other variables. After entering the values in a and b we performed addition and subtraction to interchanged the values. Firstly, we added the two numbers and stored in a. Then subtracted a and b and stored the value in b. Finally, we again subtracted the value of a and b and stored in a. Thus we obtained the interchanged values of the two variables.