Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

C++ PROGRAMS

ASSIGNMENT # 3

COMSATS UNIVERSITY SAHIWAL CAMPUS


SUBMITTED TO PROF. AMEER AHMAD

Shahzaib Akhtar
SP22-BCS-197
PROGRAM 1: Write a program to print 10 random numbers
between 0-100 and find out the largest among them.
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int Rmax()
{
int a[10];
int max=0;
srand(time(0));
for(int i=0;i<10;i++)
{
a[i]=rand()%101+0;
cout<<a[i]<<endl;
if(a[i]>max)
max=a[i];
}
return max;
}
int main()
{
cout<<"Random Maximum number amoung array is : "<<Rmax()<<endl;
system("pause");
return 0;
}
OUTPUT 1
65
72
76
12
71
91
8
4
3
23
Random Maximum number amoung array is : 91
Press any key to continue . . .

OUTPUT 2
51
84
14
47
8
50
4
73
70
12
Random Maximum number amoung array is : 84
Press any key to continue . . .
PROGRAM 2: Write a program that takes number from user as
an input and display the reverse of that number.
#include<iostream>
using namespace std;
int main()
{
int n,ld,rev=0;
cout<<"Enter number : ";
cin>>n;
if(n>0)
{
for(;n>0;)
{
ld=n%10;
rev=(rev*10)+ld;
n=n/10;
}
cout<<"Your number after reverse is : "<<rev<<endl;
}
else
cout<<"You enter invalid number"<<endl;
system("pause");
return 0;
}
OUTPUT 1
Enter number : 123456
Your number after reverse is : 654321
Press any key to continue . . .

OUTPUT 2
Enter number : -123456
You enter invalid number
Press any key to continue . . .
PROGRAM 3: Write a program to find if the number entered by
the user is palindrome or not.
#include<iostream>
using namespace std;
int main()
{
int n,temp,ld,rev=0;
cout<<"Enter number : ";
cin>>n;
temp=n;

for(;temp>0;)
{
ld=temp%10;
rev=(rev*10)+ld;
temp=temp/10;
}

if(n==rev)
cout<<"Your number is palindrome"<<endl;
else
cout<<"Your number is not palindrome"<<endl;

system("pause");
return 0;
}
OUTPUT 1
Enter number : 121
Your number is palindrome
Press any key to continue . . .

OUTPUT 2
Enter number : 123
Your number is not palindrome
Press any key to continue . . .

You might also like