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

PROG. QUESTIONS

The document contains multiple C programming solutions for various tasks, including finding the sum of two integers, checking if a number is even or odd, finding the largest element in an array, reversing a string, swapping two numbers without a temporary variable, and calculating the sum of digits of a number. It also includes pattern generation programs such as pyramid patterns, diamond patterns, number pyramids, and Pascal's triangle. Each solution is presented with code snippets and prompts for user input.

Uploaded by

geekygod07
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)
11 views7 pages

PROG. QUESTIONS

The document contains multiple C programming solutions for various tasks, including finding the sum of two integers, checking if a number is even or odd, finding the largest element in an array, reversing a string, swapping two numbers without a temporary variable, and calculating the sum of digits of a number. It also includes pattern generation programs such as pyramid patterns, diamond patterns, number pyramids, and Pascal's triangle. Each solution is presented with code snippets and prompts for user input.

Uploaded by

geekygod07
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

Question : Write a C program to find the sum of two integers.

Solution:

#include <stdio.h>

int main() {
int num1, num2, sum;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("Sum = %d\n", sum);

return

0;
}

Question : Create a C program to check if a given number is even


or odd.

Solution:

#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);

return 0;
}
Question : Create a C program to find the largest element in an
array.

Solution:

C
#include <stdio.h>

int main() {
int n, i, largest;

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


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

largest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}

printf("The largest element in the array is: %d\n",


largest);

return

0;
}

Question : Develop a C program to reverse a string.

Solution:

C
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int length, i;
printf("Enter a string: ");
scanf("%s", str);

length = strlen(str);

printf("Reversed string: ");


for (i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");

return 0;
}

Question : Develop a C program to swap two numbers without using a


temporary variable.

Solution:

C
#include <stdio.h>

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1,


num2);

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1

- num2;

printf("After swapping: num1 = %d, num2 = %d\n", num1,


num2);

return 0;

Question : Write a C program to find the sum of digits of


a given number.
Solution:

C
#include <stdio.h>

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

printf("Enter a number: ");


scanf("%d", &number);

while (number != 0) {
digit = number % 10;
sum += digit;
number /= 10;
}

printf("Sum of

digits = %d\n", sum);

return

0;
}

PATTERN QUESTIONS

Pattern 1: Pyramid Pattern

C
#include <stdio.h>

int main() {
int rows, i, j;

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


scanf("%d", &rows);

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


for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for
(j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}

return

0;
}

Pattern 2: Diamond Pattern

C
#include <stdio.h>

int main() {
int rows, i, j;

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


scanf("%d", &rows);

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


for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for

(j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}

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


for (j = 1; j <= rows - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");

return 0;
}
Pattern 3: Number Pyramid

C
#include <stdio.h>

int main() {
int rows, i, j, number = 1;

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


scanf("%d", &rows);

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


for (j = 1; j

<= rows - i; j++) {


printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("%d ", number);

number++;
}
printf("\n");
}

return 0;
}

Pattern 4: Pascal's Triangle

C
#include <stdio.h>

int main() {
int rows, i, j, coefficient = 1;

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


scanf("%d", &rows);

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


for (j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
coefficient = 1;
} else

{
coefficient = coefficient * (i - j + 1) / j;
}
printf("%d ", coefficient);
}
printf("\n");
}

return

0;
}

PATTERN 5 :
*
**
***
****
*****

#include <stdio.h>

int main() {
int rows, i, j;

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


scanf("%d", &rows);

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


for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return

0;
}

You might also like