0% found this document useful (0 votes)
25 views15 pages

solutions

cpp qustions

Uploaded by

thesandipsunar
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)
25 views15 pages

solutions

cpp qustions

Uploaded by

thesandipsunar
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/ 15

/*

1. Define a function which receives a number as argument and returns 1 if the number is prime
otherwise returns 0. Use this function in main function to display all prime numbers between two
numbers.

#include<stdio.h>

// Function to check if a number is prime

int isprime(int n) {

if (n <= 1) return 0; // 0 and 1 are not prime numbers

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

return 0; // Not prime if divisible by i

return 1; // Prime number

int main() {

int start, end, i;

// Input the range

printf("Enter start number: ");

scanf("%d", &start);

printf("Enter end number: ");

scanf("%d", &end);
printf("Prime numbers between %d and %d are: ", start, end);

// Loop through the range and print prime numbers

for (i = start; i <= end; i++) {

if (isprime(i)) { // Check if the number is prime

printf("%d ", i); // Print the prime number

printf("\n");

return 0;

*/

/*

2. Write a program which asks order of two matrices and read two matrices of given order.
Subtract second matrix from first and display the resultant matrix.

#include<stdio.h>

int main()

int i, j, m, n;

printf("enter the value of m:\n");

scanf("%d", &m);
printf("enter the value of n:\n");

scanf("%d", &n);

printf("the required order of the matrix is: %d*%d\n", m, n);

int mat1[m][n], mat2[m][n], result[m][n];

//for matrix1

printf("enter the values for mat1:");

for(i=0; i<m; i++)

for(j=0; j<n; j++)

printf("enter values[%d][%d]\n", i+1, j+1);

scanf("%d", &mat1[i][j]);

printf("the values are: %d \n", mat1[i][j]);

//for matrix2

printf("enter the values for mat2:");

for(i=0; i<m; i++)

for(j=0; j<n; j++)

{
printf("enter values[%d][%d]\n", i+1, j+1);

scanf("%d", &mat2[i][j]);

printf("the values are: %d \n", mat2[i][j]);

//for result

for(i=0; i<m; i++)

for(j=0; j<n; j++)

result[i][j]=mat1[i][j]-mat2[i][j];

printf("the result is: %d \n", result[i][j]);

return 0;

//3. Write a program to read a string from user and reverse it without using string related library
function.

#include<stdio.h>

#include<stdlib.h>
int reverse(char str[], char reversestr[]){

int i, len=0;

while(str[len]!='\0')

len++;

for(i=0; i<len; i++)

reversestr[i]= str[len-i-1];

reversestr[len]='\0';

int main()

char str[100], reversestr[100];

printf("enter the string:\n");

scanf("%s", str);

//function call

reverse(str, reversestr);

printf("the reversed string is: %s ", reversestr);

return 0;

}
4. Write a program that determine the largest of three numbers using pointer.

#include<stdio.h>

int largest(int *n1, int *n2, int *n3, int *large)

if(*n1>*n2 && *n1>*n3)

*large= *n1;

else if(*n2>*n1&& *n2>*n3)

*large= *n2;

else

*large=*n3;

int main()

{
int n1, n2, n3, large;

printf("enter three numbers:");

scanf("%d %d %d", &n1, &n2, &n3);

largest(&n1, &n2, &n3, &large);

printf("The largest number is: %d", large);

return 0;

5. Write a program to read 10 numbers and reorders them in ascending order. Define separate
function for reading , sorting and displaying array elements.

#include<stdio.h>

void reading(int arr[], int size)

printf("enter %d numbers ", size);

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

scanf("%d", &arr[i]);

void sorting(int arr[], int size)

for(int i=0; i<size-1; i++)

{
for(int j=0; j<size-i-1; j++)

if(arr[j]>arr[j+1])

int temp= arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

void displaying(int arr[], int size)

printf(" the numbers in ascending order are:\n");

for(int i=0; i<size; i++)

printf("%d", arr[i]);

printf("\n");

int main()

int numbers[10];

int size=10;
reading(numbers, size);

sorting(numbers, size);

displaying(numbers, size);

return 0;

6. Define two functions getArraynum() to read an array from user and determine the greatest
number respectively. Write main program to declare as floating point type array of size 10 and pass this
array to first function getarraynum() to read its elements and then pass the same array to another
function greatestnum() to determine the greatest number among elements of the array.

#include<stdio.h>

void getarraynum(float arr[], int size)

printf("Enter %d float numbers", size);

for(int i=0; i<size; i++)

scanf("%f", &arr[i]);

float greatestnum(float arr[], int size)


{

float max= arr[0];

for(int i=1; i<size; i++)

if(arr[i]>max)

max= arr[i];

return max;

int main()

float numbers[10];

int size=10;

getarraynum(numbers, size);

float greatest= greatestnum(numbers, size);

printf("the greatest number is: %f", greatest);

return 0;

7. Create a structure named student that has name, roll, marks and remarks as members. Assume
appropriate types and size of member. Use this structure to read and display records of 4 students.
Create two functions: one is to read information of students and other is to display the information. Pass
array of structure to these functions.

#include <stdio.h>

#define SIZE 4 // Correct macro definition

// Structure definition for student

struct student {

char name[20]; // Name of the student

int roll; // Roll number of the student

float marks; // Marks obtained by the student

char remarks[100]; // Remarks (changed to an array for proper input)

};

// Function to read student information

void read(struct student stu[]) {

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

printf("Enter the information of student %d\n", i + 1);

printf("Enter name: ");

scanf("%s", stu[i].name); // Use %s to read strings

printf("Enter roll number: ");

scanf("%d", &stu[i].roll); // Read the roll number

printf("Enter marks: ");

scanf("%f", &stu[i].marks); // Read the marks

printf("Enter remarks: ");

scanf(" %[^\n]", stu[i].remarks); // Read remarks, allowing spaces


}

// Function to display student information

void display(struct student st[]) {

printf("Student Name\tRoll\tMarks\tRemarks\n");

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

printf("%s\t\t%d\t%.2f\t%s\n", st[i].name, st[i].roll, st[i].marks, st[i].remarks);

int main() {

struct student s[SIZE]; // Declare an array of student structures

printf("Student information\n");

read(s); // Call to read student data

printf("Detail information:\n");

display(s); // Call to display student data

return 0;

8. Define a class named distance with meter and cm as private data members and appropriate
function members. Use this class to read two objects of the distance class , add them by passing these
two objects to a function members and finally display result object in main() function.

#include<iostream>
using namespace std;

class distance

private:

int meter;

int cm;

public:

getvalue()

cout<<"enter distance in meter:";

cin>>meter;

cout<<"enter distance in cm";

cin>>cm;

cout<<"the distance in meter and cm are:" <<meter<<"and" <<cm;

}; incomplete one

9. Define a class shape with dim1 and dim2 as private members. Create two constructors of the
class, one with one argument and other with two arguments. Write a main() program to define object
rectangle with two dimensions and another object square with only one dimension using appropriate
constructor and calculate their area.

*/

#include<iostream>

using namespace std;


class shape

private:

int dim1, dim2;

public:

shape(int side):dim1(side), dim2(side)

shape(int length, int breadth): dim1(length), dim2(breadth)

int area()

return dim1*dim2;

};

int main()

shape rectangle(5,6);

cout<<"the area of rectangle is"<<rectangle.area()<<endl;

shape square(4);
cout<<"The area of square is:"<<square.area()<<endl;

return 0;

You might also like