0% found this document useful (0 votes)
17 views13 pages

Postclass Answers

The document contains a series of C programming code snippets organized into categories such as Operators, Decision Making, Looping, Series, Pattern, Arrays (1D and 2D), Strings, and Recursion. Each section provides examples of different programming concepts, including calculations, conditional statements, loops, series generation, pattern printing, array manipulations, string handling, and recursive functions. The examples demonstrate basic programming techniques and problem-solving skills in C.

Uploaded by

gaming with BSD
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)
17 views13 pages

Postclass Answers

The document contains a series of C programming code snippets organized into categories such as Operators, Decision Making, Looping, Series, Pattern, Arrays (1D and 2D), Strings, and Recursion. Each section provides examples of different programming concepts, including calculations, conditional statements, loops, series generation, pattern printing, array manipulations, string handling, and recursive functions. The examples demonstrate basic programming techniques and problem-solving skills in C.

Uploaded by

gaming with BSD
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/ 13

Postclass answers:

Operators

1)#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int sel=a*b;
int sp=a*c;
int profit=sel-sp-100;

printf("%d\n",profit);

return 0;
}

2)#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
int fd=a/1000;
int ld=a%10;
int sum=fd+ld;
printf("%d\n",sum);

return 0;
}

3) #include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int team=a/b;
int left=a%b;
printf("The number of friends in each team is %d and left out is %d",team,left);

Decision making
1)#include <stdio.h>

int main() {
int n;
scanf("%d", &n);

if (n < 100 || n > 999)


printf("Invalid Number\n");
else if ((n / 10) % 10 % 3 == 0)
printf("Trendy Number\n");
else
printf("Not a Trendy Number\n");

return 0;
}

2)#include <stdio.h>

int main() {
int R1, N, R2, X, total, cost;

scanf("%d %d %d %d", &R1, &N, &R2, &X);

total = (X + 59) / 60;

if (total <= N)
cost = total * R1;
else
cost = N * R1 + (total - N) * R2;

printf("%d\n", cost);

return 0;
}

3)#include <stdio.h>

int main() {
int height1, height2, height3;

scanf("%d %d %d", &height1, &height2, &height3);

int tallest = height1;


if (height2 > tallest)
tallest = height2;
if (height3 > tallest)
tallest = height3;

printf("%d\n", tallest);

return 0;
}

Looping

1)#include <stdio.h>

int main() {
int N, product = 1;
scanf("%d", &N);

while (N > 4) {
product *= 3;
N -= 3;
}
product *= N;

printf("%d\n", product);
return 0;
}

2)#include <stdio.h>

int main() {
int K;
char N[100];

scanf("%s", N);
scanf("%d", &K);

if (K > 0 && K <= strlen(N)) {


printf("%c\n", N[K - 1]);
} else {
printf("-1\n");
}
return 0;
}

3)#include <stdio.h>

int main() {
int n1, n2, count = 0;
scanf("%d %d", &n1, &n2);

for (int i = n1; i <= n2; i++) {


int num = i, digits[10] = {0}, unique = 1;
while (num > 0) {
if (digits[num % 10]++) {
unique = 0;
break;
}
num /= 10;
}
if (unique) count++;
}

printf("%d\n", count);
return 0;
}

Series

1)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of terms

// Generate and print the series


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

return 0;
}

2)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of terms

int current = 6, difference = 5; // Start with 6 oranges and a difference of 5


for (int i = 1; i <= n; i++) {
printf("%d ", current); // Print the current term
current += difference * i; // Update current with the growing difference
}

return 0;
}

3)#include <stdio.h>

int main() {
int N;
scanf("%d", &N); // Input the starting number

// Print the Collatz sequence


while (N > 1) {
printf("%d ", N);
if (N % 2 == 0) {
N /= 2; // If N is even, divide it by 2
} else {
N = 3 * N + 1; // If N is odd, multiply by 3 and add 1
}
}
printf("1\n"); // Print the final number 1

return 0;
}

Pattern

1)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of rows

for (int i = 0; i < n; i++) { // Loop through rows


for (int j = 0; j < i; j++) { // Print leading spaces
printf(" ");
}
for (int k = 0; k < n - i; k++) { // Print asterisks
printf("* ");
}
printf(" \n"); // Move to the next row
}

return 0;
}

2)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of rows for the upper half

// Upper half of the diamond


for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
printf(" "); // Print leading spaces
}
for (int k = 1; k <= 2 * i - 1; k++) {
printf("* "); // Print asterisks
}
printf("\n"); // Move to the next row
}

// Lower half of the diamond


for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
printf(" "); // Print leading spaces
}
for (int k = 1; k <= 2 * i - 1; k++) {
printf("* "); // Print asterisks
}
printf("\n"); // Move to the next row
}

return 0;
}

3)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of rows for the upper half

// Upper half of the hourglass


for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
printf(" "); // Print leading spaces
}
for (int k = 0; k < (2 * (n - i) - 1); k++) {
printf("* ");
}
printf("\n");
}

// Lower half of the hourglass


for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < i; j++) {
printf(" "); // Print leading spaces
}
for (int k = 0; k < (2 * (n - i) - 1); k++) {
printf("* ");
}
printf("\n");
}

return 0;
}

Arrays 1D

1)#include <stdio.h>

int main() {
int N, sum = 0, actualSum = 0;
scanf("%d", &N); // Input the length of the sequence

int A[N - 1]; // Array to hold N-1 elements


for (int i = 0; i < N - 1; i++) {
scanf("%d", &A[i]);
actualSum += A[i]; // Sum of all elements in the array
}

// Calculate the total sum of the sequence from 1 to N


sum = (N * (N + 1)) / 2;
// The missing number is the difference between the total sum and actual sum
printf("%d\n", sum - actualSum);

return 0;
}

2)#include <stdio.h>

int main() {
int N, count = 0;
scanf("%d", &N); // Input the number of houses

int A[N]; // Array to store the number of stairs in each house


for (int i = 0; i < N; i++) {
scanf("%d", &A[i]); // Input the stairs count for each house
if (A[i] % 3 == 0) { // Check if the stairs are a multiple of 3
count++;
}
}

printf("%d\n", count); // Output the count of houses Alice can climb


return 0;
}

3)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input the number of readings

int A[n]; // Array to store temperature readings


for (int i = 0; i < n; i++) {
scanf("%d", &A[i]); // Input the readings
}

// Print the reversed array


for (int i = n - 1; i >= 0; i--) {
printf("%d ", A[i]);
}
printf("\n");

return 0;
}
Arrays 2D

1)#include <stdio.h>

int main() {
int m, n, count = 0;
scanf("%d %d", &m, &n); // Input rows and columns

int matrix[m][n];
for (int i = 0; i < m; i++) {
int sorted = 1; // Assume row is sorted
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]); // Input elements
if (j > 0 && matrix[i][j] < matrix[i][j - 1]) {
sorted = 0; // Row is not sorted
}
}
if (sorted) count++; // Increment if sorted
}

printf("%d\n", count); // Output count of sorted rows


return 0;
}

2)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input size of the matrix

int matrix[n][n];
int primarySum = 0, secondarySum = 0;

// Input matrix elements and compute sums of diagonals


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]); // Input each element
if (i == j) {
primarySum += matrix[i][j]; // Add element to primary diagonal sum
}
if (i + j == n - 1) {
secondarySum += matrix[i][j]; // Add element to secondary diagonal sum
}
}
}

// Output the sums of the diagonals


printf("%d %d\n", primarySum, secondarySum);

return 0;
}

3)#include <stdio.h>

int main() {
int n;
scanf("%d", &n); // Input size of the matrix

int matrix[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]); // Input matrix elements
}
}

// Print the rotated matrix directly


for (int j = 0; j < n; j++) {
for (int i = n - 1; i >= 0; i--) {
printf("%d ", matrix[i][j]); // Rotate by accessing elements in reverse order
}
printf("\n");
}

return 0;
}

Strings

1)#include <stdio.h>

int main() {
char seats[27]; // Array to hold the seat labels
int count = 0;

// Input the seat arrangement


scanf("%s", seats);

for (int i = 0; seats[i] != '\0'; i++) {


// Check if the seat matches its expected position
if (seats[i] == 'A' + i || seats[i] == 'a' + i) {
count++;
}
}

// Print the count of correctly positioned seats


printf("%d\n", count);
return 0;
}

2)#include <stdio.h>

int main() {
char word[100];
int freq[26] = {0}, maxCount = 0;

scanf("%s", word); // Input the word

for (int i = 0; word[i] != '\0'; i++) {


freq[word[i] - 'a']++; // Update the count for the letter
if (freq[word[i] - 'a'] > maxCount) {
maxCount = freq[word[i] - 'a']; // Track the maximum frequency
}
}

printf("%d\n", maxCount); // Output the highest frequency


return 0;
}

3)#include <stdio.h>
#include <string.h>

int main() {
char S[51], compressed[51];
int index = 0;

// Input the server log string


scanf("%s", S);

// Compress the string by removing consecutive duplicates


for (int i = 0; S[i] != '\0'; i++) {
if (i == 0 || S[i] != S[i - 1]) {
compressed[index++] = S[i];
}
}
compressed[index] = '\0'; // Null-terminate the compressed string

// Output the compressed string in reverse order


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

return 0;
}

Recursion

1)#include <stdio.h>

int findMax(int arr[], int n) {


return (n == 1) ? arr[0] : ((arr[n - 1] > findMax(arr, n - 1)) ? arr[n - 1] : findMax(arr, n - 1));
}

int main() {
int n, arr[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("%d\n", findMax(arr, n));
return 0;
}

2)#include <stdio.h>

int countSetBits(int number) {


if (number == 0) {
return 0;
}
return (number & 1) + countSetBits(number >> 1);
}

int main() {
int number;
scanf("%d", &number);

printf("%d\n", countSetBits(number));
return 0;
}

3)#include <stdio.h>

int stringLength(char str[]) {


if (str[0] == '\0') {
return 0;
}
return 1 + stringLength(str + 1);
}

int main() {
char str[1000];

fgets(str, sizeof(str), stdin);


str[strcspn(str, "\n")] = '\0';

printf("%d\n", stringLength(str));
return 0;
}

—★—★—★—★—★—

You might also like