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

C++ Programs - CSlab-3

The document provides 10 C++ programs with their code and sample outputs. The programs cover topics like solving quadratic equations, checking if a number is palindrome, finding factorial of a number, sorting an array, calculating length of a string, student record management using structures, and swapping two numbers using pointers.

Uploaded by

rEmO aNtOnY
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)
49 views

C++ Programs - CSlab-3

The document provides 10 C++ programs with their code and sample outputs. The programs cover topics like solving quadratic equations, checking if a number is palindrome, finding factorial of a number, sorting an array, calculating length of a string, student record management using structures, and swapping two numbers using pointers.

Uploaded by

rEmO aNtOnY
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/ 9

COMPUTER SCIENCE LAB

C++ PROGRAMS
----------------------------------------------------------------------------------------------------------------------------

1)Roots of a Quadratic Equation

AIM

Input the three coefficients of a quadratic equation and find the roots.

Program

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c;
float d,r1,r2;
cout<<”Enter the three coefficients”;
cin>>a>>b>>c;
if(a==0)
{
cout<<”Not a quadratic equation “;
}
d=(b*b) -4*a*c;
if(d==0)
{
r1=(-b)/(2*a);
cout<<”roots are real and equal and root is “ <<r;
}
else if(d>0)
{
r1=(-b +sqrt(d))/(2*a);
r2=(-b -sqrt(d))/(2*a);
cout<<”roots are real and unequal”<<”\n”;
cout<<”root 1= “<<r1<<”\t root 2= “<<r2;
}
else
cout<<”Roots are complex and imaginary \n”;
return 0;
}

Sample output

Enter the coefficients

2 -5 3
Roots are real and unequal

root 1 = 3/2 root 2 = 1

2)Display a word corresponding to a digit

AIM

Input a digit and display the same in word. (Zero for 0, One for 1, ...., Nine for 9)

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<”Enter a number between 0 to 9”;
cin>>n;
switch(n)
{
case 0: cout<<"Zero";
break;
case 1: cout<<"One";
break;
case 2: cout<<"Two";
break;
case 3: cout<<"Three";
break;
case 4: cout<<"Four";
break;
case 5: cout<<"Five";
break;
case 6: cout<<"Six";
break;
case 7: cout<<"Seven";
break;
case 8: cout<<"Eight";
break;
case 9: cout<<"Nine";
break;
default: cout<<"Invalid input! ";
}
return 0;
}
Sample output

Enter a number between 0 to9


5
Five
3)Sum of n natural numbers

AIM

Display the sum of squares of n natural numbers without using formula

#include<iostream>
using namespace std;
int main()
{
int n,i,sum=0;
cout<<"Enter the value for n";
cin>>n;
for(i=1;i<=n;i++)
sum=sum+ i*i;
cout<<"Sum of squares of First "<<n<<" Natural numbers = "<<sum;
return 0;
}

Sample output

Enter the value for n


5
Sum of squares of First 5 natural numbers = 55

4)Palindrome

Aim

Input an integer number and check whether it is a palindrome or not

Program

#include<iostream>
using namespace std;
int main()
{
int num,rev=0,digit,copy;
cout<<"Enter the number ";
cin>>num;
copy=num;
while(num>0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(copy==rev)
cout<<"Given Number is palindrome";
else
cout<<"Given Number is not palindrome";
return 0;
}

Sample output

Enter the number

121

Given number is palindrome

5)Check integer for prime

Aim

Input a number and check whether it is prime or not.

Program

#include<iostream>
using namespace std;
int main()
{
int num,i,flag=0;
cout<<"Enter the number ";
cin>>num;
for(i=2;i<=num/2;i++)
{
if(num%i == 0)
{
flag++;
break;
}
}
if(flag==0)
{
cout<<”Prime number\n”;
}
else
{
cout<<”Not a prime number\n”;
}
return 0;
}
Sample output

Enter the number

29

Prime number

6)Sorting of array

Aim

Create an array to store the heights of n students and sort the values

#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter the size of array: ";
cin>>n;
int a[n];
cout<<"\nEnter the elements: ";
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<"\nThe sorted array is ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
return 0;
}
Sample output

Enter the size of the array


5
Enter the elements
123
100
145
132
160
The sorted array is
100
123
132
145
160

7)Length of a string

Aim

Display the length of a string without using strlen() function.

#include <iostream>
using namespace std;
int main()
{
char str[30];
int i,length=0;
cout<<"Enter the string:";
gets(str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
cout<<"Length of the string is:"<<length<<endl;
return 0;
}

Sample output

Enter the string

Malayalam

Length of the string is : 9

8)nCr using user defined function

Aim

Define a function to find the factorial of a number. Using this function find the value of nCr

Program
#include<iostream>
using namespace std;
int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}

int main()
{
int n,r,nCr;
cout<<"Enter the value of n and r for finding nCr";
cin>>n>>r;
if(n>=r && r>=0)
{
nCr = fact(n)/( fact(r) * fact (n-r));
}
cout<<"nCr = "<<nCr;
return 0;
}

Sample output

Enter the value of n and r for finding nCr

53
nCr = 10

9)Student record management using structure

Aim

Create a structure to represent admission number, name and marks given for CE, PE of a subject. Input
the details of student and display admission number, name and total marks obtained.

Program

#include <iostream>
using namespace std;
struct student
{
char name[50];
int adm_no;
float ce,pe,te;
};
int main()
{
student s;
cout << "\nEnter name of the student: ";
cin >> s.name;
cout << "\nEnter admission number: ";
cin >> s.adm_no;
cout << "\nEnter CE,PE marks: ";
cin >> s.ce>>s.pe;
s.te=s.ce+s.pe;
cout << "\nDisplaying details of the student" ;
cout << "\nName: " << s.name;
cout << "\nAdmission number:: " << s.adm_no;
cout << "\nCE marks: " << s.ce;
cout << "\nPE marks: " << s.pe;
cout << "\nTotal marks: " << s.te;
return 0;
}
Sample output

Enter the name of the student


Sandra

Enter the admission number


1001

Enter CE,PE marks


18 54

Displaying details of the student

Name: Sandra

Admission number : 1001

CE mark : 18

PE mark : 54

Total marks :72

10)Swapping of two numbers

Aim

Input two numbers swap them by defining a function with pointer arguments.

#include<iostream>
using namespace std;
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int num1,num2;
cout<<"Enter the numbers ";
cin>>num1>>num2;
cout<<"Before Swapping: num1 = "<<num1<<" \t num2 ="<<num2;
swap(num1,num2);
cout<<"\nAfter Swapping : num1= "<<num1<<"\t num2 ="<<num2;
return 0;
}

Sample output

Enter the numbers

15 23

Before Swapping: num1 =15 num2=23


After Swapping: num1 =23 num2=15

You might also like