0% found this document useful (0 votes)
50 views

Quiz 2 PF

The document contains the solutions to two programming questions submitted by a student for their Programming Fundamentals quiz. The first question asks to write a program that prints a diamond shape using minimal print statements and nested for loops. The student provides the C code for a program that takes user input for the number of rows and prints the diamond shape. The second question asks to write a program that gets a character from the user and prints all characters from that input to z, using both call by value and call by reference. The student provides the C code for a program implementing this using separate functions for uppercase and lowercase characters with the appropriate call methods.

Uploaded by

khan g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Quiz 2 PF

The document contains the solutions to two programming questions submitted by a student for their Programming Fundamentals quiz. The first question asks to write a program that prints a diamond shape using minimal print statements and nested for loops. The student provides the C code for a program that takes user input for the number of rows and prints the diamond shape. The second question asks to write a program that gets a character from the user and prints all characters from that input to z, using both call by value and call by reference. The student provides the C code for a program implementing this using separate functions for uppercase and lowercase characters with the appropriate call methods.

Uploaded by

khan g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Comsat University Abbottabad Department of BSE-2C

COMSAT UNIVERSITY ISLAMABAD


ABBOTTABAD CAMPUS

Course Title : Programming Fundamental


QUIZ NO : 02
Submitted By : USAMA
Registration NO : CIIT/FA20-BSE-086/ABT
Section : C
Submitted to : SIR RAB NAWAZ
Date : Wednesday, May 04, 2021
Comsat University Abbottabad Department of BSE-2C

Question 1:
(Diamond Printing Program)Write a program that prints the
following diamond shape. You may use printf statements that print either a
single asterisk (*) or asingle blank. Maximize your use of repetition (with
nested for statements) and minimize the number of printf statements.
Answer:
Program Code:
// c program to print a diamond shape
#include<stdio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d",&n);
for(i = 0; i <= n; i++)
{
for(s = n; s > i; s--)
printf(" ");
for(j=0; j<i; j++)
printf("* ");
printf("\n");
}
for(i = 1; i < n; i++)
{
for(s = 0; s < i; s++)
printf(" ");
Comsat University Abbottabad Department of BSE-2C

for(j = n; j > i; j--)


printf("* ");

printf("\n");
}
return 0;
}

Output:
Enter the 10 rows that show the diamond shape.

Question 2:
Write a program that gets a character from the user, using call by
value and by reference, print all the characters from the read characters to z
(Either entered in small or capital), also print the result in main how many
characters are printed from the character read. Implement this program by
using both the calling methods...
Comsat University Abbottabad Department of BSE-2C

1. Call by value
2. Call by reference.
Answer:
Program Code:

#include <stdio.h>

int func_upper(char *);


int func_lower(char);

int main() {
char chr, chr3, loop1;
int number=0;

printf("Enter any character (upper or lower case) . . . ");


scanf("%c", &chr);
chr3 = chr;
if (chr>='a' && chr<='z')
{
number = func_lower(chr3);

} else
{
number = func_upper(&chr3);
}
Comsat University Abbottabad Department of BSE-2C

printf("\n\nThere are %d alphabets starting from %c till end\n\n\n", number,


chr3);
getche();
return 0;
}

int func_upper(char *chr1)


{
int count=0;
char loop1;

for (loop1 = *chr1; loop1 <= 'Z'; ++loop1)


{
printf("%c ", loop1);
count = count + 1;
}
return count;
}

int func_lower(char chr2)


{
int count=0;
char loop2;

for (loop2 = chr2; loop2 <= 'z'; ++loop2)


{
Comsat University Abbottabad Department of BSE-2C

printf("%c ", loop2);


count = count + 1;
}
return count;
}

Output:

1st output:

2nd output:
Comsat University Abbottabad Department of BSE-2C

The End

You might also like