C & C++ Sem 2 Practical Solutions
C & C++ Sem 2 Practical Solutions
com
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
OUTPUT:
Enter an operator (+, -, *,): *
Enter two operands: 1.5, 4.5
1.5 * 4.5 = 8.1
-4
5
8
9
11
43
485
Enter search element 11
11 found at location 5
Source Code:
#include<stdio.h>
int fib(int);
int main()
{
int n, i = 0, c;
printf("Enter number of Terms");
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for (c = 1; c <= n; c++)
{
printf("%d\t", fib(i));
i++;
}
return 0;
}
int fib(int n)
{
if (n == 0 || n == 1)
return n;
else
Downloaded from: www.sucomputersforum.com
Source Code:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT:
Enter a positive integer: 6
Factorial of 6 = 720
OUTPUT:
Enter the first string
sucomputers
Enter the second string
forum
String obtained on concatenation is sucomputersforum
12. Write a program to create a student structure containing fields for roll no.,
name, class, total marks
Source Code:
#include <stdio.h>
#include<conio.h>
struct student
{
char firstName[50];
int roll;
char class[10];
int marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
for (i = 0; i < 3; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter Class");
scanf("%s",&s[i].class);
printf("Enter marks: ");
scanf("%d", &s[i].marks);
}
printf("Displaying Information:\n\n");
for (i = 0; i < 3; ++i) {
printf("\nRoll number: %d\t", i + 1);
printf("First name: ");
printf("%s\t",s[i].firstName);
printf("Class is :");
printf("%s\t",s[i].class);
printf("Marks: %.1d", s[i].marks);
printf("\n");
}
return 0;
}
OUTPUT:
Enter information of students:
For roll number1,
Enter first name: waseem
Enter Class12
Enter marks: 56
13. Write a program to create array of structure containing fields for empid,
name, and salary
Source Code:
#include <iostream>
using namespace std;
struct employee {
string ename;
int eid, phn_no;
int salary;
};
void display(struct employee emp[], int n)
{
cout << "Name\tEid\tPhone Number\tSalary\n";
for (int i = 0; i < n; i++) {
cout << emp[i].ename << "\t" << emp[i].eid << "\t"
<< emp[i].phn_no << "\t" << emp[i].salary << "\n";
}
}
int main()
{
int n = 3;
struct employee emp[n];
emp[0].ename = "Kalyan";
emp[0].eid = 24;
emp[0].phn_no = 1234567788;
emp[0].salary = 20000;
emp[1].ename = "Krishna";
emp[1].eid = 31;
emp[1].phn_no = 1234567891;
Downloaded from: www.sucomputersforum.com
emp[1].salary = 56000;
emp[2].ename = "Rajkumar";
emp[2].eid = 45;
emp[2].phn_no = 1100661111;
emp[2].salary = 30500;
display(emp, n);
return 0;
}
OUTPUT :
Name Eid Phone Number Salary
Kalyan 24 1234567788 20000
Krishna 31 1234567891 56000
Rajkumar 45 8881101111 30500
14. Write a program to read student name, rollno, marks and display the same
using classes and objects
Source Code:
#include <iostream>
using namespace std;
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
void getDetails(void);
void putDetails(void);
};
void student::getDetails(void)
{
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
perc=(float)total/500*100;
}
void student::putDetails(void)
{
cout << "Student details:\n";
cout << "Name:"<< name << "\t Roll Number:" << rollNo << "\tTotal:" << total <<
"\tPercentage:" << perc;
}
int main()
{
student std;
std.getDetails();
std.putDetails();
return 0;
}
OUTPUT:
Enter name: Waseem
Enter roll number: 25
Enter total marks outof 500: 460
Student details:
Name:Waseem Roll Number:25 Total:460 Percentage:92
15. Write a program to read employee name, empid, salary and display the
same using classes and objects
#include "iostream"
using namespace std;
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData ()
{
std::cout<< "\n\tEnter Employee Id : ";
std::cin >> Id;
std::cout << "\n\tEnter Employee Name : ";
std::cin >> Name;
std::cout << "\n\tEnter Employee Age : ";
std::cin >> Age;
std::cout << "\n\tEnter Employee Salary : ";
std::cin >> Salary;
}
void PutData ()
{
std::cout << "\n\nEmployee Id : " << Id;
std::cout << "\nEmployee Name : " << Name;
std::cout << "\nEmployee Age : " << Age;
std::cout << "\nEmployee Salary : " << Salary;
}
};
int main ()
{
Employee E;
Downloaded from: www.sucomputersforum.com
E.GetData ();
E.PutData ();
return 0;
}
OUTPUT:
Enter Employee Id : 101
Enter Employee Name : waseem
Enter Employee Age : 33
Enter Employee Salary : 25000
Employee Id : 101
Employee Name : waseem
Employee Age : 33
Employee Salary : 25000