Introduction To Programming Exam
Introduction To Programming Exam
15/02506.
BBIT.
EVENING.
RUARAKA CAMPUS.
INTRODUCTION TO PROGRAMMING.
15/02506
QUESTION ONE.
#include <stdio.h> /* it is stdio.h not studio.h, stdio. h is the header file for standard input and
output. This is useful for getting the input from the user(Keyboard) and output result text to the
monitor(screen).*/
int main() {
char StudentName [100]; /*variable definition consists of only letters,digits or the underscore
character and the first letter must be an alphabet*/
float ExamValue, Sum,Avg; /* the semicolon is a statement terminator by default, statements in C
and C++ can have any number of lines So the semicolon tells C that the statement has been
terminated, and whatever follows is a new statement */
int students,exams;
for (students= 0; students < 5; students++){
Sum=0.0;
printf("Enter student name \n");
scanf("%c", StudentName); /* format specifier %c for character StudentName*/
for (exams = 0; exams < 3; exams++){ /* Exams is not a template, a nested loop for exams*/
printf("Enter exam grade: \n");
scanf("%f",& ExamValue);
Sum = Sum + ExamValue; /* semicolon is a statement terminator*/
}
Avg = Sum / 3.0; /* semicolon is a statement terminator*/
printf("Average for %c is %f\n", StudentName,Avg); /* format specifier%C for studentname*/
}
return 0;
}
15/02506
Question one B
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int n;
int d;
} fraction;
int main()
{
int n1, d1;
int n2, d2;
printf("Enter fraction 1 (numerator and denominator seperated by space):");
scanf("%d%d",&n1,&d1);
printf("Enter fraction 2 (numerator and denominator seperated by space):");
scanf("%d%d",&n2,&d2);
fraction f1={n1,d1};
fraction f2={n2,d2};
fraction r={f1.n*f2.d+f2.n*f1.d, f1.d*f2.d};
printf("Result=%d/%d", r.n, r.d); printf("\n");
return 0;
15/02506
QUESTION TWO
A.
#include <stdio.h>
int main(){
int math,physics,chemistry;
printf("Enter marks for math");
scanf("%d",&math);
printf("Enter marks for physics");
scanf("%d",&physics);
printf("Enter marks for chemistry");
scanf("%d",&chemistry);
if(math >= 80){
printf("Result : PASS ");
}
else if ( physics >=75){
printf("Result : PASS");
}
else if ( chemistry >=70){
printf("Result : PASS");
}
else {
printf("Result : Fail");
}
return 0;
}
15/02506
QUESTION TWO C
#include<stdio.h>
int main()
{
int x,y,s,p,total;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
p=x*y;
s=x*y;
total=(s*s)+p*(p+y);
printf("%d",total);
return 0;
}