Program to Print Right Angled Triangle with Increasing Alternate Case Character Pattern
In this program we are going to see how to print the right angled triangle with increasing alternate case character pattern.
Example-1 When starting character=h and row value=3 h I j k L m
Example-2: When starting character=a and row value=5 a B c d E f G h I j k L m N o
Now, let’s see the actual program to print it.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Approach:
- Enter the character and store it in a variable
c. - Then enter total row and store it in an integer variable
row_count. - Take one outer for loop to iterate the rows.
- Take one inner loop to iterate the columns and print the character.
- After each iteration print a new line.
JAVA CODE:
import java.util.Scanner;
import java.lang.*;
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//Taking character to be printed as input from the user
System.out.print("First Character : ");
char c = scan.next().charAt(0);
//Taking number of rows as input from the user
System.out.print("Rows : ");
int row_count = scan.nextInt();
int row, col;
//Converting the character to its ascii value
int asciiValue = (int) c;
//Checks whether the character is a letter or not
if(asciiValue>=65&&asciiValue<=122)
{
//Outer loop to iterate rows
for(row = 0; row<row_count; row++){
//Inner loop to iterate columns
for(col = 0; col<row+1; col++){
//Alternating the case in each iteration
if((row+col)%2==0)
System.out.print(Character.toLowerCase(c)+" ");
else
System.out.print(Character.toUpperCase(c)+" ");
//Incrementing the character
c++;
}
//Prints a newline
System.out.println();
}
}
}
}
Output First Character : a Rows : 5 a B c d E f G h I j k L m N o
C CODE:
#include <stdio.h>
int main()
{
//Taking character to be printed as input from the user
printf("First Character : ");
char c;
scanf("%c", &c);
//Taking number of rows as input from the user
printf("Rows : ");
int row_count;
scanf("%d", &row_count);
int row, col;
//Converting the character to its ascii value
int asciiValue = (int)c;
//Checks whether the character is a letter or not
if (asciiValue >= 65 && asciiValue <= 122)
{
//Outer loop to iterate rows
for (row = 0; row < row_count; row++)
{
//Inner loop to iterate columns
for (col = 0; col < row + 1; col++)
{
//Alternating the case in each iteration
if ((row + col) % 2 == 0)
printf("%c ", tolower(c));
else
printf("%c ", toupper(c));
//Incrementing the character
c++;
}
//Prints a newline
printf("\n");
}
}
return 0;
}
Output First Character : a Rows : 5 a B c d E f G h I j k L m N o
C++ CODE:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
//Taking character to be printed as input from the user
cout << "First Character : ";
char c;
cin >> c;
//Taking number of rows as input from the user
cout << "Rows : ";
int row_count;
cin >> row_count;
int row, col;
//Converting the character to its ascii value
int asciiValue = (int)c;
//Checks whether the character is a letter or not
if (asciiValue >= 65 && asciiValue <= 122)
{
//Outer loop to iterate rows
for (row = 0; row < row_count; row++)
{
//Inner loop to iterate columns
for (col = 0; col < row + 1; col++)
{
//Alternating the case in each iteration
if ((row + col) % 2 == 0)
cout << (char)tolower(c) << " ";
else
cout << (char)toupper(c) << " ";
//Incrementing the character
c++;
}
//Prints a newline
cout << endl;
}
}
return 0;
}
Output: First Character : a Rows : 5 a B c d E f G h I j k L m N o
Related Java Star Pattern Programs:
- Java Program to Print Right Angled Triangle with Repeating Character (Increasing Order) Pattern
- Java Program to Print Right Angled Triangle with Repeating Character (Decreasing Order) Pattern
- Java Program to Print Right Angled Triangle with Column Wise Increasing Character Pattern
- Java Program to Print Right Angled Triangle with Row wise Decreasing Character Pattern
- Java Program to Print Right Angled Triangle with Row wise Increasing Character Pattern