Você está na página 1de 15

Computer Project

1. Program to read marks of 50 students and store


them under an array.
#include<iostream.h>
int main()
{
const int size = 50;
float marks[size];
for (int i=0 ; I < size ; i++)
{
cout<<Enter marks of students << i+1 << \n;
cin > > marks[i];
}
cout<<\n ;
for( I = 0 ; i < size; i++)
cout << Marks [ << i << ] = << marks [i] << \n ;
return 0;
}

Output
Enter marks of student 1
89
Enter marks of student 2
98
Enter marks of student 3
88
Marks[1] =89
Marks[2]=98
Marks[3]=88

2. Program to accept sales of each day of month & print


avg. & total sales of month.

#include<iostream.h>
int main()
{
const int size = 3 ;
float sales[size],avg = 0, total= 0;
for(int i = 0; i< size; i++)
{
cout<<enter sales made on day << i+1 << :;
cin>> sales [i];
total +=sales[i];
}
avg =total/size;
cout<<\n Totalsales=<<total<<\n;
cout<<average sales = <<avg << \n;
return0;
}

Output:Enter sales made on day 1 : 19925


Enter sales made on day 2 : 23324
Total sales :49249
Average sales : 25565.65415

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


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

int main()
{
int A[20],size,i,flag =0 ,num ,pos;
cout<<\n Enter the number of elements in the array : ;
cin >> size;
cout << \n Enter the elements of array (in ascending order): ;
for (i=0 ; i< size; i++)
{
cin>>A[i];
cout<<\n Enter the element to be searched for :

cin>> num ;
}
for(i=0; i<size ;i++)
{
if(A[i] == num)
{
flag = 1;
pos = i ;
break ;
}
if(flag == 0)
{
cout<<\n Element not found ;
}
else
{
cout<<\n Element found at position <<

(pos+1);
}

}
return 0;
}

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


Enter the elements of array :

4 7 9 18

Enter the element to be searched for : 9


Element found at position 4

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


hyphen.
#include<iostream.h>
#include<string.h>
int main()
{

char string [80];


cout << enter string max 79 characters)\n ;
cin.getline(string,80);
int x1 = strlen (string);
for(int i = 0 ; string[i] !=\0 ; i++)
{
if(string[i] == )
string[i] = - ;
}
cout<< The changed string is \n ;
cout.write(string,x1) ;
return0 ;
}

Output :Enter string(max 79 characters)


India is great!!!
The changed string is
India is great!!!

5.program to
#include<iostream.h>
#include<string.h>
void chg(char*&nm)
{
strcpy (nm, kush) ;
\\copy kush to nm

}
int main()
{
char name[ ] = sandeep
cout < < name < < \t < < endl ;
chg (name) ;
cout < < name < < \t < < endl ;
return0 ;
}

Output:sandeep
kush

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


#include<iostream.h>
#include<stdio.h>
Int main( )
{
char str [80] ;
int i, count =0;

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


gets(str) ;
for (i=0 ;str [i] != \0 ; i++)
{
if(str[ i ] = = )
count + + ;
}
cout<< \n Number of spaces in the given string are : <<count ;
return 0 ;
}

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


Number of spaces in the given string are : 3

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


hyphen.
#include<iostream.h>
#include<string.h>
int main()
{
char string [80];
cout << enter string max 79 characters)\n ;
cin.getline(string,80);
int x1 = strlen (string);

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


{
if(string[i] == )
string[i] = - ;
}
cout<< The changed string is \n ;
cout.write(string,x1) ;
return 0 ;
}

Output :Enter string(max 79 characters)


India is great!!!
The changed string is
India is great!!!

8. Program using structure to get details about movie


name , type and ticket cost.
#include<iostream.h>
#include<stdio.h>
int main ( )
{
struct movie
{
char movie_name[20];
char movie_type ;
int ticket _cost ;

} MOVIE ;
MOVIE.ticket_cost = 100 ;
gets (MOVIE.movie_ name ) ;
cin >> MOVIE.movie_type ;
return0 ;
}

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


gym.
#include<iostream.h>
struct supergym
{
int membernumber ;
char membername[20] ;
char membertype[4];
};
int main( )
{
supergym person1,person2 ;
cout << member number : ;
cin>> person1.membernumber ;

cout<<member name : ;
cin >>person1.membername ;
strcpy(person1.membertype,MIG) ;
person2=person1;
cout<< member number : << person2.membernumber ;
cout<<member name : << person2.membername ;
cout<<member type : <<person2.membertype ;
return0 ;
}

OUTPUT:Member Number: 213


Member Name : Yaman
Member Type: yealy

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


#include<iostream>
int main()
{
int width,height,area,perimeter;
cout<<"Enter Width of Rectangle = ";
cin>>width;
cout<<"Enter Height of Rectangle = ";
cin>>height;
area=height*width;
cout<<"Area of Rectangle ="<<area<<endl;
perimeter=2*(height+width);
cout<<" Perimeter of rectangle are = "<<perimeter<<endl;
return 0;

OUTPUT:Enter width of rectangle = 6


Enter height of rectangle = 8
Area of rectangle= 48
Perimeter of rectangle=96

11.Program to print fibonacci series upto given terms.


#include<iostream>
int main()
{
int range, first = 0, second = 1, fibonicci=0;
cout << "Enter Range for Terms of Fibonacci Sequence: ";
cin >> range;
cout << "Fibonicci Series upto " << range << "Terms "<< endl;
for ( int c = 0 ; c < range ; c++ )
{
if ( c <= 1 )
{
fibonicci = c;
}
else
{

fibonicci = first + second;


first = second;
second = fibonicci;
}
cout << fibonicci <<" ";
return 0;
}

OUTPUT:-

Enter Range for Terms of Fibonacci


Sequence:3
Fibonicci Series upto:3
0 1 1 2 3

12.Program to find factorial of a number.


#include<iostream>
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}

OUTPUT :-

Enter number to find its factorial=5


Factorial of the given number is : 120

13.Program to swap the values of two integers.


#include<iostream>
int main()
{
int var1, var2, swap;
cout<<"Enter value for first integer: ";
cin>>var1;
cout<<"Enter value for second integer: ";
cin>>var2;
cout<<" Values Before swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
swap=var1;
var1=var2;
var2=swap;
cout<<" Values After swapping: "<<endl;
cout<<"First Integer ="<<var1<<endl;
cout<<"Second Interger ="<<var2<<endl;
return 0;

Output:Enter value for first integer=5


Enter value for second integer=4
Value before swapping=54
Values after swapping =45

14. Program to produce reverse of the entered number .


#include<iostream>
int main()
{
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter:";
cin>> number; //Taking Input Number in variable number
for( ; number!= 0 ; )
{
reverse = reverse *10;
reverse = reverse + number%10;
number = number/10;
}
cout<<"New Reversed Number is: "<<reverse;
return 0;
}

Output:Enter the number to be reversed:456123


New reversed number is :321654

Você também pode gostar