August 4, 2016

Example of Switch Statement

Example of Switch Statement

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 float a,b,r;
 char ch;
 cout<<"Enter First Number :\t";
 cin>>a;
 cout<<"Enter Second Number :\t";
 cin>>b;
 cout<<"Enter operator (+,-,*,/,%) :\t";
 cin>>ch;
 cout<<"\n";
 switch (ch)
 {
  case '+': r=a+b;
        cout<<"\n Adding :\t"<<r;
        break;
  case '-': r=a-b;
        cout<<"\n Subtracting :\t"<<r;
        break;
  case '*': r=a*b;
        cout<<"\n Multipling :\t"<<r;
        break;
  case '/': if(b==0)
        cout<<"\nDivide by ZERO";
        else
        r=b/a;
        cout<<"\n Dividing :\t"<<r;
        break;
  case '%': if(b==0)
        cout<<"\nDivide by ZERO";
        else
        {
         int res,q;
         q=b/a;
         res=b - (q*a);
         res=r;
         cout<<"\n Modulus :\t"<<r;
         }
         break;
  default : cout<<"\nCheck your CHOICE";
  }
  getch();
 }

Prime Number

Prime Number

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int flag=1,num,i;
cin>>num;
for(i=2;i<num;i++)
  {
    if(num%i==0)
      { flag=0;break;}
  }
  if(flag==1)
  cout<<"\nPrime";
  else
  cout<<"\nNot Prime";
  getch();
  }

FACTORIAL

FACTORIAL

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,b,c=1,x,z;
cout<<"Enter Number:\t";
cin>>a;
if(a%2==0)
  {
   cout<<"\nEven";
   a=b;
   while(b)
     {
       z*=b;
       --b;
       }
       cout<<"\nThe Factorial of"<<b<<"is"<<z<<"\n";
   }else
      {
    for(x=2;x<z;x++)
      {
        if(z%x==0)
          {  z=0;break;
           }
      }
      if(z==1)
        {
        cout<<"\nOdd Prime";
        }else
        {
        cout<<"\nNot a Prime but ODD";
        }
        }
getch();
}

STRING PALINDROME

String is Palindrome or not. 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
 clrscr();
 char str[80];
 int i=0,flag=0,j,len;
 cout<<"Enter String :\t";
 gets(str);
 while(str[i]!='\0')
 {
  len++;
  i++;
  }
   for(i=0,j=len-1;i<j;i++,j--)
   {
    if(str[i]!=str[j])
    {
     flag=1;break;}
     }
     if(flag==0)
     cout<<"PALINDROME";
     else
     cout<<"NOT PALINDROME";
     getch();
     }

August 2, 2016

Comparing String without using the function (strcmp)

 Comparing String without using the function (strcmp)

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
 clrscr();
 char a[80],b[80];
 int i=0,j=0,flag=0,len,l;
 cout<<"Enter first string :\t";
 gets(a);
 cout<<"Enter second string :\t";
 gets(b);
  while(a[i]!='\0')
   {
     len++;
     i++;
    }
  while(b[j]!='\0')
  {
     l++;
     j++;
   }
   for(i=0,j=0;a[i]!='\0'||b[j]!='\0';i++,j++)
   {
     if((int)a[i]>(int)b[j])
     {flag=1;break;}
      else
      if((int)a[i]<(int)b[j])
     {flag=2;break;}
    }
    if(flag==1)
    cout<<"1";
    else
    if(flag==2)
    cout<<"-1";
    else
    cout<<"0";
    getch();
  }

LCM

LCM

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a,b,lcm;
 cout<<"Enter two no.'s :\t";
 cin>>a>>b;
 if (a%b==0)
 {
  lcm=b;
  }
else
    if(a%2==0 && b%2==0)
    {
    lcm=(a*b)/2;
    }
     else
     if(a%2==0||b%2==0)
     {
     lcm=a*b;
      }
      else
      {lcm=a*b;}

      cout<<"LCM = "<<lcm;
      getch();
}

Disqus Shortname

Comments system