You are on page 1of 16

COMPUTER SCIENCE PRACTICAL FILE

Program 1
Write a C++ program to print numbers from 1 to 10 using for loop.

Code
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
system("cls");
int a;
cout<<" The numbers from 1 to 10 are as follows:"<<endl;
for(a=1;a<=10;a++)
{
cout<<a<<endl;
}
system("pause");
return 0;
}Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 2
Write a C++ program to find the factorial of a number taking the input from the user.

Code
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n, f=1;
cout<<"Enter the Number :";
cin>>n;
for (int i= 1; i<= n;i++)
{
f*=i;
}
cout<<"Factorial of "<<n<<" is "<<f;
getch();
return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 3
Write a C++ program to input characters and change their case.

Code
#include <iostream.h>
#include <stdio.h>
int main()
{
char ch;
do
{
cout<<" Enter a character:";
ch=getchar();
if(ch=='\n')
ch=getchar();
cout<<endl;
if(ch>=65&&ch<=90)
ch=ch+32;
else
if(ch>=97&&ch<=122)
ch=ch-32;
putchar(ch);
cout<<endl;
}
while(ch!='0');
3

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 4
Write a C++ program to read a string and print its length.

Code
#include <iostream.h>
#include <string.h>
#include <stdio.h>
int main()
{
char astr[51];
int length;
cout<<"\n Enter a string (max 50 characters)\n ";
gets(astr);
length=strlen(astr);
puts(astr);
cout<<"\n The length of the string is :"<<length<<"\n";
return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 5
Write a C++ program to count number of character entered.

Code
#include <iostream.h>
int main(){
int chcount=0;
const char ent='\n';
char ch;
cout<<"Enter character \n";
cin.get(ch);
while(ch!=ent)
{ chcount++;
cout.put(ch);
cin.get(ch);
}
cout<<"\n The number of characters ="<<chcount<<"\n";
return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 6
Write a C++ program to whether two strings contain equal number of characters

Code
#include <iostream.h>
#include <string.h>
int main()
{
char string1[50],string2[50];
cout<<"Enter strings \n";
cin.getline(string1,50);
cin.getline(string2,50);
if (strlen(string1)==strlen(string2))
cout<<"\n Both strings contain equal number of characters"<<"\n";
else
cout<<"\n Both strings contain different number of characters"<<"\n";
return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


Program 7
Write a C++ program to read marks of 50 students & store them under an array

Code
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
int main() {
clrscr();
float marks[50];
for (int i=0;i<50;i++)
{
cout<<"Enter the marks of student"<<i+1<<":";
cin>>marks[i];
}
cout<<"\n";
for(i=0;i<50;++i)
cout<<"Marks["<<i<<"]="<<marks[i]<<"\n";
return 0;
}

Output

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 8
Write a C++ program to check of a string is palindrome or not

Code
#include <iostream.h>
int main()
{
char inputString[100];
int l, r, length = 0;
cout << "Enter a string for palindrome check\n";
cin >> inputString;
while(inputString[length] != '\0')
length++;
l = 0;
r = length -1;

while(l < r){


if(inputString[l] != inputString[r]){
cout<<"It is Not a Palindrome"<< endl;
return 0;
}
9

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


l++;
r--;
}
cout << "It is a Palindrome\n" << endl;
return 0;
}

Output

10

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 9
Write a C++ program to print the largest element of an array(using functions)

Code
#include <iostream.h>
#include <stdlib.h>
int main()
{
system("cls");
float large (float arr[],int n);
char ch;
int i=0;
float amount[50],biggest;
for(i=0;i<50;i++)
{
cout<<"Enter element no. "<<(i+1)<<"\n";
cin>>amount[i];
cout<<"More elements? (y/n) \n";
cin>>ch;
if(ch!='y')
11

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


break;
}
if (i<50)
i++;
biggest=large(amount,i);
cout<<"The largest amount of the array is :"<<biggest<<"\n";
return 0;

}
float large(float arr[],int n)
{
float max=arr[0];
for(int j=1;j<n;++j)
{
if (arr[j]>max)
max=arr[j];
}
return max;
}

Output

12

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

Program 10
Write a C++ program to calculate interest amount using function overloading. Calculate
interest amount using function overloading.

Code
#include <iostream.h>
#include <stdlib.h>

void amount(float princ,int time,float rate)


{
cout<<"\n Principal Amount:"<<princ;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(princ*time*rate)<<"\n";
}
13

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


void amount(float princ,int time)
{
cout<<"\n Principal Amount:"<<princ;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:0.08";
cout<<"\nInterest Amount:"<<(princ*time*0.08)<<"\n";
}
void amount(float princ,float rate)
{
cout<<"\n Principal Amount:"<<princ;
cout<<"\tTime:2 years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(princ*2*rate)<<"\n";
}
void amount(int time,float rate)
{
cout<<"\n Principal Amount: 2000";
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(2000*time*rate)<<"\n";
}
void amount(float princ)
{
cout<<"\n Principal Amount:"<<princ;
cout<<"\tTime: 2 years";
14

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE


cout<<"\tRate:0.08";
cout<<"\nInterest Amount:"<<(princ*2*0.08)<<"\n";
}
int main()
{
system("cls");
cout<<"case 1";
amount(2000.0F);
cout<<"case 2";
amount(2500.0F,3);
cout<<"case 3";
amount(2300.0F,3,0.11F);
cout<<"case 4";
amount(2,0.12F);
cout<<"case 5";
amount(5,0.07F);
return 0;
}

Output

15

NAME:Vishwas Bhati
ROLL NUMBER:

COMPUTER SCIENCE PRACTICAL FILE

16

NAME:Vishwas Bhati
ROLL NUMBER:

You might also like