C++ Assignment (Mulualem and ... )
C++ Assignment (Mulualem and ... )
1. Write a program in C++ to find Size of fundamental data types. (char, short, int, long, long
long, float, double, long double, bool)
#include<iostream>
using namespace std;
int main(){
cout<<"size of char:"<<sizeof(char)<< "byte"<<endl;
cout<<"size of int:"<<sizeof(int)<<"byte"<<endl;
cout<<"size of short:"<<sizeof(short)<<"byte"<<endl;
cout<<"size of long:"<<sizeof(long)<<"byte"<<endl;
cout<<"size of long long:"<<sizeof(long long)<<"byte"<<endl;
cout<<"size of float:"<<sizeof(float)<<"byte"<<endl;
cout<<"size of double:"<<sizeof(double)<<"byte"<<endl;
cout<<"size of long double:"<< sizeof(long double)<<"byte"<<endl;
cout<<"size of bool:"<<sizeof(bool)<<"byte"<<endl;
return 0;
}
#include <iostream>
return 0;
}
1
3. Write a program in C++ to convert temperature in Fahrenheit to Kelvin.
#include<iostream>
using namespace std;
double fahrenheittokelvin(double fahrenheit)
{
double kelvin=(fahrenheit - 32) * 5/9 + 273.15;
return kelvin;
}
int main()
{
double fahrenheit;
cout<<" enter temperature in fahrenheit";
cin>>fahrenheit;
double kelvin=fahrenheittokelvin(fahrenheit);
cout<<"temperature in kelvin"<<kelvin<<endl;
return 0;
4. Write a C++ program to display the following by using for loop statement
(1, 1) (1, 2) (1, 3) (1, 4) (3, 1) (3, 2) (3, 3) (3, 4)
#include<iostream>
using namespace std;
int main()
{
for(int i=1; i<=3; i+=2)
{
for(int j=1; j<=4; j++)
{
cout<<"("<<i<<*" ,"<<j<<")";
}
}
cout<<endl;
return 0;
2
5. Write a program in C++ to compute the total and average of four numbers.
#include <iostream>
return 0;
}
6. Write a program in C++ that takes a number as input and prints its multiplication table up
to 10.
#include <iostream>
3
7. Write a program in C++ which accepts the user's first and last name and print them in
reverse order with a space between them.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string fristname,lastname;
cout<<"enter your fristname";
cin>>fristname;
cout<<"enter your lastname";
cin>>lastname;
cout<<"Name in reverse order "<<lastname<<" "<<fristname<<endl;
return 0;
########
*******
&&&
***
1
#include<iostream>
using namespace std;
int main()
{
cout<<"########"<<endl;
cout<<"*******"<<endl;
cout<<"&&&"<<endl;
cout<<"***"<<endl;
cout<<"1"<<endl;
return 0;
}
4
9. Write a C++ program to display the following vertically using while loop statement.10, 11,
13, 14, 16, 18, 19, 20
#include <iostream>
using namespace std;
int main()
{
int i=10;
while(i<=20)
{
if(i==12||i==15||i==17)
goto increment;
cout<<i<<endl;
increment:
i++;
}
return 0;
}
1
12
123
1234
12345
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
12345
1234
123
12
1
5
#include<iostream>
using namespace std;
int main()
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
(a)
*
**
***
****
*****
******
*******
********
*********
**********
#include <iostream>
return 0;
}
6
(b)
**********
*********
********
*******
******
*****
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=i;j<=10;j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}
7
(c)
**********
*********
********
*******
******
*****
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
cout<<" ";
}
for(int j=i;j<=10;j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}
8
(d)
*
**
***
****
*****
******
*******
********
*********
**********
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=i;j<=10;j++)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}