Pakistan Institute of Engineering & Applied Sciences: Q# Questions Marks 1
Pakistan Institute of Engineering & Applied Sciences: Q# Questions Marks 1
: Zero
EXAMINATION
: Mid-Term
TOTAL MARKS : 30
-----------------------------------------------------------------------------------------------------------------------------Instructions (i)
(ii)
----------------------------------------------------------------------------------------------------------------------------- -------------------------------
Q#
1
Questions
Marks
Write a program using conditional operator to find the smallest of the 3 numbers
entered by the user.
#include<stdio.h>
int main()
{
float a,b,c,min;
printf("Enter 1st number:");
scanf("%f",&a);
printf("Enter 2nd number:");
scanf("%f",&b);
printf("Enter 3rd number:");
scanf("%f",&c);
min=(a<b)?a:b;
min=(min<c)?min:c;
printf("The minimum of above 3 entered numbers is %0.2f",min);
getch();
return 0;
}
Write a C program to check whether the year entered by user is a leap year or not.
#include<stdio.h>
int main()
{
int year;
printf("Enter an year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else printf("%d is not a leap year.", year);
}
else printf("%d is a leap year.", year );
}
else printf("%d is not a leap year.", year);
getch();
return 0;
}
Page
of___
Initials ________
Master Solution
Write a C program to display the ASCII value of a character entered by user.
#include <stdio.h>
int main()
{
char var1;
printf("Enter character: ");
scanf("%c",&var1);
printf("You entered %c.\n",var1);
printf("ASCII value of %d",var1);
getch();
return 0;
}
Point out the errors in the following program and rectify them.
Page
2x5
5(a)
5(b)
#include <stdio.h>
int main()
{
int i = 0, j = 0;
for (i; i < 2; i++){
for (j = 0; j < 3; j++){
printf("1\n");
break;
}
printf("2\n");
}
printf("after loop\n");
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i = 0;
do
{
i++;
if (i == 2)
continue;
printf("In while loop ");
} while (i < 2);
printf("%d\n", i);
getch();
return 0;
}
Output
Output
1
2
1
2
after loop
In while loop 2
of___
Initials ________
Master Solution
5(c)
5(d)
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0;
for(i=0;i<10;++i)
{
printf(%d ,i);
if(i==2)
{
i=3;
continue;
}
else if(i==6)
{
break;
}
}
if(i==7)
printf(near the end\n);
else
printf(the end\n);
getch();
return 0;
printf(or is it??);
}
#include <stdio.h>
int main()
{
int k = 8;
int m = 7;
int z = k < m ? k = m : m++;
printf("%d", z);
getch();
return 0;
}
Output
0 1 2 4 5 6 the end
Output
7
5(e)
#include <stdio.h>
#include <conio.h>
int main()
{
int count;
for (count=0;count<12;count++)
{
if(count%2!=0)
printf(" %d \n",count);
}
getch();
return 0;
}
Output
1
3
5
7
9
11
Write a C program which accepts a positive integer N and a real value x and evaluates
cos(x) by truncating the above infinite series after N number of terms. Also compare
the result with built in function cos(x).
#include<stdio.h>
#include<math.h>
int fact();// factorial function
main()
{
float x,series=0.0;
int N,i;
printf("Enter x for which you want to calculate cos(x) ");
scanf("%f",&x);
printf("Enter number of terms in Maclauren Series ");
scanf("%d",&N);
for (i=0;i<=N;i++)
{
series+=pow(-1,i)*pow(x,2*i)/fact(2*i);
}
Page
of___
Initials ________
Master Solution
printf("********************************************\n");
printf("From Series Expansion: cos(%.4f)=%.4f\nFrom Libarary function:
cos(%.4f)=%.4f",x,series,x,cos(x));
printf("\n********************************************\n");
getch();
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//factorial function
int fact(int num)
{
int prod=1;
if(num==0)
return prod;
else
while(num!=1)
{
prod*=num;
num--;
}
return prod;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-------------Good Luck------------....
Page
of___
Initials ________