C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

SAMPLE QUESTION -2080

C-PROGRAMMING SOLUTION 

/*5.A.C. Write a program in C to display the Armstrong numbers between given ranges P and Q. [Hint:
An Armstrong number is a number that is equal to the sum of digits raised to the power
total number of digits in the number, e.g. 1634 = 1
4+64+34+44
 = 1+1296+81+256 = 1634]*/

#include <math.h>
#include <stdio.h>
int main() {
  int low, high, number, originalNumber, rem, count = 0;
  double result = 0.0;
  printf("Enter two numbers(intervals): ");
  scanf("%d %d", &low, &high);
  printf("Armstrong numbers between %d and %d are: ", low, high);

 
  for (number = low + 1; number < high; ++number) {
    originalNumber = number;

    // number of digits calculation
    while (originalNumber != 0) {
      originalNumber /= 10;
      ++count;
    }

    originalNumber = number;

    // result contains sum of nth power of individual digits
    while (originalNumber != 0) {
      rem = originalNumber % 10;
      result += pow(rem, count);
      originalNumber /= 10;
    }

    // check if number is equal to the sum of nth power of individual digits
    if (result == number) {
      printf("%d ", number);
    }

    // resetting the values
    count = 0;
    result = 0;
  }

  return 0;
}

5.B.

#include <stdio.h>
 
void main(){
  int rows, i, j;
  printf("Enter Number Of Rows You Want: ");
  scanf("%d", &rows);
 
  for(i=rows; i>=1; i--) {
    for(j=rows; j>=i; j--) {
      printf("%d ", i);
    }
    printf("\n");
  }
}

6.A.C 

#include <stdio.h>

#define MAX_ROWS 100
#define MAX_COLS 100

int main() {
    int rows, cols,i,j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    if (rows <= 0 || cols <= 0) {
        printf("Invalid matrix size.\n");
        return 1;
    }

    int matrix[MAX_ROWS][MAX_COLS];

    printf("Enter the elements of the matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    int largest , secondLargest;

    // Find the largest and second largest elements
    for ( i = 0; i < rows; i++) {
        for ( j = 0; j < cols; j++) {
            if (matrix[i][j] > largest) {
                secondLargest = largest;
                largest = matrix[i][j];
            } else if (matrix[i][j] > secondLargest && matrix[i][j] != largest) {
                secondLargest = matrix[i][j];
            }
        }
    }

    // Check if there is a second largest element
    
        printf("The second largest element is: %d\n", secondLargest);


    return 0;
}

6.B.C.

//Write a program to find the length of a string without using string handling function.
#include <stdio.h>
int main() {
    char s[] = "Programming is fun";
    int i;

    for (i = 0; s[i] != '\0'; ++i);
    
    printf("Length of the string: %d", i);
    return 0;
}

7.C. 

/*Write a program in C to find the sum of
N even natural numbers divisible by 5. The value of N must be taken from the main
function and passed to a function named findsum that calculates the sum and the sum must
be displayed from the main*/
#include<stdio.h>
int main()
{
    int num,s;
    printf("enter the value of n\n");
    scanf("%d",&num);
    s=findsum(num);
    printf("the sum=%d",s);
}
int findsum(int n)
{
    int i,sum=0;

    for(i=1;i<=n;i++)
    {
        if(i%2==0 && i%5==0)
        {
            sum=sum+i;
        }
    }
    return sum;
}

8.C.

/*Write a program to read and store campus details such
as name, address, established_year, and no_of_students in a structure. Input data for 4
campuses. Pass the structure variable to a function and print the names of the campus which
has more than 1000 students*/
#include<stdio.h>
    struct date
    {
        int day; 
        int month;
        int year;
    };
    struct Campus
    {
        char name[100];
        char address[100];
        int no_students;
        struct date d;
    }c[4];
    int  i;
    void display(struct Campus c[])
{
    for(i=0;i<4;i++)
    {
    
    if(c[i].no_students>1000)
    {
        printf("%s\n",c[i].name);
    }
}
}
int main()
{

    printf("enter the information of four campuses\n");
    for(i=0;i<4;i++)
    {
        printf("enter name of campus %d\n",i+1);
        scanf("%s",c[i].name);
        printf("enter no of students of campus %d\n",i+1);
        scanf("%d",&c[i].no_students);
        printf("enter address of campus %d\n",i+1);
        scanf("%s",c[i].address);
        printf("enter established year of campus %d\n",i+1);
        printf("enter established day of campus %d\n",i+1);
        scanf("%d",&c[i].d.day);
        printf("enter established month of campus %d\n",i+1);
        scanf("%d",&c[i].d.month);
        printf("enter established year of campus %d\n",i+1);
        scanf("%d",&c[i].d.year);
    }
    display(c);
}

9.C. 

/*Write a program to read any string and write it to a file. Read the contents from the file and
separate the uppercase and lowercase letters into two different files*/
#include<stdio.h>
#include<string.h>
int main()
{
    FILE *fp,*fpu,*fpl;
    char str[100],ch;
    fp=fopen("abc.txt","w+");
    if(fp==NULL)
    {
        printf("file cannot be opened");
    }
    fpu=fopen("upper.txt","w");
    if(fp==NULL)
    {
        printf("file cannot be opened");
    }
    fpl=fopen("lower.txt","w");
    if(fp==NULL)
    {
        printf("file cannot be opened");
    }
    printf("enter the string\n");
    gets(str);
    printf("write the string to file\n");
    fputs(str,fp);
    rewind(fp);
    printf("reading the string from file\n");
    while((ch=fgetc(fp))!=EOF)
    {
        if(isupper(ch))
        {
            printf("%c",ch);
            fputc(ch,fpu);
        }
        else 
        {
            fputc(ch,fpl);
        }
    }
    fclose(fp);
    fclose(fpl);
    fclose(fpu);
    
}