17
Computer Project

C++ Project

Embed Size (px)

Citation preview

Page 1: C++ Project

Computer Project

Page 2: C++ Project

1. Program to read marks of 50 students and store1. Program to read marks of 50 students and store them under an array.them under an array.

#include<iostream.h>#include<iostream.h>

int main()int main(){ {

const int size = 50;const int size = 50;float marks[size];float marks[size];for (int i=0 ; I < size ; i++)for (int i=0 ; I < size ; i++)

{ { cout<<”Enter marks of students” << i+1 << “\n”; cout<<”Enter marks of students” << i+1 << “\n”; cin > > marks[i]; cin > > marks[i]; } }

cout<<”\n” ; cout<<”\n” ; for( I = 0 ; i < size; i++) for( I = 0 ; i < size; i++)

cout << “ Marks [ “ << i << “] = “ << marks [i] << “ \n “ ;cout << “ Marks [ “ << i << “] = “ << marks [i] << “ \n “ ; return 0;return 0;} }

OutputOutput

Enter marks of student 1Enter marks of student 1

8989

Enter marks of student 2Enter marks of student 2

9898

Enter marks of student 3Enter marks of student 3

8888

Marks[1] =89Marks[1] =89

Marks[2]=98Marks[2]=98

Marks[3]=88Marks[3]=88

Page 3: C++ Project

2. Program to accept sales of each day of month & print2. Program to accept sales of each day of month & print avg. & total sales of monthavg. & total sales of month..

#include<iostream.h> #include<iostream.h>

int main()int main()

{ {

const int size = 3 ;const int size = 3 ;

float sales[size],avg = 0, total= 0;float sales[size],avg = 0, total= 0;

for(int i = 0; i< size; i++)for(int i = 0; i< size; i++)

{ {

cout<<”enter sales made on day” << i+1 <<” :”;cout<<”enter sales made on day” << i+1 <<” :”;

cin>> sales [i];cin>> sales [i];

total +=sales[i];total +=sales[i];

}}

avg =total/size;avg =total/size;

cout<<”\n Totalsales=”<<total<<”\n”;cout<<”\n Totalsales=”<<total<<”\n”;

cout<<”average sales = “ <<avg << “\n”;cout<<”average sales = “ <<avg << “\n”;

return0;return0;

}}

Output:-Output:-

Enter sales made on day 1 : 19925Enter sales made on day 1 : 19925

Enter sales made on day 2 : 23324Enter sales made on day 2 : 23324

Total sales :49249Total sales :49249

Average sales : 25565.65415Average sales : 25565.65415

Page 4: C++ Project

3.Program to search for a specific element in 1-D array.3.Program to search for a specific element in 1-D array.

#include<iostream.h>#include<iostream.h>

#include<stdio.h>#include<stdio.h>

int main()int main()

{ {

int A[20],size,i,flag =0 ,num ,pos; int A[20],size,i,flag =0 ,num ,pos;

cout<<”\n Enter the number of elements in the array : “;cout<<”\n Enter the number of elements in the array : “;

cin >> size;cin >> size;

cout << “\n Enter the elements of array (in ascending order):” ;cout << “\n Enter the elements of array (in ascending order):” ;

for (i=0 ; i< size; i++)for (i=0 ; i< size; i++)

{{

cin>>A[i];cin>>A[i];

cout<<”\n Enter the element to be searched for : “ ;cout<<”\n Enter the element to be searched for : “ ;

cin>> num ;cin>> num ;

}}

for(i=0; i<size ;i++)for(i=0; i<size ;i++)

{ {

if(A[i] == num)if(A[i] == num)

{ {

flag = 1;flag = 1;

pos = i ;pos = i ;

break ;break ;

}}

if(flag == 0) if(flag == 0)

{{

cout<<”\n Element not found “;cout<<”\n Element not found “;

}}

else else

Page 5: C++ Project

{{

cout<<”\n Element found at position” << (pos+1);cout<<”\n Element found at position” << (pos+1);

}}

}}

return 0;return 0;

}}

Output:-Output:-

Enter the number of elements in the arrayEnter the number of elements in the array : 5 : 5

Enter the elements of array Enter the elements of array : 1 4 7 9 18: 1 4 7 9 18

Enter the element to be searched for Enter the element to be searched for : 9: 9

Element found at position Element found at position 44

4. Program to replace every space in a string with a4. Program to replace every space in a string with a hyphenhyphen..

Page 6: C++ Project

#include<iostream.h>#include<iostream.h>

#include<string.h>#include<string.h>

int main()int main()

{ {

char string [80];char string [80];

cout << “enter string max 79 characters)\n” ;cout << “enter string max 79 characters)\n” ;

cin.getline(string,80); cin.getline(string,80);

int x1 = strlen (string);int x1 = strlen (string);

for(int i = 0 ; string[i] !=’\0’ ; i++) for(int i = 0 ; string[i] !=’\0’ ; i++)

{{

if(string[i] ==‘ ‘)if(string[i] ==‘ ‘)

string[i] = ‘-‘ ;string[i] = ‘-‘ ;

}}

cout<< “The changed string is \n” ;cout<< “The changed string is \n” ;

cout.write(string,x1) ;cout.write(string,x1) ;

return0 ;return0 ;

}}

Output :-Output :-

Enter string(max 79 characters)Enter string(max 79 characters)

India is great!!!India is great!!!

The changed string isThe changed string is

India – is – great!!!India – is – great!!!

5.program to5.program to

#include<iostream.h>#include<iostream.h>#include<string.h>#include<string.h>

Page 7: C++ Project

void chg(char*&nm)void chg(char*&nm)

{{

strcpy (nm, “kush”) ; strcpy (nm, “kush”) ;

\\copy\\copy “kush” to nm “kush” to nm

} }

int main() int main()

{{

char name[ ] = “sandeep “char name[ ] = “sandeep “

cout < < name < < “\t” < < endl ;cout < < name < < “\t” < < endl ;

chg (name) ; chg (name) ;

cout < < name < < “\t” < < endl ;cout < < name < < “\t” < < endl ;

return0 ; return0 ;

}}

Output:-Output:-

sandeepsandeep

kushkush

6.Program to count no. of spaces in a string6.Program to count no. of spaces in a string ..

#include<iostream.h>#include<iostream.h>

#include<stdio.h>#include<stdio.h>

Page 8: C++ Project

Int main( )Int main( )

{ {

char str [80] ;char str [80] ;

int i, count =0;int i, count =0;

cout<<”\n Enter any string(max. 80 chars) : “ ;cout<<”\n Enter any string(max. 80 chars) : “ ;

gets(str) ;gets(str) ;

for (i=0 ;str [i] != ‘\0’ ; i++)for (i=0 ;str [i] != ‘\0’ ; i++)

{{

if(str[ i ] = = ‘ ‘ )if(str[ i ] = = ‘ ‘ )

count + + ;count + + ;

}}

cout<< “\n Number of spaces in the given string are :” <<count ;cout<< “\n Number of spaces in the given string are :” <<count ;

return 0 ; return 0 ;

}}

Output:-Output:-

Enter any string (max. 80 chars) : I Love My India Enter any string (max. 80 chars) : I Love My India

Number of spaces in the given string are : 3Number of spaces in the given string are : 3

7. Program to replace every space in a string with a7. Program to replace every space in a string with a hyphenhyphen..

#include<iostream.h>#include<iostream.h>

#include<string.h>#include<string.h>

int main()int main()

Page 9: C++ Project

{ {

char string [80];char string [80];

cout << “enter string max 79 characters)\n” ;cout << “enter string max 79 characters)\n” ;

cin.getline(string,80);cin.getline(string,80);

int x1 = strlen (string);int x1 = strlen (string);

for(int i = 0 ; string[i] !=’\0’ ; i++)for(int i = 0 ; string[i] !=’\0’ ; i++)

{{

if(string[i] ==‘ ‘)if(string[i] ==‘ ‘)

string[i] = ‘-‘ ;string[i] = ‘-‘ ;

}}

cout<< “The changed string is \n” ;cout<< “The changed string is \n” ;

cout.write(string,x1) ;cout.write(string,x1) ;

return 0 ;return 0 ;

}}

Output :-Output :-

Enter string(max 79 characters)Enter string(max 79 characters)

India is great!!!India is great!!!

The changed string isThe changed string is

India – is – great!!!India – is – great!!!

8. Program using structure to get details about movie8. Program using structure to get details about movie name , type and ticket cost.name , type and ticket cost.

#include<iostream.h>#include<iostream.h>

#include<stdio.h>#include<stdio.h>

int main ( )int main ( )

Page 10: C++ Project

{{

struct movie struct movie

{ {

char movie_name[20];char movie_name[20];

char movie_type ; char movie_type ;

int ticket _cost ; int ticket _cost ;

} MOVIE ; } MOVIE ;

MOVIE.ticket_cost = 100 ; MOVIE.ticket_cost = 100 ;

gets (MOVIE.movie_ name ) ; gets (MOVIE.movie_ name ) ;

cin >> MOVIE.movie_type ; cin >> MOVIE.movie_type ;

return0 ; return0 ;

}}

9.Progam using structure to get details of members of a9.Progam using structure to get details of members of a gymgym..

#include<iostream.h>#include<iostream.h>

struct supergymstruct supergym

{ {

int membernumber ;int membernumber ;

char membername[20] ;char membername[20] ;

Page 11: C++ Project

char membertype[4];char membertype[4];

} ;} ;

int main( )int main( )

{ {

supergym person1,person2 ;supergym person1,person2 ;

cout <<” member number :” ;cout <<” member number :” ;

cin>> person1.membernumber ;cin>> person1.membernumber ;

cout<<”member name : “ ;cout<<”member name : “ ;

cin >>person1.membername ;cin >>person1.membername ;

strcpy(person1.membertype,”MIG”) ;strcpy(person1.membertype,”MIG”) ;

person2=person1;person2=person1;

cout<<” member number : “ << person2.membernumber ;cout<<” member number : “ << person2.membernumber ;

cout<<”member name : “ << person2.membername ;cout<<”member name : “ << person2.membername ;

cout<<”member type :” <<person2.membertype ;cout<<”member type :” <<person2.membertype ;

return0 ;return0 ;

}}

OUTPUT:-OUTPUT:-

Member Number: 213Member Number: 213

Member Name : YamanMember Name : Yaman

Member Type: yealyMember Type: yealy

10. Program to find area and perimeter of a rectangle.10. Program to find area and perimeter of a rectangle.

#include<iostream>#include<iostream>

int main()int main()

{{

int width,height,area,perimeter;int width,height,area,perimeter;

cout<<"Enter Width of Rectangle = ";cout<<"Enter Width of Rectangle = ";

cin>>width;cin>>width;

Page 12: C++ Project

cout<<"Enter Height of Rectangle = ";cout<<"Enter Height of Rectangle = ";

cin>>height;cin>>height;

area=height*width;area=height*width;

cout<<"Area of Rectangle ="<<area<<endl;cout<<"Area of Rectangle ="<<area<<endl;

perimeter=2*(height+width);perimeter=2*(height+width);

cout<<" Perimeter of rectangle are = "<<perimeter<<endl;cout<<" Perimeter of rectangle are = "<<perimeter<<endl;

return 0;return 0;

}}

OUTPUT:-OUTPUT:-

Enter width of rectangle = 6Enter width of rectangle = 6

Enter height of rectangle = 8Enter height of rectangle = 8

Area of rectangle= 48Area of rectangle= 48

Perimeter of rectangle=96 Perimeter of rectangle=96

11.Program to print fibonacci series upto given terms.11.Program to print fibonacci series upto given terms.

#include<iostream>#include<iostream>

int main()int main()

{{

int range, first = 0, second = 1, fibonicci=0;int range, first = 0, second = 1, fibonicci=0;

cout << "Enter Range for Terms of Fibonacci Sequence: ";cout << "Enter Range for Terms of Fibonacci Sequence: ";

cin >> range;cin >> range;

cout << "Fibonicci Series upto " << range << "Terms "<< endl;cout << "Fibonicci Series upto " << range << "Terms "<< endl;

for ( int c = 0 ; c < range ; c++ ) for ( int c = 0 ; c < range ; c++ )

Page 13: C++ Project

{{

if ( c <= 1 )if ( c <= 1 )

{{

fibonicci = c;fibonicci = c;

}}

elseelse

{{

fibonicci = first + second;fibonicci = first + second;

first = second;first = second;

second = fibonicci;second = fibonicci;

}}

cout << fibonicci <<" ";cout << fibonicci <<" ";

return 0;return 0;

}}

OUTPUT:-OUTPUT:-Enter Range for Terms of FibonacciEnter Range for Terms of Fibonacci

Sequence:3Sequence:3 Fibonicci Series upto:3Fibonicci Series upto:3

0 1 1 2 3 0 1 1 2 3

12.Program to find factorial of a number.12.Program to find factorial of a number.

#include<iostream>#include<iostream>

int main()int main()

{{

int num,factorial=1;int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: ";cout<<" Enter Number To Find Its Factorial: ";

cin>>num;cin>>num;

for(int a=1;a<=num;a++)for(int a=1;a<=num;a++)

{{

Page 14: C++ Project

factorial=factorial*a;factorial=factorial*a;

}}

cout<<"Factorial of Given Number is ="<<factorial<<endl;cout<<"Factorial of Given Number is ="<<factorial<<endl;

return 0;return 0;

}}

OUTPUT :-OUTPUT :-

Enter number to find its factorial=5Enter number to find its factorial=5

Factorial of the given number is : 120Factorial of the given number is : 120

13.Program to swap the values of two integers.13.Program to swap the values of two integers.

#include<iostream>#include<iostream>

int main()int main()

{{

int var1, var2, swap;int var1, var2, swap;

cout<<"Enter value for first integer: ";cout<<"Enter value for first integer: ";

cin>>var1;cin>>var1;

cout<<"Enter value for second integer: ";cout<<"Enter value for second integer: ";

cin>>var2;cin>>var2;

cout<<" Values Before swapping: "<<endl;cout<<" Values Before swapping: "<<endl;

cout<<"First Integer ="<<var1<<endl;cout<<"First Integer ="<<var1<<endl;

Page 15: C++ Project

cout<<"Second Interger ="<<var2<<endl;cout<<"Second Interger ="<<var2<<endl;

swap=var1;swap=var1;

var1=var2;var1=var2;

var2=swap;var2=swap;

cout<<" Values After swapping: "<<endl;cout<<" Values After swapping: "<<endl;

cout<<"First Integer ="<<var1<<endl;cout<<"First Integer ="<<var1<<endl;

cout<<"Second Interger ="<<var2<<endl;cout<<"Second Interger ="<<var2<<endl;

return 0;return 0;

}}

Output:-Output:-

Enter value for first integer=5Enter value for first integer=5

Enter value for second integer=4Enter value for second integer=4

Value before swapping=54Value before swapping=54

Values after swapping =45Values after swapping =45

14. Program to produce reverse of the entered number14. Program to produce reverse of the entered number..

#include<iostream>#include<iostream>

int main() int main()

{{

int number, reverse = 0;int number, reverse = 0;

cout<<"Input a Number to Reverse and press Enter:";cout<<"Input a Number to Reverse and press Enter:";

cin>> number; //Taking Input Number in variable numbercin>> number; //Taking Input Number in variable number

for( ; number!= 0 ; )for( ; number!= 0 ; )

{{

reverse = reverse *10;reverse = reverse *10;

reverse = reverse + number%10;reverse = reverse + number%10;

number = number/10;number = number/10;

}}

Page 16: C++ Project

cout<<"New Reversed Number is: "<<reverse;cout<<"New Reversed Number is: "<<reverse;

return 0;return 0;

}}

Output:-Output:-

Enter the number to be reversed:456123Enter the number to be reversed:456123

New reversed number is :321654New reversed number is :321654