PRE-FINAL EXAMINATION
COMPUTER PROGRAMMING 1
Degree/Program: BSIT, BSCS, ACT
1ST SEMESTER S.Y: 2021-2022
Name: Jeva Sanchez BSIT
Test I. WRITING STATEMENTS
Direction: Write for statements that print the following sequences of values: (6 pts each)
1. 1, 2, 3, 4, 5, 6, 7
Answer:
#include <stdio.h>
#include <stdlib.h>
Int main
(int argc, char *argv[])
{
Int I;
For(i=1;i<=7;i=i+1)
Printf(“%d\n”,i);
Return 0;
2. 3, 8, 13, 18, 23
Answer:
#include<stdio.h>
#include<conio.h>
Int main()
{
Int I;
For(i=3;i<=23;i=i+5)
Printf(“%d\n”,i);
Return 0;
}
3. 20, 14, 8, 2, –4, –10
Answer:
#include<stdio.h>
#include<conio.h>
Int main()
{
Int I;
For(i=20;i>=-10;i=i-6)
Printf(“%d\n”,i);
Return 0;
}
4. 19, 27, 35, 43, 51
Answer:
#include<stdio.h>
#include<conio.h>
Int main()
{
Int I;
For(i=19;i<=51;i=i+8)
Printf(“%d\n”,i);
Return 0;
}
5. 10,20,30,40,50
Answer:
#include<stdio.h>
#include<conio.h>
Int main()
{
Int I;
For(i=10;i<=50;i=i+10)
Printf(“%d\n”,i);
Return 0;
}
TEST II. PROGRAMMING (15 points each)
1. (Find the Smallest and largest)Using for loop, write a program that will let you enter 10
integers and will find the smallest and the largest of several integers. Assume that the first value
read specifies the number of values remaining.
Answer:
2. (Modified Diamond Printing Program) Using nested for loop, Write a program that prints the
following diamond shape. You may use printf statements that print either a single asterisk (*) or
a single blank. Maximize your use of repetition (with nested for statements) and minimize the
number of printf statements. Modify the program you wrote to read an odd number in the range 1
to 19 to specify the number of rows in the diamond. Your program should then display a
diamond of the appropriate size. Below is an example of diamond printing.
Answer:
#include<stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system(“pause”) or input
loop */
Int main(int argc, char *argv[])
{
Int num,I,j;
Printf(“Enter number of lines:”);
Scanf(“%d”,&num);
Int stars=1,space=num/2;
While(stars<=num)
{
For(i=0;i<space;i++)
Printf(“ “);
For(j=0;j<stars;j++)
Printf(“*”);
Printf(“\n”);
Stars=stars+2;
Space--;
}
Stars=stars-2;
Space++;
While(stars>=1)
{
For(i=0;i<space;i++)
Printf(“ “);
For(j=0;j<stars;j++)
Printf(“*”);
Printf(“\n”);
Stars=stars-2;
Space++;
}
}