0% found this document useful (0 votes)
23 views6 pages

C Midsem Solutions

Uploaded by

v create for u
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)
23 views6 pages

C Midsem Solutions

Uploaded by

v create for u
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/ 6

1.

)
#include <stdio.h>

int main() {
int choice,exit_flag=0;
int a, b;
unsigned long long factorial;
while (1) {
// Step 1: Display menu options
printf("\nMenu:\n");
printf("a) Enter 1 to Find the factorial of a number\n");
printf("b) Enter 2 to Display the remainder on dividing two numbers\n");
printf("c) Enter 3 to Display the quotient on dividing two numbers\n");
printf("d) Enter 4 to Find whether the given number is prime or not.\n");
printf("e) Enter 0 to exit from the menu.\n");
printf("Enter your choice: ");

// Step 2: Read the input choice from the user


scanf("%d", &choice);

// Step 3: Perform operations based on choice


// Repeat until user choice is 0
switch (choice) {
case 1:
factorial=1;
printf("Enter a number: ");
scanf("%d", &a);
for (int i=1;i<=a;i++)
factorial*=i;
printf("%llu",factorial);
break;

case 2:
printf("Enter two numbers separated by space: ");
scanf("%d %d", &a, &b);
printf("Remainder of %d / %d = %d\n", a, b, a % b);
break;

case 3:

printf("Enter two numbers separated by space: ");


scanf("%d %d", &a, &b);
printf("Quotient of %d / %d = %d\n", a, b, a / b);
break;
case 4:

int flag=0;
printf("Enter a number: ");
scanf("%d", &a);
if ((a==0) || (a==1)){
printf("non prime");
break;}
else if ((a==2 || a==3)){
printf("Prime");
break;
}
for(int i=2;i*i<=a;i++)
{
if (a%i==0){
printf("non prime");
flag=1;
break;
}

}
if (flag==0)
printf("prime");
break;
case 0:
exit_flag = 1;
printf("Exiting the menu.\n");
break;

default:
printf("Invalid choice!\n");
break;
}
if(exit_flag==1){
break;
}

return 0;
}
2.)

#include <stdio.h>

int main() {
int x1, y1, x2, y2, x3, y3;
int a, b, c, result;

// Input two points (x1, y1) and (x2, y2)


printf("Enter point (x1, y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter point (x2, y2): ");
scanf("%d %d", &x2, &y2);

// Calculate coefficients a, b, and c


a = y1 - y2;
b = x2 - x1;
c = x1*y2 - x2*y1;

printf("The straight line equation is: %dx + (%d)y + (%d) = 0\n", a, b, c);

// Read point (x3, y3)


printf("Enter point (x3, y3) to check its position relative to the line: ");
scanf("%d %d", &x3, &y3);

// Check the position of the point


result = a*x3 + b*y3 + c;
if (result > 0) {
printf("The point (x3, y3) is above the line.\n");
} else if (result < 0) {
printf("The point (x3, y3) is below the line.\n");
} else {
printf("The point (x3, y3) is on the line.\n");
}

return 0;
}

3.)

845

4.)
#include <stdio.h>

int main() {
int n, num;
int largest = -32768; // take possible min value based on the data type.
int secondLargest = -32768; // take possible min value based on the data type.

printf("Enter the total number of numbers: ");


scanf("%d", &n);

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

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


scanf("%d", &num);

// If current number is greater than largest


if (num > largest) {
// Then the current largest becomes second largest
secondLargest = largest;
// And the current number becomes largest
largest = num;
}
// If current number is in between largest and second largest
else if (num > secondLargest && num != largest) {
// Then the current number becomes second largest
secondLargest = num;
}
}

if (secondLargest == -32768) {
printf("There is no second largest number or all numbers are same.\n");
} else {
printf("The second largest number is: %d\n", secondLargest);
}

return 0;
}

5.)

5,6 and GCD


6. )

#include <stdio.h>

int main() {
int sum=0,val;
printf("enter the elements of 5*5 matrix");
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
scanf("%d",&val);
if (i==j){
sum+=val;
}
}
}
printf("%d",sum);
return 0;
}

7. )

#include <stdio.h>

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

int result = 0, multiplier = 1;


while (num > 0) {
if (num % 10 != 0) {
result += (num % 10) * multiplier;
multiplier *= 10;
}
num /= 10;
}
printf("%d",result);
}

You might also like