2. 1.Write a program thataccepts a characterfrom the user and display
its ASCII Value.
#include<iostream>
usingnamespace std;
intmain()
{
char c;
cout << "Entera character : ";
cin >> c;
cout << "ASCIIvalue of " << c <<" is: " << (int)c;
return0;
}
OUTPUT:
3. 2.Write aprogram in C++ that inputs name, age from the user and thendisplays
these values onthe
Screen?
#include <iostream>
usingnamespace std;
#define MAX_LENGTH100
intmain()
{
char name[MAX_LENGTH]={0};
intage;
cout<<"Enter name of the person:";
cin.getline(name,MAX_LENGTH);
cout<<"Enter age:";
cin>>age;
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
return0;
}
OUTPUT:
4. 3. Write a program that reads a four digit number from user,then the
program separatesdigits of the
number e.g.4567 to displayed as:
4
5
6
7
#include <iostream>
usingnamespace std;
intmain()
{
intnum;
cout << "Entera four-digitnumber:";
cin >> num;
cout <<num /1000<< endl;
num=num % 1000;
cout <<num /100<<endl;
num=num % 100;
cout<<num /10<<endl;
num=num % 10;
cout<<num <<endl;
return0;
}
OUTPUT:
5. 4. Write aprogram that take temperature inFahrenheitfromuser and showthe
equivalent temperature
in Celsius.
FORMULA:
[c = (f - 32) * 5/9; ]
ConvertFahrenheittoCelsiusinC++
#include<iostream>
usingnamespace std;
int main()
{
floatcel,far;
cout<<"Enter temp.inFahrenheit:";
cin>>far;
cel = (far- 32) * 5/9;
cout<<"Temp.inCelsius:"<<cel;
return0;
}
OUTPUT:
6. 5. Write a program in C++ which would swap the values of two variables using the
third variable.
#include <iostream>
usingnamespace std;
intmain()
{
inta = 5, b = 10, temp;
cout << "Before swapping."<<endl;
cout << "a = " << a << ", b = " << b << endl;
temp= a;
a = b;
b = temp;
cout << "nAfterswapping."<<endl;
cout << "a = " << a << ", b = " << b << endl;
return0;
}
OUTPUT:
7. 6. Write a program in C++ which would swap the values of two variables without
using the third variable.
#include<iostream>
usingnamespace std;
intmain()
{
inta,b;
cout<<"Enter the firstno=:";
cin>>a;
cout<<"nnEnterthe secondno=:";
cin>>b;
cout<<"na"<<" is ="<<a<<"nb"<<" is="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"afterswap number";
cout<<"nna="<<a<<"nb="<<b;
return0;
}
OUTPUT:
8. 7.Check the output of the following piece of code on compiler and explain why is
it so:
ANS= in this program swap the value using of two number .
#include<iostream>
usingnamespace std;
intmain()
{
char c = 'a';
intn = 'a';
cout << "c = " << c << endl;
cout << "n = " << n << endl;
return0;
}
OUTPUT: