0% found this document useful (0 votes)
6 views

Lect 4.1

ma

Uploaded by

3li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lect 4.1

ma

Uploaded by

3li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dijlah University College

Computer Science Department


2nd Stage
Object Oriented Programming
Lecturer Ali Taha Yaseen
Ex 1: C++ functions to find the largest and smallest of three
integers entered by user in the main program.
#include <iostream>
using namespace std;
int max( int,int ,int);
int min(int ,int ,int);
int main ()
{
int smallest,largest,x1,x2,x3;
cout <<"enter three integers ";
cin>>x1>>x2>>x3;
largest=max(x1,x2,x3);
smallest=min(x1,x2,x3);
cout<<"the Largest "<<largest<<endl;
cout<<"the Smallest " <<smallest<<endl;
return(0);
}
int max(int v1,int v2,int v3)// function to find the largest
{
int big;
big=v1;
if (v2>big) big=v2;
if (v3>big) big=v3;
return (big) ;
}
int min(int y1,int y2,int y3)// function to find the smallest
{
int small;
small=y1;
if (y2<small) small=y2;
if (y3<small) small=y3;
return (small);
}
Output of the program :
Enter three integers : 3
4
5
The largest = 5
The smallest = 3
Ex 2:C++ Program to Find the Length of a String.
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50];
int len;
cout << "Enter your string : ";
gets(str); //gets() reads characters
len = strlen(str); // srtlen() Get string length
cout << "Length of the string is : " << len;
return 0;
}
Output of the program :
Enter your string :Dijlah University college
Length of the string is : 25
EX 3: Program to count number of words in a sentence.
#include<iostream>
#include<string>
using namespace std;
int const size=100;
int wordcount(char[]);
int main()
{
char str[size];
cout<<"Enter a Sentence :";
cin.getline(str,size,'\n'); //cin. getline() is used to read unformatted string
cout<< "number of words = "<<wordcount(str);
}
int wordcount(char x[])
{
int count=1;
for (int i=0;i<strlen(x);i++)
if(x[i]==' ') count++;
return(count);
}
Output of the program :
Enter a Sentence: Dijlah University college
Number of words is : 3

You might also like