August 4, 2016
Diamond Pattern
Diamond Pattern
#include<iostream.h>
#include<conio.h>void main()
{
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();
}
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();
}
Example Of IF Statement
Example Of IF Statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int age;
cout<<"Enter Age:\t";
cin>>age;
if(age>18);
{
cout<<"Yes";
cout<<"\nEligible for Voting";
}else
{
cout<<"\nSorry";
cout<<"\nNot eligible";
}
getch();
}
Calculating FORCE
C++ Program for Calculating FORCE
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float d,t,v,m,a,f;
cout<<"\nEnter Distance:\t";
cin>>d;
cout<<"\nEnter Time:\t";
cin>>t;
v=d/t;
cout<<"Velocity:\t"<<v;
a=v/t;
cout<<"\nAccleration:\t"<<a;
cout<<"\nEnter Mass:\t";
cin>>m;
f=m*a;
cout<<"\nForce:\t"<<f;
getch();
}
SWAPPING
C++ PROGRAM FOR SWAPPING
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
cout<<"x:\t";
cin>>x;
cout<<"\ny:\t";
cin>>y;
z=x-y;
cout<<"\nz:\t"<<z;
x=x-z;
cout<<"\nAfter swaping x=\t"<<x;
y=y+z;
cout<<"\nAfter swaping y=\t"<<y;
getch();
}
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
cout<<"x:\t";
cin>>x;
cout<<"\ny:\t";
cin>>y;
z=x-y;
cout<<"\nz:\t"<<z;
x=x-z;
cout<<"\nAfter swaping x=\t"<<x;
y=y+z;
cout<<"\nAfter swaping y=\t"<<y;
getch();
}
August 1, 2016
Curved Surface Area of Cone
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int l,r,csa;
cout<<"Enter Radius:\t";
cin>>r;
cout<<"\nEnter Slant Height:\t";
cin>>l;
csa=3.14*r*l;
cout<<"\nCurved Surface Area:\t"<<csa;
getch();
}
#include<conio.h>
void main()
{
clrscr();
int l,r,csa;
cout<<"Enter Radius:\t";
cin>>r;
cout<<"\nEnter Slant Height:\t";
cin>>l;
csa=3.14*r*l;
cout<<"\nCurved Surface Area:\t"<<csa;
getch();
}
Subscribe to:
Posts (Atom)