Thursday, March 7, 2013

Pass/Fail using Logical Operators


Write a program for Pass/Fail using logical operators

#include <iostream.h>
#include <conio.h>
void main ()
{
int marks;
cout<<"Enter marks: ";
cin>>marks;
cout<<(marks>=33? "Pass" : "Fail");
getch();
}

Swapping with TEMP


Write a program for swapping two numbers with the help of TEMP.

#include <iostream.h>
#include <conio.h>
void main()
{
int a,b, temp;
cout<<"Enter value for a: ";
cin>>a;
cout<<"Enter value for b: ";
cin>>b;
temp=a;
a=b;
b=temp;
cout<<"Value of a: " <<a<<"Value of b: "<<b;

getch();
}

Swapping two numbers


Write a program swapping two numbers.

#include <iostream.h>
#include <conio.h>
void main()
{
int a,b;
cout<<"Ener value for a: ";
cin>>a;
cout<<"Enter value for b: ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"Value of a: "<<a<<"\n""Value of b: "<<b;
getch();
}

Addition with two variables


Write a program for addition using only two variables.

#include <iostream.h>
#include <conio.h>
void main()
{
int a,b;
cout<<"Enter value for a: ";
cin>>a;
cout<<"Enter value for b: ";
cin>>b;
cout<<"a+b = " <<a+b;

getch();
}

Leap Year


Write a program for leap year using logical operators.

#include <iostream.h>
#include <conio.h>
void main()
{
int year;
clrscr();
cout<<"Enter year to check:";
cin>>year;
cout<<((year%400==0)||((year%100!=0)&&(year%4==0))) ? "Not Leap Year" : "Leap Year";

getch();
}

3-digit in Reverse


Write a program that user inputs 3-digit number and its output in reverse order.

#include <iostream.h>
#include <conio.h>
void main()
{
int digit,a,b,c,num,rev;
cout<<"Enter a three-digit number: ";
cin>>digit;
a=digit%10;
num=digit/10;
rev=a;
b=num%10;
num=num/10;
rev=(rev*10)+b;
c=num%10;
num=num/10;
rev=(rev*10)+c;
cout<<"\n Orignal number is: "<<digit;
cout<<"\n Reverse number is: "<<rev;
getch();
}

Units Tens Hundreds


Write a program that user inputs a 3-digit number and it converts into “Units”, “Tens”, 
“Hundreds”

#include <iostream.h>
#include <conio.h>
void main()
{
int num;
cout<<"Enter three-digit number: ";
cin>>num;
cout<<num%10<<": Units"<<"\n";
cout<<(num/10)%10<<": Tens"<<"\n";
cout<<(num/100)%10<<": Hundreds"<<"\n";

getch();
}

Getting started with Hello World!


Getting started with Hello World!
#include <iostream.h>
#include <conio.h>
void main ()
{
cout<<"Hello World!";
getch();
}