Tuesday, 11 October 2022

UNIT 03: INPUT/OUTPUT HANDLING IN C++ - Lab Activities - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 03: Input / Output Handling In C++
Lab Activities

Q.1: Develop programs for manipulating the following formulas.
Title Formula Description
 Calculating speed of an objects=d/t Speed = distance / time
 Newton's second law of motionF=ma Force = mass x acceleration
 Calculating acceleration a = (vf-vi) / t Acceleration is equal to (final velocity - initial velocity) / time
 Area of trianglea = 1/2bh Area = 1/2 (base)(height)
 Convert the Celsius to FahrenheitF = (c*1.8) + 32-

Ans: (i) Calculating speed of an object

 #include<iostream>
 #include <conio.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 float speed;
 float d;
 float t;
 cout << "\n \t Enter distance = ";
 cin >> d;
 cout << "\n\t Enter time = ";
 cin >> t;
 speed = d/t;
 cout << "\n\t The speed of the object is = " << speed <<" m/s";
 return 0;
 }



(ii) Newton's second law of motion

 #include <iostream>
 #include <conio.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 float f;
 float m;
 float a;
 cout << "\n \t Enter mass = ";
 cin >> m;
 cout << "\n\t Enter acceleration = ";
 cin >> a;
 f = m*a;
 cout << "\n\t The value of F is = " << f <<" N";
 return 0;
 }



(iii) Calculating acceleration

 #include <iostream>
 #include <conio.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 float in_velocity;
 float acceleration;
 float f_velocity;
 float time;
 cout << "\n \t Enter the initial velocity of an object in m/s = ";
 cin >> in_velocity;
 cout << "\n\t Enter the final velocity of an object in m/s = ";
 cin >> f_velocity;
 cout << "\n \t Enter the time in seconds = ";
 cin >> time;
 acceleration = (f_velocity - in_velocity)/ time;
 cout << "\n\t The Acceleration of the object is = " << acceleration <<" m/s2";
 return 0;
 }



(iv) Area of triangle

 #include <iostream>
 #include <conio.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 float base;
 float height;
 float area;
 cout << "\n \t Enter the base of triangle = ";
 cin >> base;
 cout << "\n\t Enter the height of triangle = ";
 cin >> height;
 area = (base * height);
 cout << "\n\t The area of the triangle is = " << area<<" s2";
 return 0;
 }



(v) Convert the Celsius to Fahrenheit

 #include <iostream>
 #include <conio.h>
 #include <stdio.h>
 using namespace std;
 int main()
 {
 float celsius;
 float fahrenheit;
 cout << "\n \t Enter the temperature in Celsius = ";
 cin >> celsius;
fahrenheit = (celsius * 1.8) + 32;
 cout << "\n\t The temperature in Fahrenheit is = " << fahrenheit<<" F";
 return 0;
 }



Q.2: Write a program to calculate the volume of a box.
Ans: VOLUME OF A BOX:

 #include<iostream>
 #include<conio.h>
 #include<stdio.h>
 using namespace std;
 int main()
 {
 float length;
 float height;
 float volume;
 float width;
 cout << "\n \t Enter the length of box = ";
 cin >> length;
 cout << "\n \t Enter the height of box = ";
 cin >> height;
 cout << "\n \t Enter the width of box = ";
 cin >> width; 
 volume = (length*height*width);
 cout << "\n \t The volume of the box is = " <<volume;
 return 0;
 }



Q.3: Write a program of marksheet, take input of five subjects, print its total and percentage also.
Ans: Marks Sheet

 #include<iostream>
 #include<conio.h>
 #include<stdio.h>
 using namespace std;
 int main()
 {
 int phy, eng, comp, chem, urdu;
 int obt_marks, per;

 cout << "\n \t Enter Chemistry Marks = ";
 cin >> chem;
 cout << "\n \t Enter Physics Marks = ";
 cin >> phy;
 cout << "\n \t Enter Computer Marks = ";
 cin >> comp;
 cout << "\n \t Enter English Marks = ";
 cin >> eng;
 cout << "\n \t Enter Urdu Marks = ";
 cin >> urdu;
 obt_marks = chem + phy + comp + eng + urdu;
 per = (obt_marks*100) /500;
 cout << "\n \t The total marks are = " << obt_marks;
 cout << "\n \t The percentage is = " << per <<"%\n";
 return 0;
 }


Q.4: Write a code to calculate mathematical expression of a2 + 2ab +b2.
Ans: Mathematical Expression:

 #include<iostream>
 #include<conio.h>
 #include<stdio.h>
 using namespace std;
 int main()
 {
 float sol;
 int a, b;
 cout << "\n\t Program to calculate expression a2 + 2ab +b2";
 cout << "\n\t Enter the value of a. \t";
 cin >> a;
 cout << "\n\t Enter the value of b. \t";
 cin >> b;
 sol = (a*a) + 2*(a*b) + (b*b);
 cout << "\n\t The solution of expression a2 + 2ab +b2 for given a and b is. \t" << sol;
 return 0;
 }


Q.5: list out errors from the C++ program and remove those errors, write the output.
#include<iostream.h>
using namespace std
int main (void);
{
int x;
cout << "\n Enter the value of x........";
cin >> x
cout << "\n The square of x......." << a*a;
return 0;
}

Ans: List of errors:
  1. #include<iostream.h>
  2. using namespace std
  3. int main (void);
  4. {
  5. int x;
  6. cout << "\n Enter the value of x........";
  7. cin >> x
  8. cout << "\n The square of x......." << a*a;
  9. return 0;
  10. }


Line No. Error
1. iostream.h
2. Terminator (;) missing after std
3. Terminator (;) present after main function
7. Terminator (;) missing after cin function
8. Wrong variable used in square expression (a)

Correct Program:

 #include<iostream>
 using namespace std;
 int main (void)
 {
 int x;
 cout << "\n Enter the value of x .....";
 cin >> x;
 cout << "\n The square of x ....." << x*x;
 return 0;
 }

OUTPUT:
Enter the value of x ..... 5
The square of x ..... 25





No comments:

Post a Comment