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

C & C++ Sem 2 Practical Solutions

This document provides 11 programming problems and their solutions in C/C++. The problems cover topics like finding the greatest of three numbers, checking if a number is a palindrome, printing prime numbers, a menu driven calculator, sorting an array with bubble sort, binary search, matrix multiplication, Fibonacci series, recursion, call by value vs call by reference, and string concatenation. For each problem, the source code and sample output are given. The document appears to be from a university providing practical programming assignments for students.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

C & C++ Sem 2 Practical Solutions

This document provides 11 programming problems and their solutions in C/C++. The problems cover topics like finding the greatest of three numbers, checking if a number is a palindrome, printing prime numbers, a menu driven calculator, sorting an array with bubble sort, binary search, matrix multiplication, Fibonacci series, recursion, call by value vs call by reference, and string concatenation. For each problem, the source code and sample output are given. The document appears to be from a university providing practical programming assignments for students.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Downloaded from: www.sucomputersforum.

com

Faculty of Computer Applications


SATAVAHANA UNIVERSITY, KARIM NAGAR
B.Com Comp Applications (CBCS-R-19 ) Semester-II
Programming with C & C++
Practical Solutions w.e.f 2019-2020
Max Marks: 20] [3 Hours /Week

1. Write a program- to find the greatest of three numbers using ternary


operator
Source Code:
# include <stdio.h>
void main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}
OUTPUT:
Enter three numbers:10 25 12
The biggest number is: 25
2. Write a program to check whether given number is palindrome or not
Source Code:
#include <stdio.h>
int main()
{ int n, rev = 0, rem, t;
printf("Enter an integer: ");
scanf("%d", &n);
t = n;
while (n != 0)
{ rem = n % 10;
rev= rev* 10 + rem;
n =n/ 10; }
if (t== rev)
printf("%d is a palindrome.", t);
else
printf("%d is not a palindrome.", t);
return 0;
}
OUTPUT:
Enter an integer: 12321
12321 is a palindrome.
3. Write a program to print the prime numbers from 2 to n where n is given
by user
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number till which you want prime numbers\n");
scanf("%d",&n);
printf("Prime numbers are:-\n");
for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d ",i);
}
}
}
OUTPUT:
Enter the number till which you want prime numbers25
Prime numbers are:-
2 3 5 7 11 13 17 21 23

4. Create menu driven application using switch to find addition, subtraction,


multiplication and division of two numbers
Source Code:
#include <stdio.h>
int main()
{
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator)
{
Downloaded from: www.sucomputersforum.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

5. Write a program to sort the elements of an Array using bubble sort


technique
Source Code:
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array:
11 12 22 25 34 64 90

6. Write a program to search an element in an array using binary search


method
Source Code:
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter search element\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
OUTPUT:
Enter number of elements
7
Enter 7 integers
Downloaded from: www.sucomputersforum.com

-4
5
8
9
11
43
485
Enter search element 11
11 found at location 5

7. Write a program to find the product of two matrices


Source Code:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
OUTPUT:
Enter number of rows and columns of first matrix
3
3
Enter elements of first matrix
120
011
201
Enter number of rows and columns of second matrix
3
3
Enter elements of second matrix
112
211
121
Product of the matrices
5 3 4
3 3 2
3 4 5

8. Write a program to print Fibonacci numbers using function (0 1 1 2 3 5


8…)

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

return (fib(n-1) + fib(n-2));


}
OUTPUT:
Enter number of Terms10
Fibonacci series terms are:
0 1 1 2 3 5 8 13 21 34

9. Write a program to find the factorial of a given number using recursion

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

10. Write a program to demonstrate call by value & call by reference


Source Code: Call By Value
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
tempnum = var1 ;
var1 = var2 ;
var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);
swapnum(num1, num2);
printf("\nAfter swapping: %d, %d", num1, num2);
}
OUTPUT:
Before swapping: 35, 45
After swapping: 35, 45

Source Code: Call By Reference


#include<stdio.h>
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ;
*var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
swapnum( &num1, &num2 );
printf("\nAfter swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
return 0;
}
OUTPUT:
Before swapping:
num1 value is 35
num2 value is 45
After swapping:
num1 value is 45
num2 value is 35

11. Write a program to concatenate two strings using string functions


Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
return 0;
}
Downloaded from: www.sucomputersforum.com

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

For roll number2,


Enter first name: Raa za
Enter Class98 14
Enter marks: 98

For roll number3,


Enter first name: Ra Raju
Enter Class15
Enter marks: 88
Displaying Information:
Roll number: 1 First name: waseem Class is :12 Marks: 56
Roll number: 2 First name: Raza Class is :14 Marks: 98
Roll number: 3 First name: Raju Class is :15 Marks: 88

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

You might also like