C++ Unit 1-Qb With Ans
C++ Unit 1-Qb With Ans
PART A
1
5. What do you mean by variables in ‘C’?
A variable is an identifier that is used to represent some specified type of
information.
Syntax :
data_typevariable_name;
Example: intmarks;
Here we are not updating the value of i. so after each iteration value of
i remains same. As a result, the condition (i<10) will always true so it will
print infinity loop.
a=10;
main( )
{
print(“Value of a : %d”,a);
}
No Break Continue
i. Takes the control to outside of Takes control to the beginning of the
the loop. loop.
int marks[6];
Syntax :data_typearray_name[row_size][column_size];
Quadratic Equation:
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex number.\n");
return 0;
}
else if(d==0){
printf("Both roots are equal.\n");
return 0;
}
else{
printf("Roots are real numbers.\n");
return 0;
}
OUTPUT:
Enter a, b and c of quadratic equation: 2 4 1
Roots are real numbers.
Roots of quadratic equation are: -0.293, -1.707
FACTORIAL:
#include <stdio.h>
int main()
{
int c, n, fact = 1;
return 0;
}
OUTPUT:
Enter a number to calculate its factorial: 6
Factorial of 6: 720
#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
8. What is an array? Discuss how to initialize a one dimensional and two dimensional
arrays with suitable example? .(Pg.No:183-202)
Part C
1.i). Write a C Program to remove the duplicate numbers present in an array & display the
remaining numbers.
#include<stdio.h>
int main() {
int arr[20], i, j, k, size;
return (0);
}
output:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
printf("Enter first matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter second matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat2[i][j]);
}
}
printf("Multiplying two matrices...\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
{
sum = sum + mat1[i][k] * mat2[k][j];
}
mat3[i][j] = sum;
}
}
printf("\nMultiplication of two Matrices : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ", mat3[i][j]);
}
printf("\n");
}
getch();
}
Output:
2. i)State the need for user define functions. Explain call by value & call by reference
methods using examples.
C allow programmers to define functions. Such functions created by the user are
called user-defined functions.
Need of User defined functions:
Every program must have a main function.
It is possible to code any program utilizing only main function,it leads to a number
of problems.
The program may become too large and complex and as a result the task of
debugging ,testing and maintaining becomes difficult.
if a program is divided into functional part ,then each part may be independently
coded and later combined into single unit.
these subprograms called “functions” are much easier to understand,debug & test.
there are times when some types of operation or calculation is repeated at many
points throughout a program.
in such,situations,we may repeat the program statements whenever they are
needed.
another approach is to edsign a function that can be called and used whenever
required.
this saves both time & space.
Call by value
In call by value, original value can not be changed or modified. In call by value, when you
passed value to the function it is locally stored by the function parameter in stack memory
location. If you change the value of function parameter, it is changed for the current function
only but it not change the value of variable inside the caller method such as main().
#include<stdio.h>
#include<conio.h>
void main()
{
int a=100, b=200;
clrscr();
swap(a, b); // passing value to function
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
}
OUTPUT:
Value of a: 200
Value of b: 100
Call by reference
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a=100, b=200;
clrscr();
swap(&a, &b); // passing value to function
printf("\nValue of a: %d",a);
printf("\nValue of b: %d",b);
getch();
}
OUTPUT:
Value of a: 200
Value of b: 100
3.i).Write a C program to enter marks of five subjects and calculate total, average and
percentage.
#include <stdio.h>
void main()
{
float sub1, sub2, sub3, sub4, sub5;
float tot, avg, per;
ii). write a C program to read a character from keyboard and print it in reverse case.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[50],b[50];
int i,j,n,totword=1,totchar=0,totline=1;
clrscr();
printf("\n Please Give The STRING : ");
scanf("%s",a);
for(j=strlen(a)-1;j>=0;j--)
printf("%c",a[j]);
getch();
}
OUTPUT:
Please Give The STRING : SYNTAX
XATNYS
#include<stdio.h>
#include<conio.h>
UNION emp
{
int empno ;
char name[10] ;
int bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,
e[i].name, e[i].bpay, e[i].allow, e[i].ded, e[i].npay) ;
}
getch() ;
}
Output:
int main()
{
int days, years, weeks;
/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
return 0;
}
OUTPUT:
ENTER DAYS: 373
YEARS:1
WEEKS: 1
DAYS:1
ii). What are the advantages of using default arguments? Explain with example
program.
Advantages:
Time complexity will be reduced.
Efficiency of program execution is fast.
Reduces complex coding.
#include <iostream>
using namespace std;
//Default argument must be trailer.
int sum(int x, int y=10, int z=20)
{
return (x+y+z);
}
int main()
{
cout << "Sum is : " << sum(5) << endl;
cout << "Sum is : " << sum(5,15) << endl;
cout << "Sum is : " << sum(5,15,25) << endl;
return 0;
}
Output:
Sum is :35
Sum is :40
Sum is :45