In: Computer Science
C++ program
Generate and display two random characters involving upper and lowercase
#include <iostream>
using namespace std;
int main(){
char c;
int r;
srand (time(NULL)); // initializing random number generator
for (int i=0; i<2; i++) {
int caseOfChar = rand() % 2; //0 for lower case, 1 for upper case
r = rand() % 26; // generate a random number
if (caseOfChar ==0) //0: lower case
c = 'a' + r; // Convert to a character from a-z
else if(caseOfChar == 1) //1: upper case
c = 'A' + r; // Convert to a character from A-Z
cout << c <<endl;
}
}
OUTPUT with Code Snippet