August 4, 2016
Diamond Pattern
Diamond Pattern
#include<iostream.h>
#include<conio.h>{
clrscr();
int i,j,n,k;
cin>>n; /* Enter Odd No. Only*/
for(i=0;i<n;i+=2)
{
for(j=0;j<n-i;j++)
{
cout<<" ";
}
for(k=1;k<i;k++)
{
cout<<" *";
}
cout<<endl;
}
for(i=0;i<n;i+=2)
{
for(j=1;j<=i;j++)
{
cout<<" ";}
for(k=0;k<n-i;k++)
{
cout<<"* ";
}
cout<<endl;
}
getch();
}
Calculating temperature in Celcius or Fahrenheit
Calculating temperature in Celsius or Fahrenheit
#include<iostream.h>#include<conio.h>
#include<math.h>
void main()
{
clrscr();
double t,ct,choice;
cout<<"Temperature Conversion Menu"<<"\n";
cout<<"1. Fahrenheit to Celsius"<<"\n";
cout<<"2. Celsius to Fahrenheit"<<"\n";
cout<<"Enter choice (1-2):\t";
cin>>choice;
if (choice==1)
{ cout<<"\n"<<"Enter temperature in Fahrenheit:\t";
cin>>t;
ct=(t-32)/1.8;
cout<<"Temperature in Celsius is "<<ct<<"\n";
}
else
{ cout<<"\n"<<"Enter temperature in Celsius:\t";
cin>>t;
ct=(1.8*t)+32;
cout<<"Temperature in Fahrenheit is :\t"<<ct;
}
getch();
}
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();
}
#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();
}
#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();
}
Subscribe to:
Posts (Atom)