0% found this document useful (0 votes)
55 views

Pakistan Institute of Engineering & Applied Sciences: Q# Questions Marks 1

The document contains a mid-term exam for a computing fundamentals course with 6 questions. Question 1 asks students to write a program to find the smallest of 3 numbers using a conditional operator. Question 2 asks students to write a program to check if a year entered by the user is a leap year. Question 3 asks students to write a program to display the ASCII value of a character. Question 4 asks students to point out and rectify errors in a given prime number checking program. Question 5 asks students to determine the output of 5 given programs. Question 6 asks students to write a program to evaluate the cosine of a value x by truncating its Maclaurin series expansion after N terms and compare the result to the built-
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Pakistan Institute of Engineering & Applied Sciences: Q# Questions Marks 1

The document contains a mid-term exam for a computing fundamentals course with 6 questions. Question 1 asks students to write a program to find the smallest of 3 numbers using a conditional operator. Question 2 asks students to write a program to check if a year entered by the user is a leap year. Question 3 asks students to write a program to display the ASCII value of a character. Question 4 asks students to point out and rectify errors in a given prime number checking program. Question 5 asks students to determine the output of 5 given programs. Question 6 asks students to write a program to evaluate the cosine of a value x by truncating its Maclaurin series expansion after N terms and compare the result to the built-
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Master Solution

Pakistan Institute of Engineering & Applied Sciences


SEMESTER

: Zero

MS/Mphil SESSION: 2013-15

CODE / COURSE :CF/Computing Fundamentals


TIME ALLOWED : 60 Mins

EXAMINATION
: Mid-Term
TOTAL MARKS : 30
-----------------------------------------------------------------------------------------------------------------------------Instructions (i)

(ii)

Read the instructions given on the Answer Book carefully.


Write your MS-ID on the question paper.

----------------------------------------------------------------------------------------------------------------------------- -------------------------------

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.

// prime.c (or /*prime.c*/)


#include <stdio.h>
#include<process.h> (for exit(0))
int main() (No semicolon here))
{
int n, j;
printf( "Enter a number: ");
scanf("%d",&n); (Ampersand missing)
for(j=2; j <= n/2; j++)
if(n%j == 0) (should be relational operator not assignment operator)
{
printf("Its not prime; divisible by %d \n",j);
getch();
exit(0);
}
printf("Its prime\n");
getch();
return 0;
}

Page

What would be the output of the following programs?

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

The Maclaurin series expansion for cos(x) is given by:

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 ________

You might also like