Alphabetic Pattern Capital Letters Pyramid C Programing - c

I got pretty stuck when I had a to do a letter pyramid in C (via Xcode). We have to input the number of rows which will be equivalent to the number of capital letters. This is what it should look like :
Enter number of rows : 4
Output:
A
ABA
ABCBA
ABCDCBA
It is limited to min: 4 and Max 26. This is what I've managed so far. Thank you in advance :
int main(){
char letter = 'A'; //NOTE ! letter + 1 = 'B'
int i, rows, space, k = 0;
printf("Enter number of rows: \t");
scanf("%i", &rows);
while(rows < 4 || rows > 26){
printf("\nError !\nMin : 4\tMax : 26\n");
scanf("%i", &rows);
}
for(i = 1; i <= rows; i++,k =0){
for(space = 1; space <= rows - i; space++){
printf(" ");
}
for(; k!= 2* i-1; k++){
printf("%c", letter);
}
for (char temp = letter + 1; temp <= letter; temp--){
printf("%c", temp);
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 4
A
AAA
AAAAA
AAAAAAA
Program ended with exit code: 0

Solved :
#include <stdio.h>
int main(){
char letter = 'A', temp, temp1; //NOTE ! letter + 1 = 'B'
int i, rows, space, k = 0;
printf("Enter number of rows: \t");
scanf("%i", &rows);
while(rows < 4 || rows > 26){
printf("\nError !\nMin : 4\tMax : 26\n");
scanf("%i", &rows);
}
for(i = 1; i <= rows; i++,k =0){
for(space = 1; space <= rows - i; space++){
printf(" ");
}
temp = letter + i - 1;
temp1 = temp;
for(; k!= 2* i-1; k++){
if(letter == temp){
printf("%c", temp1);
temp1--;
}else{
printf("%c", letter);
letter++;
}
}
letter = 'A';
printf("\n");
}
return 0;
}

Related

How to count uppercase letter in each word in a 2D string

I was trying to find out uppercase letters in each word in 2D string using C. But I'm not figuring out with the logic. I was assuming this result -
Sample Input
2
Dhaka
apple
Sample Output
1
0
Here's my code -
#include<stdio.h>
#include<string.h>
int main()
{
int count = 0, num;
char str[50][50];
printf("How many word you want to enter: ");
scanf("%d", &num);
printf("\nEnter any word: ");
for(int i=0; i<num; i++)
{
for(int j=0; j<num; j++)
{
gets(str);
}
}
for(int i=0; str[i]!='\0'; i++)
{
for(int j=i+1; j<num; j++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
count++;
}
}
}
printf("\n%d\n", count);
return 0;
}
Source :
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
int count = 0, num;
//Enter how many lines you want to enter
printf("How many lines you want to enter: ");
scanf("%d", &num);
char str[num][100];
//printf("\nEnter any word: ");
for(int i=0; i<num; i++){
//flushs the previous buffer
getchar();
//Press enter to enter a line
scanf("%[^\n]",str[i]);
}
for(int i=0;i<num; i++,count=0){
for(int j=0; str[i][j]!='\0'; j++){
//printf("%c",str[i][j]);
//if(str[i][j] >= 'A' && str[i][j] <= 'Z'){
// count++;
//}
if( isupper(str[i][j]) )
count++;
}
printf("\n%d\n",count);
}
return 0;
}
Output :
$ gcc string.c && ./a.out
How many lines you want to enter: 3
djjdjd JJ
j3j3j3j JJJJJKK
$#jjd
2
7
0

Can we scanf an array?

I want to input an array of integers then print out the even numbers from the inputted numbers..
example is if I input 2466688992,
it will output 24666882;
I have a my code below:
#include<stdio.h>
int main()
{
int a[5],i;
printf("Enter array of numbers: ");
scanf("%d",&a);
for(i=0; i<sizeof(a); i++){
if(a[i]%2==0)
printf("%d",a[i]);
}
getch();
return 0;
}
It resulted into garbage : 2468000075416640419940000004225568000
This is the function that prints even numbers in an integer :
#include<stdio.h>
int main(){
int num,rem,even=0,digit;
printf(" Enter an integer number: ");
scanf("%d",&num);
printf("\n The even digits present in %d are \n",num);
while(num>0){
digit = num % 10;
num = num / 10;
rem = digit % 2;
if(rem == 0)
even++;
printf("\n %d.",digit);
}
return 0;
}
You should scan the array as a string (unless you want to impose the number of items in the array), and then parse the string to store the different numbers:
long a[50];
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
int j = 0;
for (int i = 0; i < len; ) {
long sign = 1;
long n = 0;
if (buf[i] == '+') {
++i;
}
else if (buf[i] == '-') {
sign = -1;
++i;
}
if (isdigit(buf[i])) {
while (isdigit(buf[i])) {
n = 10 * n + buf[i++] - '0';
}
a[j] = n * sign;
}
else
i++;
}
for (int i = 0; i < j; i++)
if (!(a[i] ℅ 2)) // true if even
printf("%ld ", a[i]);
This will store all your digits in your array a of size j.
Edit: if you are talking about digits then its easier:
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
for (int i = 0; i < len; i++)
if (isdigit(buf[i]) && !((buf[i] - '0') ℅ 2)) // true if even, note that '0' equals 0x30 so there is no need to sub it to check for odd/even in reality.
printf("%c ", buf[i]);

While Loop not Executing in C

I am unable to get this program to print out the lines of symbols with a tab. I included a picture of how it should be printing. Currently it's working except that there is no indentation. Any help at all would be greatly appreciated.
The idea is to display even numbered lines with indents (‘\t’) and the odd ones without indent:
Example of correct output
#include <stdio.h>
int main(void) {
int num_lines;
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
if (num_lines < 7) {
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
}
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
while (num_lines > 0) {
int n = num_symbols;
int nl = 1;
int nll = nl / 2;
while (nl <= num_lines) {
if ( (nl % 2) == 0) {
while (nll > 0) {
printf("\t");
--nll;
}
while (n > 0) {
printf("%c", symbol);
--n;
}
}
else {
while (n > 0) {
printf("%c", symbol);
--n;
}
}
++nl;
}
printf("\n");
--num_lines;
}
return;
}
In your loop there are problems in the way you declare your variables and put your conditions.
Try to think step by step:
1. Am I on on a pair or impair line?
2. If yes I put \t
3. Print x times my number
4. Do the same until you've done enough lines
if (num_symbols < 7 || num_symbols > 27) {
}
int which_line = 1;
while (num_lines > 0) {
if (which_line % 2 == 0) { //STEP 1
for (int tmp_line = which_line - 1; tmp_line >= 1 ; tmp_line--) { //STEP2
printf("\t");
}
}
for (int tmp_symbols = num_symbols; tmp_symbols >= 1 ; tmp_symbols--) { //STEP 3
printf("%c", symbol);
}
printf("\n");
which_line++; //STEP 4
--num_lines;
}
You can try this to print your desired pattern :
#include<stdio.h>
int main(void) {
int num_lines,i,j;
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
char symbol ;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
i = 1;
while (i <= num_lines) {
if(i%2 == 0){
j = i/2;
while(j != 0){
printf("\t");
j--;
}
}
j = 0;
while(j < num_symbols){
printf("%c",symbol);
j++;
}
printf("\n");
i++;
}
return 0;
}
^_^
#include <stdio.h>
int main(void) {
int num_lines;
do{
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}while(num_lines < 7);
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
for (int i = 1; i <= num_lines; ++i) {
if (i % 2 == 0) {
for (int m = 0; m < i / 2; ++m) {
printf("\t");
}
}
for (int j = 0; j < num_symbols; ++j) {
printf("%c", symbol);
}
printf("\n");
}
return 0;
}

How do I print a string's characters in a diagonal pattern?

I am trying to write a program that reads a string from the user and prints the string's characters in a diagonal pattern. I set the maximum length of the string as 50 characters. The program can print the characters in a diagonal pattern, but it doesn't print the characters correctly.
#include<stdio.h>
int main () {
int i = 0, j = 0, m;
char c[50];
printf("Enter a string: ");
scanf("%c", c);
m = sizeof(c) / sizeof(c[0]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= i; j++) {
if (j == i) {
printf("%c", c[i-1]);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Check the code below:
#include<stdio.h>
int main ()
{
int i=0,s=0;
char c[50];
printf("Enter a string: ");
scanf("%s",c);
while(c[i] != '\0')
{
s = i;
while(s--)
printf(" ");
printf("%c\n",c[i]);
i++;
}
return 0;
}

Basic database-like program in C using arrays

I have just learnt about arrays. I was trying to create a database program using arrays. It's a very basic program.
#include<stdio.h>
#define N 1 //number of entries needed
int main()
{
int i, k = 1, l = 1, w, x = 0, y = 0;
int rollnum[N], hsc[N], cet[N], a[N], b[N];
char name[100], city[100], c;
for(i = 0; i < N; i++)
{
printf("%d.\n", (i+1));
printf("Enter first name : ");
do
{
c = getchar();
if(c != '\n')
{
name[k] = c;
k++;
}
}
while(c != '\n');
a[i] = k;
k++;
printf("\n");
printf("Enter roll number : ");
scanf("%d", &rollnum[i]);
printf("\n");
getchar();
printf("Enter city : ");
do
{
c = getchar();
if(c != '\n')
{
city[l] = c;
l++;
}
}
while(c != '\n');
b[i] = l;
l++;
printf("\n");
printf("Enter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\n");
printf("Enter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
getchar();
}
printf("\n\n\n");
k = 1;
l = 1;
for(i = 0; i < N; i++)
{
printf("Entry %d\n", (i+1));
printf("Student Name : ");
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
putchar('\n');
printf("Marks : \n");
printf("\t");
printf("HSC : %d ", hsc[i]);
printf("\t");
printf("CET : %d / 200", cet[i]);
printf("\n\n\n");
}
return 0;
}
The program is not functioning the way I want it to! When I enter a name, the first letter is being printed out twice, same is the case with city! If I put 2 entries by modifying my 'N' , I'm getting the first letters of name and address(of second entry) as garbage values . I don't think there is any error in my logic, because i tried doing it manually in my notebook and I did not find any fault in it.
Can anyone help me find the mistake? I know the program might not at all be good and efficient, but I'm just trying out stuff that I've learnt!
You have made some mistakes initializing some variables. Also you don't have to read a string one character at a time. You can use scanf("%s", someString) to read a whole string.
And here is a working code that looks a lot more cleaner:
#include<stdio.h>
#define N 2
int main()
{
int rollnum[N], hsc[N], cet[N], i;
char name[100][100], city[100][100];
for(i = 0; i < N; i++)
{
printf("%d.\nEnter first name : ", (i+1));
scanf("%s", name[i]);
printf("\nEnter roll number : ");
scanf("%d", &rollnum[i]);
printf("\nEnter city : ");
scanf("%s", city[i]);
printf("\nEnter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\nEnter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
}
printf("\n\n\n");
for(i = 0; i < N; i++)
{
printf("Entry %d\nStudent Name : %s\nRoll number : %d\nCity : %s\nMarks : \n\tHSC : %d \tCET : %d / 200\n\n\n", (i+1), name[i], rollnum[i], city[i], hsc[i], cet[i]);
}
return 0;
}
It works for multiple entries too.
In order to make your program work you have to replace this:
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
with this:
if (i == 0)
x = a[i]-1;
else
x = a[i] - a[i-1];
for(w = 0; w < x; w++)
putchar(name[k++]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
if (i == 0)
y = b[i]-1;
else
y = b[i] - b[i-1];
for(w = 0; w < y; w++)
putchar(city[l++]);
the problem was that you didn't calculate the length of the words correctly, and also another error that I still can't explain but I removed that to.

Resources