Postclass Answers
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);
return 0;
}
2)#include <stdio.h>
int main() {
int R1, N, R2, X, total, cost;
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;
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);
3)#include <stdio.h>
int main() {
int n1, n2, count = 0;
scanf("%d %d", &n1, &n2);
printf("%d\n", count);
return 0;
}
Series
1)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of terms
return 0;
}
2)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of terms
return 0;
}
3)#include <stdio.h>
int main() {
int N;
scanf("%d", &N); // Input the starting number
return 0;
}
Pattern
1)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of rows
return 0;
}
2)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of rows for the upper half
return 0;
}
3)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of rows for the upper half
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
return 0;
}
2)#include <stdio.h>
int main() {
int N, count = 0;
scanf("%d", &N); // Input the number of houses
3)#include <stdio.h>
int main() {
int n;
scanf("%d", &n); // Input the number of readings
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
}
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;
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
}
}
return 0;
}
Strings
1)#include <stdio.h>
int main() {
char seats[27]; // Array to hold the seat labels
int count = 0;
2)#include <stdio.h>
int main() {
char word[100];
int freq[26] = {0}, maxCount = 0;
3)#include <stdio.h>
#include <string.h>
int main() {
char S[51], compressed[51];
int index = 0;
return 0;
}
Recursion
1)#include <stdio.h>
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 main() {
int number;
scanf("%d", &number);
printf("%d\n", countSetBits(number));
return 0;
}
3)#include <stdio.h>
int main() {
char str[1000];
printf("%d\n", stringLength(str));
return 0;
}
—★—★—★—★—★—