Solutions - M1-M2-M3.docx-1
Solutions - M1-M2-M3.docx-1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a,b;
float c,d;
scanf("%d %d",&a,&b);
scanf("%f %f",&c ,&d);
int int_sum = a+b;
int int_diff = a-b;
float float_sum = c+d;
float float_diff = c-d;
printf("%d %d\n",int_sum,int_diff);
printf("%0.1f %0.1f", float_sum,float_diff);
return 0;
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char ch;
char s[24];
char t[100];
scanf("%c", &ch);
scanf("%s", s);
getchar();
scanf("%[^\n]%*c", t);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", t);
return 0;
}
3.Conditional statements in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b,i;
scanf("%d\n%d", &a, &b);
// Complete the code.
for(i=a;i<=b;i++)
{
if(i<10)
{
if(i==1)
printf("one\n");
else if(i==2)
printf("two\n");
else if(i==3)
printf("three\n");
else if(i==4)
printf("four\n");
else if(i==5)
printf("five\n");
else if(i==6)
printf("six\n");
else if(i==7)
printf("seven\n");
else if(i==8)
printf("eight\n");
else if(i==9)
printf("nine\n");
}
}
}
4. Bitwise Operators
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void calculate_the_maximum(int n, int k) {
int max_and = 0, max_or = 0, max_xor = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
int temp_and = i & j;
int temp_or = i | j;
int temp_xor = i ^ j;
if (temp_and > max_and && temp_and < k) {
max_and = temp_and;
}
if (temp_or > max_or && temp_or < k) {
max_or = temp_or;
}
if (temp_xor > max_xor && temp_xor < k) {
max_xor = temp_xor;
}
}
}
printf("%d\n%d\n%d", max_and, max_or, max_xor);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
5. Valid Paranthesis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
// Global variables for stack and top
char stack[MAX_SIZE];
int top = -1;
// Function to push a character onto the stack
void push(char data) {
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\n");
return;
}
top++;
stack[top] = data;
}
// Function to pop a character from the stack
char pop() {
if (top == -1) {
printf("Empty stack!\n");
return ' ';
}
char data = stack[top];
top--;
return data;
}
MODULE -2
int main()
{
int n;
scanf("%d", &n);
int len = 2*n - 1;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
int min = i < j ? i : j;
min = min < len-i ? min : len-i-1;
min = min < len-j-1 ? min : len-j-1;
printf("%d ", n-min);
}
printf("\n");
}
return 0;
}
Output
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
void insertionSort(int N, int arr[]) {
int i,j;
int value;
for(i=1;i<N;i++)
{
value=arr[i];
j=i-1;
while(j>=0 && value<arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=value;
}
for(j=0;j<N;j++)
{
printf("%d",arr[j]);
printf(" ");
}
}
int main(void) {
int N;
scanf("%d", &N);
int arr[N], i;
for(i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
insertionSort(N, arr);
return 0;
}
#include <stdio.h>
// Function to calculate the area of a triangle
float calculateArea(float base, float height) {
return (0.5 * base * height);
}
int main() {
float base1, height1, base2, height2, area1, area2;
// Input for the first triangle
printf("Enter the base and height of the first triangle: ");
scanf("%f %f", &base1, &height1);
// Input for the second triangle
printf("Enter the base and height of the second triangle: ");
scanf("%f %f", &base2, &height2);
// Calculate the areas of both triangles
area1 = calculateArea(base1, height1);
area2 = calculateArea(base2, height2);
// Compare the areas and determine which triangle is larger
printf("\nArea of first triangle: %.2f", area1);
printf("\nArea of second triangle: %.2f", area2);
if (area1 > area2) {
printf("\nThe first triangle is larger.\n");
printf("The second triangle is smaller.\n");
} else if (area1 < area2) {
printf("\nThe second triangle is larger.\n");
printf("The first triangle is smaller.\n");
} else {
printf("\nBoth triangles have the same area.\n");
}
return 0;
}
4. Happy Numbers
#include<stdio.h>
int main()
{
int num,sum=0;
printf("Enter the number");
scanf("%d",&num);
while((num!=1)&& (num!=4)){
while(num>0){
sum=sum+((num%10)*(num%10));
num=num/10;
}
num=sum;
sum=0;
}
if(num==1){
printf("Happy Number");
}
else{
printf("not Happy Number");
}
return 0;
}
5. Triangle Numbers
#include <stdio.h>
// Function to calculate the nth triangular number
int triangularNumber(int n) {
return (n * (n + 1)) / 2;
}
int main() {
int n, i;
return 0;
}
MODULE-3
1. For loop in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b,i;
scanf("%d\n%d", &a, &b);
// Complete the code.
for(i=a;i<=b;i++)
{
if(i<10)
{
if(i==1)
printf("one\n");
else if(i==2)
printf("two\n");
else if(i==3)
printf("three\n");
else if(i==4)
printf("four\n");
else if(i==5)
printf("five\n");
else if(i==6)
printf("six\n");
else if(i==7)
printf("seven\n");
else if(i==8)
printf("eight\n");
else if(i==9)
printf("nine\n");
}
else {
if(i%2==1)
printf("odd\n");
else {
printf("even\n");
}
}
}
return 0;
}
2. Calculate Nth term
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
int find_nth_term(int n, int a, int b, int c) {
//Write your code here.
int term, t1 = a, t2 = b, t3 = c;
if (n == 1)
term = t1;
else if (n == 2)
term = t2;
else if (n == 3)
term = t3;
else {
for (int i = 4; i <= n; i++) {
term = t1 + t2 + t3;
t1 = t2;
t2 = t3;
t3 = term;
}
}
return term;
}
int main() {
int n, a, b, c;
scanf("%d %d %d %d", Cn, Ca, Cb, Cc);
int ans = find_nth_term(n, a, b, c);
print("%d", ans);
return 0;
}
#include<stdio.h>
int main() {
int n, i;
float marks, sum = 0;
// Input the marks for each student and calculate the sum
for (i = 1; i <= n; i++) {
printf("Enter marks for student %d: ", i);
scanf("%f", &marks);
sum += marks;
}
// Display the total sum of marks
printf("\nThe sum of marks for %d students is: %.2f\n", n, sum);
return 0;
}
4. Variadic Functions
#include <stdio.h>
#include <stdarg.h>
// Variadic function to calculate the sum of given integers
int sum(int count, ...) {
va_list args;
int total = 0;
return total;
}
int main() {
// Example usage of the sum function
int result1 = sum(3, 10, 20, 30); // Sum of 3 numbers: 10, 20, 30
int result2 = sum(5, 1, 2, 3, 4, 5); // Sum of 5 numbers: 1, 2, 3, 4, 5
// Display the results
printf("The sum of 10, 20, 30 is: %d\n", result1);
printf("The sum of 1, 2, 3, 4, 5 is: %d\n", result2);
return 0;
}
#include <stdio.h>
// Function to calculate the Nth Tribonacci number
int tribonacci(int n) {
if (n == 0) return 0;
if (n == 1 || n == 2) return 1;
int a = 0, b = 1, c = 1, next;
for (int i = 3; i <= n; i++) {
next = a + b + c; // Calculate the next term
a = b; // Update a to the next term
b = c; // Update b to the next term
c = next; // Update c to the next term
}
return c;
}
int main() {
int n;
// Input the value of N
printf("Enter the value of N: ");
scanf("%d", &n);
// Calculate and display the Nth Tribonacci number
printf("The %dth Tribonacci number is: %d\n", n, tribonacci(n));
return 0;
}