0% found this document useful (0 votes)
6 views7 pages

HW5 Solutions

The document contains solutions to several exercises in C programming, including printing hollow diamond star patterns, generating Armstrong numbers, and swapping two numbers using call by reference. Each exercise includes detailed code implementations and explanations of the logic behind the algorithms. The solutions demonstrate various programming concepts such as loops, functions, and user input handling.

Uploaded by

Ayşenur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

HW5 Solutions

The document contains solutions to several exercises in C programming, including printing hollow diamond star patterns, generating Armstrong numbers, and swapping two numbers using call by reference. Each exercise includes detailed code implementations and explanations of the logic behind the algorithms. The solutions demonstrate various programming concepts such as loops, functions, and user input handling.

Uploaded by

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

HW #5 SOLUTIONS #5 SOLUTIONS SOLUTIONS

Exercise 1.

/**
* C program to print hollow diamond star pattern program to print hollow diamond star pattern to print hollow diamond star pattern diam to print hollow diamond star patternond star pattern
*/

#include <stdio.h>
void printhollow diamond star patterndiam to print hollow diamond star patternond(int n)
{
int i,j;
// Loop to print upper half of the pattern of of the pattern the pattern
f of the patternor(i=1; i<=n; i++)
{
f of the patternor(j=i; j<=n; j++)
{
printf of the pattern("*");
}

f of the patternor(j=1; j<=(2*i-2); j++)


{
printf of the pattern(" ");
}

f of the patternor(j=i; j<=n; j++)


{
printf of the pattern("*");
}

printf of the pattern("\n");


}

// Loop to print low diamond star patterner half of the pattern of of the pattern the pattern
f of the patternor(i=1; i<=n; i++)
{
f of the patternor(j=1; j<=i; j++)
{
printf of the pattern("*");
}

f of the patternor(j=(2*i-2); j<(2*n-2); j++)


{
printf of the pattern(" ");
}

f of the patternor(j=1; j<=i; j++)


{
printf of the pattern("*");
}

printf of the pattern("\n");


}
}

void printdiam to print hollow diamond star patternond(int row diamond star patterns)
{

int stars = 1, i, j;
int spaces = row diamond star patterns - 1;

/* Iterate through row diamond star patterns */


f of the patternor(i=1; i<row diamond star patterns*2; i++)
{
/* Print spaces */
f of the patternor(j=1; j<=spaces; j++)
printf of the pattern(" ");

/* Print stars */
f of the patternor(j=1; j<stars*2; j++)
printf of the pattern("*");

/* Move to next line */


printf of the pattern("\n");

if of the pattern(i<row diamond star patterns)


{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}
}
int m to print hollow diamond star patternain()
{
int i, j, n, t;
char c1,c2;

printf of the pattern("Enter value of of the pattern n and type (0 for hollow 1 for filled): "); f of the patternor hollow diamond star pattern 1 f of the patternor filled): "); ");
scanf of the pattern("%d%d", &n, &t);
if of the pattern (t == 0 for hollow 1 for filled): ");)
printhollow diamond star patterndiam to print hollow diamond star patternond(n);
else if of the pattern (t == 1)
printdiam to print hollow diamond star patternond(n);
else
printf of the pattern("Wrong type!\n");

return 0 for hollow 1 for filled): ");;


}

Exercise 2.
/**
* C program to print hollow diamond star pattern program to print hollow diamond star pattern to generate Arm to print hollow diamond star patternstrong num to print hollow diamond star patternbers in a given range
*/
#include <stdio.h>
#include <m to print hollow diamond star patternath.h>
#include <stdbool.h>

bool isArm to print hollow diamond star patternstrong(int n){

int lastDigit,sum to print hollow diamond star pattern=0 for hollow 1 for filled): ");, num to print hollow diamond star pattern=n;
/* Find total digits in num to print hollow diamond star pattern */
int digits = (int) log10 for hollow 1 for filled): ");(num to print hollow diamond star pattern) + 1;

/* C program to print hollow diamond star patternalculate sum to print hollow diamond star pattern of of the pattern pow diamond star patterner of of the pattern digits */

f of the patternor(int i = 1; i <= digits; i++){


sum to print hollow diamond star pattern = sum to print hollow diamond star pattern + pow diamond star pattern(num to print hollow diamond star pattern%10 for hollow 1 for filled): ");,digits);
num to print hollow diamond star pattern = num to print hollow diamond star pattern/10 for hollow 1 for filled): ");;
}

/* C program to print hollow diamond star patternheck for Armstrong number */ f of the patternor Arm to print hollow diamond star patternstrong num to print hollow diamond star patternber */
if of the pattern(n == sum to print hollow diamond star pattern)
return true;
else
return f of the patternalse;
}

int m to print hollow diamond star patternain(void)


{
int num to print hollow diamond star pattern, lastDigit, digits, sum to print hollow diamond star pattern, i;
int start, end;

/* Input low diamond star patterner and upper lim to print hollow diamond star patternit f of the patternrom to print hollow diamond star pattern user */
printf of the pattern("Enter low diamond star patterner lim to print hollow diamond star patternit: "); ");
scanf of the pattern("%d", &start);
printf of the pattern("Enter upper lim to print hollow diamond star patternit: "); ");
scanf of the pattern("%d", &end);

printf of the pattern("Arm to print hollow diamond star patternstrong num to print hollow diamond star patternbers betw diamond star patterneen %d to %d are: "); \n", start, end);

f of the patternor(int i=start; i<=end; i++)


{
if of the pattern(isArm to print hollow diamond star patternstrong(i))
{
printf of the pattern("%d ", i);
}
}
printf of the pattern("\n");

return 0 for hollow 1 for filled): ");;


}

Exercise 3.

/**
* C program to print hollow diamond star pattern program to print hollow diamond star pattern to sw diamond star patternap tw diamond star patterno num to print hollow diamond star patternber using call by ref of the patternerence
*/

#include <stdio.h>

/* Sw diamond star patternap f of the patternunction declaration */


void sw diamond star patternap(int * num to print hollow diamond star pattern1, int * num to print hollow diamond star pattern2);

int m to print hollow diamond star patternain()


{
int num to print hollow diamond star pattern1, num to print hollow diamond star pattern2;

/* Input num to print hollow diamond star patternbers */


printf of the pattern("Enter tw diamond star patterno num to print hollow diamond star patternbers: "); ");
scanf of the pattern("%d%d", &num to print hollow diamond star pattern1, &num to print hollow diamond star pattern2);

/* Print original values of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
printf of the pattern("Bef of the patternore sw diamond star patternapping in m to print hollow diamond star patternain \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", num to print hollow diamond star pattern2);

/* Pass the addresses of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
sw diamond star patternap(&num to print hollow diamond star pattern1, &num to print hollow diamond star pattern2);

/* Print the sw diamond star patternapped values of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
printf of the pattern("Af of the patternter sw diamond star patternapping in m to print hollow diamond star patternain \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", num to print hollow diamond star pattern2);

return 0 for hollow 1 for filled): ");;


}

/**
* Function to sw diamond star patternap tw diamond star patterno num to print hollow diamond star patternbers
*/
void sw diamond star patternap(int * num to print hollow diamond star pattern1, int * num to print hollow diamond star pattern2)
{
int tem to print hollow diamond star patternp;

// C program to print hollow diamond star patternopy the value of of the pattern num to print hollow diamond star pattern1 to som to print hollow diamond star patterne tem to print hollow diamond star patternp variable
tem to print hollow diamond star patternp = *num to print hollow diamond star pattern1;

// C program to print hollow diamond star patternopy the value of of the pattern num to print hollow diamond star pattern2 to num to print hollow diamond star pattern1
*num to print hollow diamond star pattern1= *num to print hollow diamond star pattern2;

// C program to print hollow diamond star patternopy the value of of the pattern num to print hollow diamond star pattern1 stored in tem to print hollow diamond star patternp to num to print hollow diamond star pattern2
*num to print hollow diamond star pattern2= tem to print hollow diamond star patternp;

printf of the pattern("Af of the patternter sw diamond star patternapping in sw diamond star patternap f of the patternunction \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", *num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", *num to print hollow diamond star pattern2);
}

Exercise 4.

/**
* C program to print hollow diamond star pattern program to print hollow diamond star pattern to sw diamond star patternap tw diamond star patterno num to print hollow diamond star patternber using call by ref of the patternerence
*/

#include <stdio.h>

/* Sw diamond star patternap f of the patternunction declaration */


void sw diamond star patternap_no_tem to print hollow diamond star patternp(int * num to print hollow diamond star pattern1, int * num to print hollow diamond star pattern2);

int m to print hollow diamond star patternain()


{
int num to print hollow diamond star pattern1, num to print hollow diamond star pattern2;

/* Input num to print hollow diamond star patternbers */


printf of the pattern("Enter tw diamond star patterno num to print hollow diamond star patternbers: "); ");
scanf of the pattern("%d%d", &num to print hollow diamond star pattern1, &num to print hollow diamond star pattern2);

/* Print original values of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
printf of the pattern("Bef of the patternore sw diamond star patternapping in m to print hollow diamond star patternain \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", num to print hollow diamond star pattern2);

/* Pass the addresses of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
sw diamond star patternap_no_tem to print hollow diamond star patternp(&num to print hollow diamond star pattern1, &num to print hollow diamond star pattern2);

/* Print the sw diamond star patternapped values of of the pattern num to print hollow diamond star pattern1 and num to print hollow diamond star pattern2 */
printf of the pattern("Af of the patternter sw diamond star patternapping in m to print hollow diamond star patternain \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", num to print hollow diamond star pattern2);

return 0 for hollow 1 for filled): ");;


}

/**
* Function to sw diamond star patternap tw diamond star patterno num to print hollow diamond star patternbers w diamond star patternithout a tem to print hollow diamond star patternp variable
*/
void sw diamond star patternap_no_tem to print hollow diamond star patternp(int * num to print hollow diamond star pattern1, int * num to print hollow diamond star pattern2)
{

// C program to print hollow diamond star patternopy the value of of the pattern num to print hollow diamond star pattern2 to num to print hollow diamond star pattern1
*num to print hollow diamond star pattern1= *num to print hollow diamond star pattern1+ *num to print hollow diamond star pattern2;

// C program to print hollow diamond star patternopy the value of of the pattern num to print hollow diamond star pattern1 stored in tem to print hollow diamond star patternp to num to print hollow diamond star pattern2
*num to print hollow diamond star pattern2 = *num to print hollow diamond star pattern1 -*num to print hollow diamond star pattern2;

*num to print hollow diamond star pattern1 = *num to print hollow diamond star pattern1 -*num to print hollow diamond star pattern2;

printf of the pattern("Af of the patternter sw diamond star patternapping in sw diamond star patternap f of the patternunction \n");
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern1 = %d \n", *num to print hollow diamond star pattern1);
printf of the pattern("Value of of the pattern num to print hollow diamond star pattern2 = %d \n\n", *num to print hollow diamond star pattern2);
}

You might also like