Anda di halaman 1dari 15

# COMPUTER PRACTICAL FILE

## 1. WAP to print Fibonacci series i.e. 0 1 1 2 3 5 8.

#include<iostream.h> #include<conio.h> void main( ) { clrscr( ) ; unsigned long first, second, third, n ; first = 0 ; second = 1 ; cout << How many elements(> 5) ? \n ; cin >> n ; cout << Fibonacci series\n ; cout << first << << second ; for(int i = 2 ; i < n ; ++i) { third = first + second ; cout << << third ; first = second ; second = third ; } }

INPUT: 7 OUTPUT: 0 1 1 2 3 5 8

2. WAP to print largest even and largest odd number from a list of numbers entered trough
keyboard. The list terminates as soon as one entered 0 (zero). #include<iostream.h> #include<conio.h> void main ( ) { clrscr( ) ; int num, leven = o, lodd = 1 ; cout << Enter numbers (0 to terminate) \n ; do { cin >> num ; if(num % 2 ==0) { if(num > leven) leven = num ; } else if(num > lodd) lodd = num ; } while(num) ; cout << \n Largest even number = << leven ; cout << \n Largest odd number = << lodd ; }

OUTPUT:

i. ii.

## 3. WAP that checks whether the given character is an alphanumeric or a digit.

#include<iostream.h> #include<ctype.h> #include<stdio.h> void main( ) { char ch ; int a ; cout << \nEnter a character :\n ; cin >> ch ; a = ch ; if(isalnum(a)) { cout << \nAlphanumeric character\n if (isdigit(a)) cout << \nAnd Digit character\n ; else if (isa1pha(a)) cout << \nAnd Alphabetic character\n ; } } else cout << \nOther character \n ;

## INPUT: i. ii. iii. ABC 123 %&#

OUTPUT: i. ii. iii. Alphanumeric Character And Alphabetic Character Alphanumeric Character And Digit Character Other Character

4. WAP that checks whether a given character is an alphabet, whether it is lower case or upper case.
#include<iostream.h> #include<conio.h> #include<ctype.h> void main ( ) { clrscr ( ) char ch ; cout << \nEnter a character \n ; cin >> ch ; if(isalpha(ch)) { if(islower(ch)) cout << \nLowercase alphabet\n ; else if (isupper (ch)) cout << \nUppercase alphabet\n ; } else cout << \nNot an alphabet \n ; }

## 5. WAP that reads a string and converts it to upper case.

#include<iostream.h> #include<ctype.h> #include<string.h> void main ( ) { char str[25] ; int flag = 1 ; cout << \nEnter a string(max 25 characters) \n ; cin.getline(str, 25) ; for(int i = 0 ; str[i] ! = \0 ; i++ ) { if(isupper(str[i])) { } else if(islower(str[i]) ) { flag = 1; str[i] = toupper(str[i]); } } if( (flag==1) | | (str[i]==\0) ) { cout<<Uppercase string is: \n ; int x1 = strlen(str); cout.write(str,x1); } } flag = 0 ; cout << str[i] << is already in uppercase\n ;

## OUTPUT: i. ii. Uppercase string is: ABC ABC is already in uppercase

6. WAP that invokes a function cal( ) which takes two integers and an arithmetic operator and prints out the corresponding result.
#include<iostream.h> #include<conio.h> #include<process.h> void main( ) { clrscr( ) ; void calc(int, int, char) ; int a, b ; char ch ; cout << Enter two integers : \n ; cin >> a >> b ; cout << \nEnter arithmetic operator( +, _, *, / , %) : \n ; cin >> ch ; calc(a, b, ch) ; } void calc(int x, int y, char c) { { case + : cout << \nSum of << x << and << y << is << (x + y) ; break ; case - : cout << \nDifference of << x << and << y << is << (x y); break ; case * : cout << \nProduct of << x << and << y << is << (x * y) ; break ; case / : if (x < y) { cout << \nFirst integer should be << greater than second.; exit(0) ; } cout << \nQuotient of : << x << / << y << is << (x / y) ; break ; case % : if(x <y) { cout << \nFirst integer should be << greater than second. ; switch(c)

exit(0) ; } cout << \nRemainder of : << x << % << y << is << (x % y) ; break ; default : cout << \nWrong operator ! ! ! ; break ; } }

INPUT: i. ii. iii. iv. v. 25 and 23 59 and 25 25 and 23 625 and 25 56 and 26

## OUTPUT: i. ii. iii. iv. v.

Sum of 25 and 23 is 48 Difference of 59 and 25 is 34 Product of 25 and 23 is 575

## 7. WAP that count number of spaces present in a line.

#include<iostream.h> #include<stdio.h> #include<conio.h> void main ( ) { clrscr( ); char str[80]; int i,count=0; cout<<"\nEnter any string (max. 80):"; gets(str); for(i=0;str[i]!='\0';++i) { if (str[i] ==' ') { count++ ; while(str[i]==' ') i++; } if(str[i]=='\0') i--; } cout<<"\nNumber of spaces in a string are:"<<count; getch( ); }

INPUT: i. ii.
Enter any string (max. 80): My name is Deepak Nagar. Enter any string (max. 80): Hello!!! Welcome to Facebook.

OUTPUT: i. ii.
Number of spaces in a string are: 4 Number of spaces in a string are: 3

## 8. WAP to transpose a matrix.

#include<iostream.h> #include<conio.h> void main ( ) { clrscr( ) ; int a [20][20], b[20][20], i , j , m, n ; cout << \nInput row & column of matrix-A \n ; cin >> m >> n ; cout << \nInput matrix-A \n ; for (i = 0 : i < m ; ++i ) { } { for(j = 0 : j < n ; ++j ) cin >> a [i][j] ; cout << \nMATRIX-A : : for(i = 0 ; i < m ; ++i ) cout << \n ; for(j = 0 : j < m : ++j ) cout << << a[i][j] ; } for(i = 0 ; i < n ; ++i ) { } cout << \nTranspose of matrix is : \n ; for(i = 0 ; i < n ; ++i ) { for(j = 0 ; j < m ; ++j ) cout << << b [i][j] ; cout << \n ; } } for(j = 0 ; j < m ; ++j ) b[i][j] = a[j][i] ;

INPUT: i. ii.
Input row & column of matrix-A: 2 and 1 Input matrix-A: 11 and 1 Input row & column of matrix-A:3 and 2 Input matrix-A: 10, 2, 1, 3, 2 and 1

## OUTPUT: i. ii. Matrix-A: 110 and 10

Transpose of matrix is : 11

## Matrix-A: 1020, 130, 1218 and 224

Transpose of matrix is : 1012 and 231

9. WAP to find row sum and column sum of a matrix. #include<iostream.h> void main( ) { int arr[ ][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} ; int rsum,csum,j ; for(int i=0; i<3; i++) { rsum=0; for(int j=0; j<5; j++) rsum+=arr[i][j] ; cout<<sum of row<<i+1<<is<<rsum<<endl; } for(i=0; i<5; i++) { csum=0; for(j=0; j<3; j++) csum+=arr[j] [i]; cout<<sum of col<<i+1<<is<<csum<<endl;
} }

INPUT: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 OUTPUT: i. ii. iii. iv. v. vi. vii. viii. sum of row1 is 15 sum of row2 is 40 sum of row3 is 65 sum of col1 is 18 sum of col2 is 21 sum of col3 is 24 sum of col4 is 27 sum of col5 is 30

10. WAP to print largest and smallest element of an array using a function.
#include<iostrem.h> #include<conio.h> int Large void main ( ) { clrscr( ) ; int v[50], large, small ; int i , n ; cout << Enter how many elements (max 50) ?\n ; cin >> n ; cout << Enter values in the vector \n ; for(i = 0 ; i < n ; i++ ) cin >> v [i] ; large = v[i] ; small = v[i] ; for(i = 0 ; i < n ; i++ ) { if(v[i] > large) large = v[i] ; else if (v[i] < small) small = v[i] ; }cout << \nLargest element = << large ; cout << \nSmallest element = << small ; }

INPUT: i. ii.
Enter how many elements (max 50)? : 5 Enter values in the vector: 12, 22, 25, 11 and 10 Enter how many elements (max 50)? : 8 Enter values in the vector: 12, 413, 55, 64, 211, 35, 1112, 225

OUTPUT: i.
ii. Largest element = 25 Smallest element = 10 Largest element = 1112 Smallest element = 12

## 11. WAP to reverse a string.

#include<iostream.h> #include<studio.h> #include<conio.h> void main ( ) { clrscr( ) ; char str[80] ; int i , j ; cout << \nEnter any string (max. 80) : ; gets(str) ; for(i = 0 ; str[i] ! = \0 ; ++i ) ; char rev [80] ; int k ; for(j = i 1, k = 0 ; j >= 0 ; --j, ++k ) rev[ k ] = str[j] ; rev[ k ] = \0 ; cout << \nString in reverse order is : ; puts(rev) ; getch ( ) ; }

INPUT: i. ii. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 6, 1, 6, 4, 4, 0, 5

OUTPUT: i. ii. String in reverse order is: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 String in reverse order is: 5, 0, 4, 4, 6, 0, 1, 6

## 12. WAP to calculate length of a string.

#include<iostream.h> #include<studio.h> #include<conio.h> void main ( ) { clrscr( ) ; char str[80] ; int i ; cout << \nEnter any string (max. 80) : ; gets(str) ; for(i =0 ; str[i] ! = \0 ; ++i ) ; cout << \n Length of the string is : << i ; getch( ) ; }

## INPUT: i. ii. 12434325656325 ABCDEFGHIJKLMNOPQRST

OUTPUT: i. ii. 14 20