In: Computer Science
Please code the program showing the output below. using C language
1. Using 'if' or 'while' or 'for' and 'break' statement / only using <stdio.h>
A
bC
dEf
GhIj
KlMnO
2. a program that identifies the largest number that can be expressed in short.
Using only loop (ex.for,if,while) and break statement
only using <stdio.h>
Part 1)
For this we take a character ch and initialize it to 'A'. Then using a for loop, we traverse 5 times for 5 lines of output.
Inside this loop, there is another for loop to print the characters, if conditions are used to determine the casing of the alphabets.
CODE:
#include <stdio.h>
int main()
{
int i,j,k=0,n;
char ch='A';
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++,k++,ch++)
{
if(k%2==0)
{
printf("%c",ch);
}
else
{
printf("%c",ch+32);
}
}
printf("\n");
}
}
OUTPUT:
2) For printing highest value of short(unsigned), we take the exponential approach. Since size of short is 2 bytes then their are 16 bits. So, 0 to 216-1 will be the range of this data type.
To calculate the exponential value, we use for loop, in which we multiply base value(2) with the product, "expo" number of times.
CODE:
int main()
{
int i, exponent = 16, base =2;
unsigned short power =1;
for(i=1; i<=exponent; i++)
{
power = power * base;
}
printf("%hu", power-1);
}
OUTPUT: