0% found this document useful (0 votes)
19 views16 pages

HW 4

The document contains code snippets for C programs that demonstrate basic concepts like loops, functions, arrays, pointers etc. It contains multiple code snippets organized into different sections with each snippet focusing on a programming concept and having sample input/output.

Uploaded by

Flaky
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)
19 views16 pages

HW 4

The document contains code snippets for C programs that demonstrate basic concepts like loops, functions, arrays, pointers etc. It contains multiple code snippets organized into different sections with each snippet focusing on a programming concept and having sample input/output.

Uploaded by

Flaky
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/ 16

: PART 1

:Question 6

>include <stdio.h#

{ void printStarPattern(int n)

{ for (int i = 0; i < n; i++)

;)" *"(printf

;printf("\n")

{ )(int main

;int input

;printf("Enter the number of stars you want to print: ")

;scanf("%d", &input)

;printStarPattern(input)

;return 0

:Question 7

>include <stdio.h#

{ int sumUpTo(int n)

;int sum = 0

{ for (int i = 1; i <= n; i++)

;sum += i
}

;return sum

{ )(int main

;int input, result

;printf("Enter an integer: ")

;scanf("%d", &input)

;result = sumUpTo(input)

;printf("The sum of all numbers up to %d is: %d\n", input, result)

;return 0

:Question 8

>include <stdio.h#

{ int sumOfEvenNumbers(int start, int end)

;int sum = 0

{ if (start % 2 != 0)

;++start

{ for (int i = start; i <= end; i += 2)

;sum += i

;return sum

{ )(int main
;int start, end, result

;printf("Enter the starting integer: ")

;scanf("%d", &start)

;printf("Enter the ending integer: ")

;scanf("%d", &end)

;result = sumOfEvenNumbers(start, end)

;printf("The sum of all even numbers between %d and %d is: %d\n", start, end, result)

;return 0

: PART 2
:Question 1

>include <stdio.h#

{ )(int main
;int num, count = 0
;double sum = 0.0

printf("Enter positive numbers (enter a negative number to


;terminate):\n")
{ while (1)
;scanf("%d", &num)
{ if (num < 0)
;break
}
;sum += num
;++count
}

{ if (count > 0)
;double average = sum / count
;printf("Average of the numbers entered: %.2f\n", average)
{ else }
;printf("No positive numbers were entered.\n")
}

;return 0
}

:Question 2

>include <stdio.h#

{ )(int main
;int i = 0

{ do
;)" *"(printf
;++i
;while (i < 10) }

;printf("\n")

;return 0
}

:Question 3

>include <stdio.h#

{ )(int main
;int rows = 3
;int columns = 4

{ for (int i = 0; i < rows; i++)


{ for (int j = 0; j < columns; j++)
;)" *"(printf
}
;printf("\n")
}

;return 0
}
:Question 4

>include <stdio.h#

{ )(int main
;int sum = 0

{ for (int i = 3; i <= 99; i += 3)


;sum += i
}

;sleep(1)
;printf("Result of the series: %d\n", sum)

;return 0
}

:Question 5

>include <stdio.h#

{ )(int main
;int n
;unsigned long long factorial = 1
;printf("Enter a positive integer: ")
;scanf("%d", &n)

{ if (n < 0)
;printf("Error! Factorial of a negative number doesn't exist.")
{ else }
{ for (int i = 1; i <= n; ++i)
;factorial *= i
}
;printf("Factorial of %d = %llu\n", n, factorial)
}

;return 0
}

:Question 6

>include <stdio.h#

{ )(int main
;int num, sum = 0, digit

;printf("Enter a number: ")


;scanf("%d", &num)
{ while (num != 0)
;digit = num % 10
;sum += digit
;num /= 10
}

;printf("Sum of digits: %d\n", sum)

;return 0
}

:PART 3
:Question 1

>include <stdio.h#

{ )(int main

;int sum = 0

{ for (int i = 3; i <= 99; i += 3)

;sum += i

;sleep(1)

;printf("Result of the series: %d\n", sum)

;return 0
}

:Question 2

>include <stdio.h#

>include <limits.h#

{ )(int main

;int num

;int largest = INT_MIN

;int smallest = INT_MAX

;printf("Enter positive numbers (enter a negative number to terminate):\n")

{ while (1)

;scanf("%d", &num)

{ if (num < 0)

;break

{ if (num > largest)

;largest = num

{ if (num < smallest)

;smallest = num

}
}

{ if (largest == INT_MIN && smallest == INT_MAX)

;printf("No positive numbers were entered.\n")

{ else }
;printf("Largest number: %d\n", largest)

;printf("Smallest number: %d\n", smallest)

;return 0

:Question 3

>include <stdio.h#

{ )(int main

;int rows = 5

{ for (int i = 1; i <= rows; i++)

{ for (int j = 1; j <= i; j++)

;printf("%d ", j)

;printf("\n")

;return 0

:Question 4

>include <stdio.h#

{ )(int main
;int rows = 5

{ for (int i = rows; i >= 1; i--)

{ for (int j = 1; j <= i; j++)

;printf("%d ", j)

;printf("\n")

;return 0

:Question 5

>include <stdio.h#

{ )(int main

;int rows = 5

{ for (int i = 1; i <= rows; i++)

{ for (int j = 1; j <= i; j++)

;printf("%d ", i)

;printf("\n")

;return 0

}
:Question 6

>include <stdio.h#

{ )(int main

;int rows = 5

{ for (int i = rows; i >= 1; i--)

{ for (int j = 1; j <= i; j++)

;printf("%d ", i)

;printf("\n")

;return 0

:PART 4
:Question 2

>include <stdio.h#

{ )(int main
;int num, sum = 0

printf("Enter positive numbers (enter a negative number to


;terminate):\n")
{ do
;scanf("%d", &num)
{ if (num >= 0)
;sum += num
}
;while (num >= 0) }

;printf("Sum of positive numbers: %d\n", sum)

;return 0
}

:Question 3

>include <stdio.h#

{ )(int main
;int num

;printf("Enter a number: ")


;scanf("%d", &num)

;printf("Multiplication table of %d:\n", num)


{ for (int i = 1; i <= 10; i++)
;printf("%d x %d = %d\n", num, i, num * i)
}

;return 0
}

:Question 4

>include <stdio.h#

{ )(int main
;int num, i = 1

;printf("Enter a number: ")


;scanf("%d", &num)

;printf("Multiplication table of %d:\n", num)


{ while (i <= 10)
;printf("%d x %d = %d\n", num, i, num * i)
;++i
}

;return 0
}

:Question 5

>include <stdio.h#
>include <limits.h#

{ )(int main
;int num
;int largest = INT_MIN

printf("Enter positive numbers (enter a negative number to


;terminate):\n")

{ while (1)
;scanf("%d", &num)
{ if (num < 0)
;break
}
{ if (num > largest)
;largest = num
}
}

{ if (largest == INT_MIN)
;printf("No positive numbers were entered.\n")
{ else }
;printf("Largest number: %d\n", largest)
}

;return 0
}

You might also like