You are on page 1of 11

Exp 2

Aim: Write C/C++ Program


i. Write a C Program to print “Welcome to C!”.
ii. Write a C++ program to take two numbers as input
and display their sum and average as output.
iii. Write a C++ program to take temperature in F as
input and print its equivalent in C. Use formula
C=(F-32)/1.8
iv. Write a C++ program to read basic salary and
increase rate on the basic salary of an employee and
then find the gross salary of the employee. The
following formulas can be used
basic_salary = basic_salary * rate;
gross_salary = basic_salary + da + hra;
da = basic_salary * 1.15;
hra = basic_salary * 0.1;

Theory:
Data types define the type of data a variable can hold, for example
an integer variable can hold integer data, a character variable can
hold character data etc.
Data types are categorised in three major groups: Primitive (Built-in),
User-defined and Derived.
Primitive Data Types
1. int – Used to indicate integers. Its size is usually 4 bytes. Meaning,
it can store values from -2147483648 to 2147483647.
2. float and double – Used to store floating-point numbers (decimals
and exponentials). The size of float is 4 bytes and the size of
double is 8 bytes. Hence, double has two times the precision of
float.
3. char – Used for characters. Its size is 1 byte. Characters in C++ are
enclosed inside singe quotes. They range from -128 to 127 or 0 to
255.
4. bool – This data type has one of two possible values: true or false.
Used in conditional statements and loops.
5. void – Indicates an absence of data. It means “nothing” or “no
value”. Used in functions and pointers.
6. wchar_t – This data type is same as char but has greater size. Its
size is 2 or 4 bytes.
User-Defined Data Types
1. Class - The building block of C++ that leads to Object-Oriented
programming is a Class. It is a user-defined data type, which
holds its own data members and member functions, which can
be accessed and used by creating an instance of that class. A
class is like a blueprint for an object.
2. Structure - A structure is a user defined data type in C/C++. A
structure creates a data type that can be used to group items of
possibly different types into a single type.
3. Union - Like Structures, union is a user defined data type. In
union, all members share the same memory location.
4. Enumeration - Enumeration (or enum) is a user defined data type
in C. It is mainly used to assign names to integral constants, the
names make a program easy to read and maintain.

Program (i):
Algorithm:
1. START
2. Print(“Welcome to C!”)
3. END
Flowchart:

Start

Print(“Welcome to C!”)

End

Program:
#include <stdio.h>
int main()
{
printf(“ Welcome to C! “);
return 0;
}
Output:
Program (ii):
Algorithm:
1. START
2. Declare two integer variables num1 and num2.
3. Take the two numbers as input in variables num1 and num2.
4. Declare two integer variables sum and avg to store the resultant
sum and average of the two numbers respectively.
5. Add the two numbers and store the result in the variable sum.
6. Calculate the average and store the result in the variable avg.
7. Print the values of the variables sum and avg.
8. END
Flowchart:
START

Declare variables

Take values

Sum = num1 + num2


Avg = (num1 + num2)/2

Print the values

END
Program:
#include <iostream>
using namespace std;
int main()
{
int num1 ,num2;
cout << “Enter First Number: “ <<;
cin >> num1;
cout << “Enter Second Number: “ <<;
cin >> num2;
int sum, avg;
sum = num1 + num2;
avg = (num1 + num2) / 2;
cout << “The sum of the two numbers is = “ << sum << endl;
cout << “The average of the two numbers is = “ << avg << endl;
return 0;
}
Output:
Program (iii):
Algorithm:
1. START
2. Declare two floating variables F and C.
3. Take the value for variable F.
4. Calculate C using the given formula
C = (F-32)/1.8
5. Print the value of variable C.
6. END
Flowchart:
START

Declare Variables

Take values

C = (F-32)/1.8

Print the value

END

Program:
#include <iostream>
using namespace std;
int main()
{
int F, C;
cout << “Enter temperature in degree Fahrenheit: “ <<;
cin << F;
C = ( F – 32 ) / 1.8;
cout << “The temperature in degree Celsius is = “ << C ;
return 0;
}
Output:

Program (iv):
Algorithm:
1. START
2. Declare floating variables basic_salary and rate.
3. Take values for both these variables.
4. Declare floating variables to store values of gross salary, da and
hra.
5. Calculate the increment in salary using the formula
basic_salary = basic_salary + (basic_salary*rate)
6. Calculate the values of gross salary, da and hra using the given
formulas
da = basic_salary*1.15;
hra = basic_salary*0.1;
gross_salary = basic_salary + da + hra;
7. Print the value of variable gross_salary.
8. END
Flowchart:

START

Declare variables

Take values

basic_salary = basic_salary * rate;


gross_salary = basic_salary + da + hra;
da = basic_salary * 1.15;
hra = basic_salary * 0.1;

END

Program:
#include <iostream>
using namespace std;
int main()
{
float basic_salary, rate, da, hra, gross_salary;
cout << "Enter Employee Salary: ";
cin >> basic_salary;
cout << "Enter Increase Rate: ";
cin >> rate;
basic_salary = basic_salary + (basic_salary * rate);
da = basic_salary*1.15;
hra = basic_salary*0.1;
gross_salary = basic_salary + da + hra;
cout << "The gross salary of the employee is = " << gross_salary <<
endl;
return 0;
}
Output:
Conclusion: Programs have been executed successfully.

You might also like