C Midsem Solutions
C Midsem Solutions
)
#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: ");
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:
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;
printf("The straight line equation is: %dx + (%d)y + (%d) = 0\n", a, b, c);
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.
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.)
#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);