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

Random Coding Problems Solutions

The document contains ten C programming solutions to various coding problems, including finding the sum of array elements, checking if a number is even or odd, displaying natural numbers, determining leap years, calculating factorials, sorting arrays, finding maximum and minimum elements, displaying patterns, checking for palindromes, and finding the largest of three numbers. Each solution includes the necessary code and comments for clarity. These examples serve as practical exercises for learning C programming concepts.

Uploaded by

s73646079
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)
14 views7 pages

Random Coding Problems Solutions

The document contains ten C programming solutions to various coding problems, including finding the sum of array elements, checking if a number is even or odd, displaying natural numbers, determining leap years, calculating factorials, sorting arrays, finding maximum and minimum elements, displaying patterns, checking for palindromes, and finding the largest of three numbers. Each solution includes the necessary code and comments for clarity. These examples serve as practical exercises for learning C programming concepts.

Uploaded by

s73646079
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

Solutions to Random Coding Problems

1. Write a program in C to find the sum of all elements of the array.

#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:

");

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

scanf("%d", &arr[i]);

sum += arr[i];

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

return 0;

2. Write a C program to check whether a given number is even or odd.

#include <stdio.h>

int main() {

int num;
printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("%d is even\n", num);

else

printf("%d is odd\n", num);

return 0;

3. Write a program in C to display the first 10 natural numbers.

#include <stdio.h>

int main() {

printf("The first 10 natural numbers are:\n");

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

printf("%d ", i);

printf("\n");

return 0;

4. Write a C program to find whether a given year is a leap year or not.

#include <stdio.h>

int main() {

int year;
printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d is a leap year\n", year);

else

printf("%d is not a leap year\n", year);

return 0;

5. Write a program in C to calculate the factorial of a given number.

#include <stdio.h>

int main() {

int n, i, fact = 1;

printf("Enter a number: ");

scanf("%d", &n);

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

fact *= i;

printf("Factorial of %d is %d\n", n, fact);

return 0;

6. Write a program in C to sort elements of array in ascending order.

#include <stdio.h>
int main() {

int n, i, j, temp;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

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

scanf("%d", &arr[i]);

for (i = 0; i < n - 1; i++) {

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

if (arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

printf("Sorted array in ascending order:\n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

7. Write a program in C to find the maximum and minimum element in an array.


#include <stdio.h>

int main() {

int n, i, max, min;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");

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

scanf("%d", &arr[i]);

max = min = arr[0];

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

if (arr[i] > max)

max = arr[i];

if (arr[i] < min)

min = arr[i];

printf("Maximum: %d\nMinimum: %d\n", max, min);

return 0;

8. Write a program in C to display the pattern like right angle triangle using an asterisk.

#include <stdio.h>
int main() {

int n;

printf("Enter the number of rows: ");

scanf("%d", &n);

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

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

printf("*");

printf("\n");

return 0;

9. Write a C program to check whether a number is a palindrome or not.

#include <stdio.h>

int main() {

int n, reversed = 0, original, remainder;

printf("Enter a number: ");

scanf("%d", &n);

original = n;

while (n != 0) {

remainder = n % 10;

reversed = reversed * 10 + remainder;

n /= 10;

}
if (original == reversed)

printf("%d is a palindrome\n", original);

else

printf("%d is not a palindrome\n", original);

return 0;

10. Write a C program to find the largest of three numbers using if statement.

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)

printf("Largest number is %d\n", a);

else if (b >= a && b >= c)

printf("Largest number is %d\n", b);

else

printf("Largest number is %d\n", c);

return 0;

You might also like