0% found this document useful (0 votes)
4 views

manual c++-1

The document contains a series of programming exercises for an Introduction to Computing lab at Aksum University, focusing on basic C++ programming concepts. It includes tasks such as printing 'Hello World', calculating sums, converting temperatures, and determining character types based on ASCII values. Each exercise is accompanied by sample code and prompts for user input.

Uploaded by

gebrusiye9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

manual c++-1

The document contains a series of programming exercises for an Introduction to Computing lab at Aksum University, focusing on basic C++ programming concepts. It includes tasks such as printing 'Hello World', calculating sums, converting temperatures, and determining character types based on ASCII values. Each exercise is accompanied by sample code and prompts for user input.

Uploaded by

gebrusiye9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE

VARIABLE, OPERATOR cout<< "\nEnter first number : ";


cin>>a;
AND EXPRESSION cout<<"\nEnter second number : ";
Question 1 Write a program to print cin>>b;
HELLO WORLD on screen. c=a+b;
cout<<"\nThe Sum is : "<<c;
#include<iostream.h> return 0;
}
int main()
Question 4 Write a program which
{ accept temperature in Fahrenheit and
cout<<"Hello world";
return 0;
print it in centigrade.
}
#include<iostream.h>
Question 2 Write a program to display
the following output using int main()
a single cout statement. {
float F,C;
Subject Marks cout<< "\nEnter temperature
Mathematics 90 in Farenheit : ";
cin>>F;
Computer 77 C=5*(F-32)/9;
Chemistry 69 cout<<"Temperature in
celcius is : "<<C;
#include<iostream.h> return 0;
}
using namespace std; Question 5 Write a program which
int main() accept principle, rate and time from user
{
cout<<"subject " and print the simple interest.
#include<iostream.h>
<<"\tmarks"<<"\nmathematic\t"
using namespace std;
<<90<<"\ncomputer\t"<<77<<"\
int main()
nchemistry\t"<<69;
{
return 0;
float F,C;
}
cout<< "\nEnter temperature
Question 3 Write a program which in Farenheit : ";
accept two numbers and print their sum. cin>>F;
C=5*(F-32)/9;
#include<iostream.h> cout<<"Temperature in
using namespace std; celcius is : "<<C;
int main() return 0;
{ }
int a,b,c;

1 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


Question 6 Write a program which cin>>r;
area = 3.14*r*r;
accepts a character and
display its ASCII value. cout<<"Area of circle :
"<<area;
#include<iostream.h> return 0;
using namespace std; }
int main()
{
9.Write a program to check whether the
char ch; given number is positive or negative
cout<<"\nEnter any character (using ? : ternary operator )
: ";
cin>>ch; #include<iostream.h>
cout<<"ASCII equivalent is : using namespace std;
"<<static_cast<int>(ch);
return 0; int main()
} {
Question 7 Write a program to swap the int a;
cout<<"Enter any non-zero
values of two variables. Number : ";
#include<iostream.h>
cin>>a;
using namespace std;
(a>0)?cout<<"Number is
positive":cout<<"Number is
int main()
negative";
{
return 0;
int a,b,temp;
}
cout<<"\nEnter two numbers :
"; Question 10 Write a program to
cin>>a>>b; check whether the given number is even
temp=a; or odd (using ? : ternary operator )
a=b;
b=temp;
cout<<"\nAfter swapping
#include<iostream.h>
numbers are : "; using namespace std;
cout<<a<<" "<<b; int main()
return 0; {
} int a;
cout<<"Enter the Number : ";
Question 8 Write a program to calculate cin>>a;
area of circle. (a%2==0)?cout<<"Number is
even":cout<<"Number is odd";
#include<iostream.h> return 0;
using namespace std; }
int main()
{
float r,area;
cout<< "\nEnter radius of
circle : ";

2 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE

FLOW OF CONTROL 1 Question Write a program to calculate


3 the total expenses. Quantity
and price per item are input
Question Any integer is input by the
by the user and discount of
1 user. Write a program to find
10% is offered if the expense is
out whether it is an odd
more than 5000.
number or even number.

#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{
{
int a; int totalexp, qty, price,
discount;
cout<<"Enter any number : ";
cin>>a;
cout<<"Enter quantity:";
cin>>qty;
if(a%2==0)
cout<<"The number is cout<<"Enter price:";
cin>>price;
even";
else
cout<<"The number is totalexp=qty*price;
odd";
if(totalexp>5000)
{
return 0;
}
discount=(totalexp*0.1);
totalexp=totalexp-
Question Find the absolute value of a discount;
2 number entered by the user. }
#include<iostream>
using namespace std; cout<<"Total Expense is Rs.
int main() "<<totalexp;
{ return 0;
int a; }
cout<<"Enter any number:";
cin>>a; Question Write a program to determine
4 whether the seller has made
if(a>0) profit or incurred loss. Also
cout<<"The absolute determine how much profit he
value of number is:"<<a;
made or loss he incurred. Cost
else
cout<<"The absolute price and selling price of an
value of number is:"<<-(a); item is input by the user.
return 0;
}

3 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


cin>>sulabh_age;
cout<<"Enter Ajay
age:";
cin>>ajay_age;

if (ram_age<sulabh_age
#include<iostream> && ram_age<ajay_age)
using namespace std; cout<<"Ram is
int main() youngest";
{ else
int cp,sp,result; if(sulabh_age<ram_age &&
sulabh_age<ajay_age)
cout<<"Enter cost price of cout<<"Sulabh
item : "; is youngest";
cin>>cp; else
cout<<"Enter selling price cout<<"Ajay is
of item : "; youngest";
cin>>sp; return 0;
}
result=sp-cp;

if(result>0) Question 6 Write a program to


cout<<"Profit : check whether a triangle is valid or
"<<result; not, when the three angles of the
else triangle are entered by the user. A
if(result<0)
cout<<"Loss : triangle is valid if the sum of all the
"<<-(result); three angles is equal to 180 degrees.
else
cout<<"No
profit no loss";
return 0; using namespace std;
} int main()
Questio If the ages of Ram, Sulabh and Ajay {
int
n5 are input by the user, write a angle1,angle2,angle3;
program to determine the youngest cout<<"Enter the three
of the three. angles of triangle:";
#include<iostream> cin>>angle1>>angle2>>an
using namespace std; gle3;
int main()
{ if
int (angle1+angle2+angle3==180)
ram_age,sulabh_age,ajay_age; cout<<"Triangle
cout<<"Enter Ram age:"; is valid";
cin>>ram_age; else
cout<<"Enter Sulabh cout<<"Triangle
age:"; is not valid";

4 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


return 0; and DA = 98% of basic salary. If the
} employee's salary is input by the
user write a program to find his
gross salary.
Questio Any year is input by the user. Write
n7 a program to determine whether the
year is a leap year or not. #include<iostream>
#include<iostream> using namespace std;
using namespace std; int main()
int main() {
{ float basic_salary,
int year; gross_salary, HRA, DA;
cout<<"Enter basic salary of
cout<<"Enter the year : Employee : ";
"; cin>>basic_salary;
cin>>year;
if (basic_salary<1500)
if( (year%400==0 || {
year%100!=0) &&(year%4==0))
cout<<"It is a HRA=0.1*basic_salary;
leap year"; DA=0.9*basic_salary;
else }
cout<<"It is else
not a leap year"; {
return 0; HRA=500;

DA=0.98*basic_salary;
}

gross_salary=basic_salary+HR
A+DA;
cout<<"Gross salary is :
"<<gross_salary;
return 0;
}
Question Write a program to calculate
9 the monthly telephone bills as
Questio In a company an employee is paid per the following rule:
n8 as under: If his basic salary is less Minimum Rs. 200 for upto 100
than Rs. 1500, then HRA = 10% of calls. Plus Rs. 0.60 per call for
basic salary and DA = 90% of basic next 50 calls. Plus Rs. 0.50 per
salary. call for next 50 calls. Plus Rs.
If his salary is either equal to or 0.40 per call for any call
above Rs. 1500, then HRA = Rs. 500 beyond 200 calls.

5 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


#include<iostream> {
using namespace std; float a,b,c,d,root1,root2;
int main() cout<<"Enter value of a, b
{ and c : ";
int calls; cin>>a>>b>>c;
float bill;
cout<<"Enter number of calls d=b*b-4*a*c;
: ";
cin>>calls; if(d==0)
{
if(calls<=100) root1=(-b)/(2*a);
bill=200; root2=root1;
else if (calls>100 && cout<<"Roots are
calls<=150) real & equal";
{ }
calls=calls-100; else if(d>0)
{
bill=200+(0.60*calls); root1=-
} (b+sqrt(d))/(2*a);
else if (calls>150 && root2=-(b-
calls<=200) sqrt(d))/(2*a);
{ cout<<"Roots are
calls=calls-150; real & distinct";
}
bill=200+(0.60*50)+(0.50*cal else
ls); {
} root1=(-b)/(2*a);
else root2=sqrt(-
{ d)/(2*a);
calls=calls-200; cout<<"Roots are
imaginary";
bill=200+(0.60*50)+(0.50*50) }
+(0.40*calls);
} cout<<"\nRoot 1=
"<<root1<<"\nRoot 2= "<<root2;
cout<<" Your bill is return 0;
Rs."<<bill; }
return 0; Question The marks obtained by a
}
11 student in 5 different subjects
Question Write a program to find the are input by the user. The
10 roots of and quadratic student gets a division as per
equation of type ax2+bx+c the following rules:Percentage
where a is not equal to zero. above or equal to 60 - First
#include<iostream>
#include<math.h> division Percentage between
using namespace std; 50 and 59 - Second division
int main() .Percentage between 40 and 49
6 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


- Third division Percentage characters.
less than 40 - Fail .Write a Characters ASCII Values
program to calculate the A–Z 65 – 90
division obtained by the a–z 97 – 122
student. 0–9 48 – 57
#include<iostream>
using namespace std; special 0 - 47, 58 - 64, 91 - 96,
int main() symbols 123 – 127
{ #include<iostream>
int using namespace std;
sub1,sub2,sub3,sub4,sub5,percentage int main ()
; {
cout<<"Enter marks of five char ch;
subjects : "; cout<<"Enter any
cin>>sub1>>sub2>>sub3>>sub4> character:";
>sub5; cin>>ch;
percentage=(sub1+sub2+sub3+s
ub4+sub5)/5; if (ch>=65 && ch<=90)
cout<<"Character is
if(percentage>=60) a capital letter";
cout<<"Ist else if (ch>=97 && ch<=122)
division"; cout<<"Character is
else if(percentage>=50) a small letter";
cout<<"IInd else if (ch>=48 && ch<=57)
division"; cout<<"Character is
else if(percentage>=40) a digit";
cout<<"IIIrd else if ((ch>0 &&
division"; ch<=47)||(ch>=58 && ch<=64)||
else (ch>=91 &&
cout<<"Fail" ; ch<=96)||(ch>=123 && ch<=127))
return 0; cout<<"Character is
} a special symbol";
return 0;
Question 12 Any character is }
entered by the user;
write a program to
determine whether the
character entered is a
capital letter, a small
case letter, a digit or a
special symbol. The
following table shows
the range of ASCII
values for various

7 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


i++;
}

cout<<"Sum :"<<sum;
return 0;
}

Question 3 Write a program to find the


factorial value of any
number entered through the
FLOW OF CONTROL 2 keyboard.
Question 1 Write a program to print
#include<iostream>
number from 1 to 10. using namespace std;

#include<iostream> int main()


using namespace std; {
int n,fact=1;
int main() cout<<"Enter any number : ";
{ cin>>n;
int i=1;
while(n>=1)
while(i<=10) {
{ fact*=n;
cout<<i<<"\n"; n--;
i++; }
}
return 0; cout<<"Factorial :"<<fact;
} return 0;
}
Question 2 Write a program to calculate
the sum of first 10 natural Question 4 Two numbers are entered
number. through the keyboard. Write
a program to find the value
#include<iostream> of one number raised to the
using namespace std; power of another.
#include<iostream>
int main() using namespace std;
{
int i=1,sum=0; int main()
{
while(i<=10) int n,p,r=1;
{ cout<<"Enter the base number
sum+=i; and exponent ";

8 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


cin>>n>>p; cout<<"Enter any number : ";
cin>>n;
for(int i=1;i<=p;i++) t=n;
r=r*n;
while(t>0)
cout<<"Result :"<<r; {
return 0; r=t%10;
} sum+=r;
t=t/10;
}
Question 5 Write a program to reveres
any given integer number. cout<<"Sum of digits of
#include<iostream> number "<<n<<" is "<<sum;
using namespace std; return 0;
}
int main()
{ Question 7 Write a program to check
int n,t,r,rev=0;
given number is prime or
cout<<"Enter any number : ";
cin>>n; not.
t=n; #include<iostream>
using namespace std;
while(t>0)
{ int main()
r=t%10; {
t=t/10; int n;
rev=rev*10+r; bool flag=false;
} cout<<"Enter any number : ";
cin>>n;
cout<<"Reverse of number
"<<n<<" is "<<rev; for(int i=2;i<n;i++)
return 0; {
} if(n%i==0)
{
flag=true;
break;
Question 6 Write a program to sum of }
digits of given integer }
number. if(flag==false && n>1)
cout<<"Number is
#include<iostream> prime";
using namespace std; else
cout<<"Number is not
int main() prime";
{ return 0;
int n,t,r,sum=0; }

9 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


char choice;

do
Question 8 Write a program to calculate {
HCF of Two given number. cout<<"Enter number
#include<iostream> ";
using namespace std; cin>>n;

int main() if(n>0)


{ sum_p++;
int dividend, divisor, rem, else if(n<0)
hcf; sum_n++;
cout<<"Enter two numbers : else
"; sum_z++;
cin>>dividend>>divisor;
cout<<"Do you want
while(rem!=0) to Continue(y/n)? ";
{ cin>>choice;

rem=dividend%divisor; }while(choice=='y' ||
if(rem==0) choice=='Y');
hcf=divisor;
else
{ cout<<"Positive Number
:"<<sum_p<<"\nNegative Number
dividend=divisor; :"<<sum_n<<"\nZero Number
divisor=rem; :"<<sum_z;
} return 0;
} }

cout<<"HCF is : "<<hcf; Question 10 Write a program to enter the


return 0; numbers till the user wants
}
and at the end it should
display the maximum and
Question 9 Write a program to enter the minimum number entered.
numbers till the user wants
and at the end it should #include<iostream>
using namespace std;
display the count of positive,
negative and zeros entered. int main()
#include<iostream> {
using namespace std; int n, max=0, min=32767;
int main() char choice;
{
int n, sum_p=0, sum_n=0, do
sum_z=0; {

10 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


cout<<"Enter number digit2=i/10 -
: "; digit1*10;
cin>>n; digit3=i%10;

if(n>max)
max=n; if(digit1*digit1*digit1 +
if(n<min) digit2*digit2*digit2 +
min=n; digit3*digit3*digit3 == i)

cout<<"Do you want cout<<i<<endl;


to Continue(y/n)? "; }
cin>>choice; return 0;
}
}while(choice=='y' ||
choice=='Y');
Question 12 Write a program to print
Fibonacci series of n terms
cout<<"Maximum Number where n is input by user :
:"<<max<<"\nMinimum Number :"<<min; 0 1 1 2 3 5 8 13 24 .....
return 0;
}
#include<iostream>
Question 11 Write a program to print out using namespace std;
all Armstrong numbers
between 1 and 500. If sum
of cubes of each digit of the int main()
{
number is equal to the int f=0,s=1,t,n;
number itself, then the
number is called an cout<<"Enter Number of terms
Armstrong number. of Series : ";
For example 153 = ( 1 * 1 * cin>>n;
1)+(5*5*5)+(3*3* cout<<f<<" "<<s<<" ";
3)
#include<iostream> for(int i=3;i<=n;i++)
using namespace std; {
t=f+s;
int main() cout<<t<<" ";
{ f=s;
int n,digit1,digit2,digit3; s=t;
}
for(int i=1;i<=500;i++) return 0;
{ }
digit1=i/100;

11 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


Question 13 Write a program to calculate sign *= -1;
the sum of following series sum += sign*1.0/i;
}
where n is input by user. cout<<"log 2 : "<<sum;
1 + 1/2 + 1/3 + 1/4 + 1/5 return 0;
+…………1/n }
#include<iostream>
using namespace std;

int main()
{
int i,n;
float sum=0;

cout<<"Enter the value of n


";
cin>>n;

for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;
return 0;
}

Question 14 Compute the natural


logarithm of 2, by adding up
to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -...
1/n
where n is a positive integer
and input by user.
#include<iostream>
using namespace std;

int main()
{
int i,n,sign=-1;
float sum=0;
cout<<"Enter the value of n
";
cin>>n;

for(i=1;i<=n;i++)
{

12 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE

FLOW OF CONTROL
[SET – 3]
Question 1
i) ********** ii) * iii) *
********** ** **
********** *** ***
********** **** ****
***** *****

iv) * v) 1 vi) 1
*** 222 212
***** 33333 32123
******* 4444444 4321234
********* 555555555 543212345
Question 2 Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the computation
should use all terms in the series up through the term involving xn
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........
Question 3 Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving xn
cos x = 1 - x2/2! + x4/4! - x6/6! .....

13 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


1/ return 0;
//Solution of (i) }
#include<iostream>
using namespace std;
int main() //Solution of (iv)
{ #include<iostream>
int i,j; using namespace std;
for(i=1;i<=4;i++) int main()
{ {
for(j=1;j<=10;j++) int i,j,k;
cout<<'*'; for(i=1;i<=5;i++)
cout<<endl; {
} for(j=5;j>i;j--)
return 0; cout<<' ';
} for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
//Solution of (ii) }
#include<iostream> return 0;
using namespace std; }
int main()
{
int i,j; //Solution of (v)
for(i=1;i<=5;i++) #include<iostream>
{ using namespace std;
for(j=1;j<=i;j++) int main()
cout<<'*'; {
cout<<endl; int i,j,k;
} for(i=1;i<=5;i++)
return 0; {
} for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<2*i;k++)
//Solution of (iii) cout<<i;
#include<iostream> cout<<endl;
using namespace std; }
int main() return 0;
{ }
int i,j,k;
for(i=1;i<=5;i++) //Solution of (vi)
{ #include<iostream>
for(j=5;j>i;j--) using namespace std;
cout<<' '; int main()
for(k=1;k<=i;k++) {
cout<<'*'; int i,j,k,l;
cout<<endl; for(i=1;i<=5;i++)
} {
for(j=5;j>i;j--)

14 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE


cout<<' '; cout<<"Enter the value of n
for(k=i;k>=1;k--) : ";
cout<<k; cin>>n;
for(l=2;l<=i;l++) for(i=2;i<=n;i+=2)
cout<<l; {
cout<<endl; fact=1;
} for(j=1;j<=i;j++)
return 0;} {
2/ p=p*x;
#include<iostream> fact=fact*j;
using namespace std; }
int main() sum+=sign*p/fact;
{ sign=-1*sign;
int i,j,n,fact,sign=-1; }
float x, p=1,sum=0; cout<<"cos "<<x<<"="<<1+sum;
cout<<"Enter the value of x return 0;
: "; }
cin>>x;
cout<<"Enter the value of n
: ";
cin>>n;

for(i=1;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;
return 0;
}

3/
#include<iostream>
using namespace std;
int main()
{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x
: ";
cin>>x;

15 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17
AKSUM UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINERING

INTRODUCTION TO COMPUTING -LAB EXERCISE

16 Department of Electrical and computer engineering compiled by :-Araya .M

December 2016/17

You might also like