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

DATABASE ASSIGNMENT 1& 2

Database
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DATABASE ASSIGNMENT 1& 2

Database
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OPEN UNIVERSITY OF TANZANIA

COURSE NAME: BACHELOR OF SCIENCE IN INFORMATION


COMMUNICATION TECHNOLOGY

WORK: PRACTICAL ASSIGNMENT ONE &TWO

SUBJECT NAME: PROGRAMING IN C

COURSE CODE: OIT 136

STUDENT NAME: THERESIA TEMBA

REGISTRATION NUMBER: U23-516-0111


QN.1. Write a program in C to display the first 25 natural numbers.

Answer

#include <stdio.h>

int main() {

// Declare a variable to store the current natural number

int i;

// Iterate from 1 to 25 to print the natural numbers

for (i = 1; i <= 25; i++) {

// Print the current natural number

printf("%d ", i);

// Print a newline character to separate the output

printf("\n");

return 0;

QN.2. Write a program in C to display the cube of the number up to given an integer.

#include <stdio.h>

int main() {

// Declare variables to store the given integer and the current cube

int n, cube;

// Prompt the user to enter the integer

printf("Enter an integer: ");

scanf("%d", &n);

// Iterate from 1 to the given integer to calculate and print cubes

for (int i = 1; i <= n; i++) {


// Calculate the cube of the current number

cube = i * i * i;

// Print the cube of the current number

printf("%d^3 = %d\n", i, cube);

return 0;

QN.3. Write the following code using recursion:

#include <stdio.h>

int main(){

int i;

for (i = 0; i<10; i++){

printf("Hello\n");

return 0;

Answer

#include <stdio.h>

void printHello(int count) {

// Base case: If count reaches 10, stop recursion

if (count == 10) {

return;

// Print "Hello" and recursively call the function with incremented count

printf("Hello\n");
printHello(count + 1);

int main() {

// Start the recursive process with count 1

printHello(1);

return 0;

QN.4.
What would be the output of:

#include <stdio.h>

display(){

printf("C\n");

main();

int main(){

printf("Hey\n");

display();

return 0;

Answer

Hey

C ... (infinite loop)

QN.5.USING CONDITION STATEMENT WRITE THE C PROGRAMMING CODE ON THE FOLLOWING


a. A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
suppose, one unit will cost 100.
Judge and print total cost for user.
b. A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.

Answer

a. Total cost calculation with discount

#include <stdio.h>

int main() {

// Declare variables for quantity, unit price, total cost, and discount

int quantity;

float unit_price = 100.0, total_cost, discount;

// Prompt the user to enter the quantity

printf("Enter the quantity: ");

scanf("%d", &quantity);

// Calculate the total cost

total_cost = quantity * unit_price;

// Check if the total cost is more than 1000 and apply the discount

if (total_cost > 1000) {

discount = total_cost * 0.1;

total_cost -= discount;

// Print the total cost

printf("Total cost: %.2f\n", total_cost);

return 0;

}
b. Grading system based on marks

#include <stdio.h>

int main() {

// Declare variables for marks and grade

int marks;

char grade;

// Prompt the user to enter the marks

printf("Enter the marks: ");

scanf("%d", &marks);

// Determine the grade based on the marks

if (marks < 25) {

grade = 'F';

} else if (marks < 45) {

grade = 'E';

} else if (marks < 50) {

grade = 'D';

} else if (marks < 60) {

grade = 'C';

} else if (marks < 80) {

grade = 'B';

} else {

grade = 'A';

// Print the grade

printf("Grade: %c\n", grade);

return 0;
}

You might also like