0% found this document useful (0 votes)
2 views22 pages

C++ Programming: Quadratic Roots & More

+2 computer science c++ program
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)
2 views22 pages

C++ Programming: Quadratic Roots & More

+2 computer science c++ program
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

Join Now: [Link] Downloaded from [Link]

in ®

Programming in C++
1
1.1 Roots of quadratic equation

PROBLEM

Input the three coe!cient of quadratic equation and find the roots

CODE

1 # include < iostream >


2 # include < cmath >
3 using namespace std ;
4 int main () {
5 double a , b , c ;
6 double root1 , root2 , realPart , imgPart ;
7 cout <<" Enter the coefficients of the quadratic equation
(a , b , c ) : " ;
8 cin >> a >> b >> c ;
9 double disc = b * b - 4 * a * c ;
10 if ( disc > 0) {
11 root1 = ( - b + sqrt ( disc ) ) / (2 * a ) ;
12 root2 = ( - b - sqrt ( disc ) ) / (2 * a ) ;
13 cout << " Real and Imaginary roots \ n " ;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

2 Programming in C++

14 cout << " Root 1: " << root1 <<" \ n " ;


15 cout << " Root 2: " << root2 <<" \ n " ;
16 } else if ( disc == 0) {
17 root1 = -b / (2 * a ) ;
18 cout << " Two Roots are equal .\ n " ;
19 cout << " Root : " << root1 ;
20 } else {
21 realPart = -b / (2 * a ) ;
22 imgPart = sqrt ( - disc ) / (2 * a ) ;
23 cout < < " Complex Roots \ n " ;
24 cout << " Root 1: " << realPart << " + " << imgPart << "
i\n";
25 cout << " Root 2: " << realPart << " - " << imgPart << "
i\n";
26 }
27 return 0;
28 }

Listing 1.1: [Link]

OUTPUT

Figure 1.1: Roots of quadratic equation

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.2 Display word corresponding to a digit 3

1.2 Display word corresponding to a digit

PROBLEM

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

CODE

1 # include < iostream >


2 using namespace std ;
3 int main () {
4 int num ;
5 cout < < " Input the digit to display in words : " ;
6 cin > > num ;
7 switch ( num )
8 {
9 case 0:
10 cout < < " Zero " ; break ;
11 case 1:
12 cout < < " One " ; break ;
13 case 2:
14 cout < < " Two " ; break ;
15 case 3:
16 cout < < " Three " ; break ;
17 case 4:
18 cout < < " Four " ; break ;
19 case 5:
20 cout < < " Five " ; break ;
21 case 6:
22 cout < < " Six " ; break ;
23 case 7:

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

4 Programming in C++

24 cout < < " Seven " ; break ;


25 case 8:
26 cout < < " Eight " ; break ;
27 case 9:
28 cout < < " Nine " ; break ;
29 default :
30 cout < < " Invalid input " ;
31 }
32 return 0;
33 }

Listing 1.2: [Link]

OUTPUT

Figure 1.2: Display word corresponding to a digit

1.3 Sum of n natural numbers

PROBLEM

Find the sum of n natural numbers without using a formula

CODE

1 # include < iostream >


2 using namespace std ;
3 int main () {

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.4 Check integer for palindrome 5

4 int n ;
5 cout < < " Enter a positive integer ( n ) : " ;
6 cin >> n ;
7 int sum = 0;
8 for ( int i = 1; i <= n ; i ++) {
9 sum += i ;
10 }
11 cout <<" The sum of the first " <<n < < " natural numbers is
: " << sum ;
12 return 0;
13 }

Listing 1.3: [Link]

OUTPUT

Figure 1.3: Sum of n natural numbers

1.4 Check integer for palindrome

PROBLEM

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


not

CODE

1 # include < iostream >

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

6 Programming in C++

2 using namespace std ;


3 int main ()
4 {
5 int n , num , digit , rev = 0;
6 cout << " Enter a positive number : " ;
7 cin >> num ;
8 n = num ;
9 while ( num > 0)
10 {
11 digit = num % 10;
12 rev = ( rev * 10) + digit ;
13 num = num / 10;
14 }
15 cout << " The reverse of the number is : " << rev ;
16 if ( n == rev )
17 cout << " \ n The number is a palindrome . " ;
18 else
19 cout << " \ n The number is not a palindrome . " ;
20 return 0;
21 }

Listing 1.4: [Link]

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.5 Check integer for prime 7

Figure 1.4: Check if the given integer is Palindrome or not

OUTPUT

1.5 Check integer for prime

PROBLEM

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

CODE

1 # include < iostream >


2 using namespace std ;
3 int main () {
4 int num ;
5 bool isPrime = true ;
6 cout <<" Provide a positive number : " ;
7 cin >> num ;
8 if ( num <=1)
9 isPrime = false ;
10 else {
11 for ( int i = 2; i <= num /2; i ++) {
12 if ( num % i == 0) {
13 isPrime = false ;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

8 Programming in C++

14 }
15 }}
16 if ( isPrime )
17 cout < < " Number is prime " ;
18 else
19 cout < < " Number is not prime " ;
20 return 0;
21 }

Listing 1.5: [Link]

OUTPUT

Figure 1.5: Check if the given integer is Prime or not

1.6 Sorting of array

PROBLEM

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

CODE

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.6 Sorting of array 9

1 # include < iostream >


2 using namespace std ;
3 int main () {
4 int heights [20] , i , j ,n , temp ;
5 cout < < " Enter the number of students :\ n " ;
6 cin > > n ;
7 cout < < " Enter the heights of students :\ n " ;
8 for ( i = 0; i < n ; i ++) {
9 cout << " Student " << ( i + 1) << " : " ;
10 cin >> heights [ i ];
11 }
12 for ( i =0; i < n ; i ++)
13 {
14 for ( j = i +1; j < n ; j ++) {
15 if ( heights [ i ] > heights [ j ])
16 {
17 temp = heights [ i ];
18 heights [ i ] = heights [ j ];
19 heights [ j ] = temp ;
20 }
21 }
22 }
23 cout << " The Sorted Heights \ n " ;
24 for ( i = 0; i < n ; i ++) {
25 cout << heights [ i ] < < " \ n " ;
26 }
27 return 0;
28 }

Listing 1.6: [Link]

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

10 Programming in C++

OUTPUT

Figure 1.6: Array sorting using bubble sort

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.7 Compute length of a string 11

1.7 Compute length of a string

PROBLEM

Find the length of a string without using strlen() function

CODE

1 # include < iostream >


2 # include < cstdio >
3 using namespace std ;
4 int main ()
5 {
6 char str [50];
7 int i , length =0;
8 cout < < " Enter the string : " ;
9 gets ( str ) ;
10 for ( i =0; str [ i ]!= ’ \0 ’ ;++ i )
11 length ++;
12 cout < < " Length of the string is : " << length ;
13 return 0;
14 }

Listing 1.7: [Link]

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

12 Programming in C++

Figure 1.7: Counting the length of a given string

1.8 Compute nCr using a user-defined func-

tion

PROBLEM

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


function find the value of nCr

CODE

1 # include < iostream >


2 using namespace std ;
3 long fact ( int n ) {
4 long result = 1;
5 for ( int i = 1; i <= n ; ++ i ) {
6 result = result * i ;
7 }
8 return result ;
9 }
10 int main () {

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.8 Compute nCr using a user-defined function 13

11 int n , r , ncr ;
12 cout < < " Enter the value of n and r ( positive integer ) " ;
13 cin >>n > > r ;
14 if (( n >= r ) && (r >=0) ) {
15 ncr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ;
16 cout < < " nCr is : " << ncr ;
17 }
18 else
19 cout < < " n and r must be non - negative integers and n > r .
" ;
20 return 0;
21 }

Listing 1.8: [Link]

Figure 1.8: nCr for di!erent n and r

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

14 Programming in C++

1.9 Student record management using structure

PROBLEM

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

CODE

1 # include < iostream >


2 # include < cstdio >
3 using namespace std ;
4 struct student {
5 int admNo ;
6 char name [25];
7 int ceMark ;
8 int peMark ;
9 int totalMark ;
10 };
11 int main () {
12 student s ;
13 cout < < " Enter the student details :\ n " ;
14 cout < < " \ n Admission Number : " ;
15 cin > > s . admNo ;
16 cout < < " \ n Name : " ;
17 cin > > ws ; //
18 gets ( s . name ) ;
19 cout < < " \ n CE Mark : " ;
20 cin > > s . ceMark ;
21 cout < < " \ n PE Mark : " ;
22 cin > > s . peMark ;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.9 Student record management using structure 15

23 s . totalMark = s . peMark + s . ceMark ;


24 cout < < " \ n The Student Details \ n " ;
25 cout < < " \ nName : " <<s . name ;
26 cout < < " \ nAdmission No . : " <<s . admNo ;
27 cout < < " \ nCE Mark : " <<s . ceMark ;
28 cout < < " \ nPE Mark : " <<s . peMark ;
29 cout < < " \ nTotal Mark : " <<s . totalMark ;
30 return 0;
31 }

Listing 1.9: [Link]

1 2

OUTPUT

1
cin >> ws tells the compiler to ignore bu!er and also to discard all the white spaces
before the actual content of string or character array
2
To resolve the issue where some IDEs may ignore the gets command, follow these steps.
Click on ”Build Commands” in the ”Set Build Commands” section. In the ’Compile’ box
and ’Build’ box, append the command -std=c++11.

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

16 Programming in C++

Figure 1.9: Data populated from the student structure

1.10 Integer swap operation using a user-defined

function with pointer arguments

PROBLEM

Input two numbers swap them by defining a function with pointer


arguments

CODE

1 # include < iostream >


2 using namespace std ;
3 void swap ( int *x , int * y )
4 {
5 int t ;
6 t =* x ;
7 * x =* y ;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.10 Integer swap operation using a user-defined function with


pointer arguments 17

8 *y=t;
9 }
10 int main ( )
11 {
12 int a , b ;
13 cout < < " Enter the values of a and b " ;
14 cin > >a > > b ;
15 cout < < " \ nBefore Swap \ n " ;
16 cout < < " a = " <<a < < " \ t b = " <<b ;
17 swap (& a ,& b ) ;
18 cout < < " \ nAfter Swap \ n " ;
19 cout < < " a = " <<a < < " \ t b = " <<b ;
20 }

Listing 1.10: [Link]

OUTPUT

Figure 1.10: Swapping of two numbers

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

18 Programming in C++

1.11 Check whether the given number is pos-

itive, negative, or zero

PROBLEM

Input a number and check if it is positive negative or zero

CODE

1 # include < iostream >


2 using namespace std ;
3 int main () {
4 int num ;
5 cout < < " Enter the integer to check : " ;
6 cin > > num ;
7 if ( num >0)
8 cout < < " \ n The given number is Positive " ;
9 else if ( num <0)
10 cout < < " \ n The given number is Negative " ;
11 else
12 cout < < " \ n The given number is Zero " ;
13 return 0; }

Listing 1.11: [Link]

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.12 Check whether the given number is odd or even 19

Figure 1.11: number check

1.12 Check whether the given number is odd

or even

PROBLEM

Input a number and check if it is odd or even

CODE

1 # include < iostream >


2 using namespace std ;
3 int main ()
4 {
5 int num ;
6 cout < < " Enter a number to check - odd or even : " ;
7 cin > > num ;
8 if ( num %2==0)
9 cout < < " The given number is even " ;
10 else
11 cout < < " The Given number is odd " ;
12 return 0;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

20 Programming in C++

13 }

Listing 1.12: [Link]

OUTPUT

Figure 1.12: odd even

1.13 Sum of the squares of the first N natural

numbers

PROBLEM

Find the sum of squares of integers up to a specified limit by in-


putting a number.

CODE

1 # include < iostream >


2 using namespace std ;
3 int main ()
4 {
5 int num , sum =0 , i ;
6 cout < < " Enter the limit : " ;
7 cin > > num ;
8 for ( i =1; i <= num ; i ++)
9 sum = sum + i * i ;

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

1.13 Sum of the squares of the first N natural numbers 21

10 cout < < " Sum of square : " << sum ;


11 return 0;
12 }

Listing 1.13: [Link]

OUTPUT

Figure 1.13: Sum of Squares of integer

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem


Join Now: [Link] Downloaded from [Link] ®

22 Programming in C++

Computer Science Lab Manual Prepared by Dr. Nishad Abdulkareem

You might also like