Sei sulla pagina 1di 18

PRACTICAL FILE

FUNDAMENTALS OF PROGRAMMING LANGUAGE

SUBMITTED BY:RANJANA SINGH 11/HEL/2253 B.SC.[h] ELECTRONICS ,2nd Yr.,3rd sem.

1. WAP to print the sum and product of digits of an integer. #include<iostream> using namespace std; int main() { int n; int digit,sum=0,temp,product=1; cout<<"\n Enter your number : "; cin>>n; temp=n; while(temp>0) { digit=temp%10; temp=temp/10; sum=sum+digit; product=product*digit; } cout<<"\n Sum of the digits is : "<<sum<<endl; cout<<"\n Product of the digits is : "<<product<<endl; system("pause"); return 0; }

2. WAP to reverse a number. #include<iostream> using namespace std;

int main() { int d,m,rev=0,temp; cout<<"\n ENTER YOUR NUMBER : "; cin>>m; temp=m; while(temp>0) { d=temp%10; temp=temp/10; rev=(rev*10)+d; } cout<<"\n ACTUAL NUMBER IS : "<<m<<endl; cout<<"\n REVERSED NUMBER IS: "<<rev<<endl; system("pause"); return 0; } 3. WAP to compute the n terms of the following series S = 1 + 1/2 + 1/3 + 1/4 + ... #include<iostream> using namespace std; int main()

{ int n; float i=1; float sum=0; cout<<"\n enter the no. of ur choice: "; cin>>n; while(i<=n) { sum=sum+1/i; i=i+1; } cout<<"\n hence the required sum of hp is: "<<sum<<endl; system("pause"); return 0; } 4. WAP to compute the n terms of the following series S = 1 - 2 + 3 - 4 + ... #include<iostream> using namespace std; int main()

{ int n,i=1,sum=0; cout<<"\n ENTER THE NUMBER: "; cin>>n; while(i<=n) { if(i%2!=0) { sum=sum+i; } else { sum=sum-i; } i=i+1; } cout<<"\n REQUIRED SUM IS : "<<sum<<endl; system("pause"); return 0; 5.Write a function to find whether a given no. is Prime or not. #include<iostream> using namespace std; bool checkprime(int n)

{ int i=1,factcount=0; while(i<=n) { if (n%i==0) factcount++; i++; } if(factcount==2) return true; else return false; } int main() { int n; cout<<"\n Enter the number: "; cin>>n; if(checkprime(n)==true) cout<< "\n Given no. is prime"<<n<<endl; else cout<<"\n Given no. is not prime"<<endl;

system("pause"); return 0; } 6. WAP to compute the factors of a given number. #include<iostream> using namespace std; void factor(int n) { int i=1; while(i<=n) { if (n%i==0) cout<<"\n FACTOR : "<<i<<endl; i++; } } int main() { int n;

cout<<"\n ENTER YOUR NUMBER : "; cin>>n; factor(n); system("pause"); return 0; } 7. WAP to print the triangle of stars as follows (take number of lines from user) * *** ***** ******* #include<iostream> using namespace std; int main() { int n; cout<<"Enter n: "; cin>>n; for(int i=n;i>0;i--) { for(int j=1;j<=i;j++) cout<<"*"; cout<<endl;

} system("pause"); return 0; }
8. WAP to perform following actions on an array entered by the user: a) Print the even-valued elements. b) Print the odd-valued elements. c) Calculate and print the sum and average of the elements of user. d) Print the maximum and minimum elements of array. e) Remove the duplicates from the array. f) Print the array in reverse order. #include<iostream> using namespace std; int main() { int n,choice,sum=0,max=0; float avg; cout<<"ENTER THE NUMBER OF ELEMENTS IN YOUR ARRAY : "; cin>>n; cout<<"ENTER THE ELEMENTS NOW:"; for (int i=0;i<n;i++) cin>>a[n];

cout<<" MENU " ; cout<<"1. PRINT EVEN ELEMENTS"<<endl; cout<<"2. PRINT ODD ELEMENTS "<<endl; cout<<" 3.CALCULATE SUM AND AVERAGE "<<endl; cout<<"4. MAX AND MIN ELEMENT"<<endl; cout<<"5. REMOVE DUPLICATES FROM THE ARRAY. "<<endl; cout<<"6. PRINT ARRAY IN REVERSR ORDER."<<endl; cout<<"enter your choice: "; cin>>choice; if (choice<1||choice>6) cout<<"PLEASE ENTER A VALID CHOICE. "<<endl; else switch(choice) { case 1. cout<<"Even- valued elements of array : "; for(int i=0;i<n;i++) { if(a[i]%2==0) cout<<a[i]; } break; case 2. cout<<"Odd-valued elements of array : "; for(int i=0;i<n;i++) { if(a[i]%2!=0) cout<<a[i];

} break; case 3.cout<<"Calculate sum and average : "; for(int i=0;i<n;i++) { sum=sum+a[i]; avg=(float)sum/n; } cout<<"Sum : "<<sum<<endl; cout<<"Average : "<<avg<<endl; break; case 4.cout<<"Max and min : "; for(int i=0;i<=n;i++) { if(a[i]>max) max=a[i]; } cout<<"Max no. is: " <<max<<endl; min=max; for(int i=0;i<=n;i++) { if(a[i]<min) min=a[i]; } cout<<"Min no. is: "<<min<<endl; break; case 5.cout<<"Remove duplicates : ";

for(int i=0;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(a[i]==a[j]) a[j]=0; } } for(int i=0;i<=n;i++) { cout<<a[i]<<endl; } break; case 6.cout<<"Print array in reverse order: "; for( i=0,j=n;i<j;i++,j--) { temp=a[i]; a[i]=a[j]; a[j]=temp; } for(i=0;i<=8;i++) { cout<< a[i] << endl; } break; } cout<<"DO YOU WANT TO CONTINUE ? ('Y'/'y') ";

cin>>chh; if(chh=='y'||chh=='Y') continue; else break; system("pause") return 0; }

14. WAP that swaps two numbers using pointers. #include<iostream> using namespace std; int main() { int a,b; int c=&a; int d=&b; int temp; cout<<"CONTENT OF FIRST POINTER: "<<*c<<endl; cout<<"CONTENT OF SECOND POINTER : "<<*d<<endl; temp=*c; *c=*d; *d=temp; cout<<"AFTER SWAPING CONTENT OF FIRST POINTER : "<<*c<<endl;

cout<<" NOW CONTENT OF SECOND POINTER :: "<<*d<<endl; system("pause") return 0; }


10. Write a menu driven program to perform following operations on strings: a) Show address of each character in string. .b) Calculate the length of the string (use pointers). c) Convert all lowercase characters to uppercase. d) Convert all uppercase characters to lowercase. e) Calculate the number of vowels.

#include<iostream> using namespace std; int main() { int count,length=0,str; cout<<" ENTER YOUR STRING : "<<endl; cin>>str; cout<<"...........MENU............."<<endl; cout<<"1.Length of the string"<<endl; cout<<"2.Address of each character"<<endl; cout<<"3.change string from lower to upper case"<<endl;

cout<<"4.change string from upper to lower case"<<endl; cout<<"5.no. of vowels in the string "<<endl; cout<<"ENTER YOuR CHOICE: "<<endl; cin>>choice; if (choice<1||choice>5) cout<<"ENTER A VALID CHOICE"<<endl; else switch(choice) { case 1: cout<<"length of the string: "<<endl; { if(str[i]!='/0') { length++; } else cout<<"LENGTH OF THE STRING: "<<length<<endl; } break; case 2:cout<<"address of each character:"<<endl; {

char*q=str; for(int i=0;i<=length-1;i++;q++) cout<<"ADDRESS OF EACH CHARACTER : "<<q<<endl; } break; case 3:cout<<"change to upper case :"<<endl; { for(int i=0;i<length;i++) if(str[i]>='a'&&str[i]<='A') { int temp=('a'-'A'); str[i]=str[i]-temp; } cout<<"STRING IN UPPER CASE : "<<str[i]<<endl; } break; case 4:cout<<"change to lower case :"<<endl; { for(int i=0;i<length;i++) if(str[i]>='a'&&str[i]<='A') {

int temp=('a'-'A'); str[i]=str[i]+temp; } cout<<"STRING IN LOWER CASE : "<<str[i]<<endl; } break; case 5:cout<<"no. of vowels: "<<endl; { for(int i=0;i<length;i++) if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i] =='u') count++; else cout<<"NO. OF VOWELS : "<<count<<endl; } break; } cout<<"DO YOU WANT TO CONTINUE ? ('Y'/'y') "; cin>>chh; if(chh=='y'||chh=='Y') continue; else

break; system("pause") return 0; }

Potrebbero piacerti anche