Sunday, 17 February 2019

C++ Program to Find the largest and smallest element in an array using 1D array

#include <iostream>

using namespace std;

int main()
{
    int a[100],mx,mn,i,n;

    cout<<"Enter the number of elements\n";
    cin>>n;

    cout<<"\nEnter the array elements \n";
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }

    mx=mn=a[0];

    for(i=0;i<n;i++)
    {
        if(mx < a[i]) mx=a[i];
        if(mn > a[i]) mn=a[i];
    }

    cout<<"\nLargest Number is "<<mx;
    cout<<"\nSmallest Number is "<<mn;

    return 0;
}

OUTPUT:

Enter the number of elements
5

Enter the array elements
6
1
3
5
9

Largest Number is 9
Smallest Number is 1

C++ Program to Check whether the given number is palindrome or not

#include <iostream>

using namespace std;

int main()
{
    int n,num,m,r=0;
    cout<<"Enter The Number\n";
    cin>>n;
    num=n;
    do
    {
        m=n%10;
        r=(r*10)+m;
        n=n/10;
    }while(n!=0);

    if (num==r)
        cout<<"\n"<<num<<" is Palindrome\n";
    else
        cout<<"\n"<<num<<" is not Palindrome\n";
    return 0;
}

OUTPUTE:

Enter The Number
121

121 is Palindrome

C++ Program to Check Whether a given date is valid or not

#include <iostream>

using namespace std;

int main()
{
    int dd,mm,yy,v;
    cout<<"Enter the Date\n";
    cin>>dd>>mm>>yy;
    if((yy<0) || (mm<1) || (mm>12) || (dd>31) || (dd<1))
        v=0;
    else if(((mm==4) || (mm==6) || (mm==9) || (mm==11)) && (dd>30) )
    v=0;
    else if((mm==2) && (yy%4==0) && (dd>29))
    v=0;
    else if( (mm==2) && (dd>28))
        v=0;
    if(v==0)
        cout<<"\nDate is Invalid\n";
        else
            cout<<"\nDate is valid\n";

    return 0;
}

OUTPUT:-

Enter the Date
14
02
2019

Date is valid