0% found this document useful (0 votes)
3 views7 pages

Cse115 Assignment

The document contains a C programming assignment by Nneji Ifeanyi Daniel from the Computer Engineering department, which includes five programming tasks. The tasks involve converting days into years, weeks, and days; calculating the distance between two points; checking if three values can form a triangle and calculating its perimeter; counting age categories from user input; and implementing a number guessing game with limited attempts. Each task is accompanied by its corresponding program code.

Uploaded by

ahmaad.nova
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)
3 views7 pages

Cse115 Assignment

The document contains a C programming assignment by Nneji Ifeanyi Daniel from the Computer Engineering department, which includes five programming tasks. The tasks involve converting days into years, weeks, and days; calculating the distance between two points; checking if three values can form a triangle and calculating its perimeter; counting age categories from user input; and implementing a number guessing game with limited attempts. Each task is accompanied by its corresponding program code.

Uploaded by

ahmaad.nova
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

NAME: Nneji Ifeanyi Daniel

DEPARTMENT: Computer Engineering

MATRIC NO: 19/ENG02/077

COURSE TITLE: Structured Programming

COURSE CODE: ENG 224

C PROGRAMMING ASSIGNMENT

1. Write a C program to convert 1343 days into years, weeks and days (Note: Ignore leap year).

Program Code:

1. #include<stdio.h>
2.
3. void main()
4.
5. {
6.
7. int nodays,years, weeks,days;
8.
9. printf("Enter the total days\n");
10.
11. scanf("%d",&nodays);
12.
13. years=nodays/365;
14.
15. weeks=(nodays%365)/7;
16.
17. days=(nodays%365)%7;
18.
19. printf("%d = %d years,%d weeks,%d days\n",nodays,years,weeks,days);
20.
21. }

Output:
2. Write a C program to calculate the distance between the two points. Note: x1, y1, x2, y2 are all
double values.

Program Code:

1. #include <stdio.h>
2. #include <math.h>

3. int main() {
4. float x1, y1, x2, y2, gdistance;
5. printf("Input x1: ");
6. scanf("%f", &x1);
7. printf("Input y1: ");
8. scanf("%f", &y1);
9. printf("Input x2: ");
10. scanf("%f", &x2);
11. printf("Input y2: ");
12. scanf("%f", &y2);
13. gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
14. printf("Distance between the said points: %.4f", sqrt(gdistance));
15. printf("\n");
16. return 0;
17. }

Output:
3. Write a C program that reads three floating values and check if it is possible to make a triangle with
them. Also, calculate the perimeter of the triangle if the said values are valid.

. 3 .
Program Code:

1. #include <stdio.h>
2. int main() {
3. float x, y, z, P, A;
4. printf("\nInput the first number: ");
5. scanf("%f", &x);
6. printf("\nInput the second number: ");
7. scanf("%f", &y);
8. printf("\nInput the third number: ");
9. scanf("%f", &z);

10. if(x < (y+z) && y < (x+z) && z < (y+x))
11. {
12. P = x+y+z;
13. printf("\nPerimeter = %.1f\n", P);

14. }
15. else
16. {
17. printf("Not possible to create a triangle..!");
18. }
19. }

Output:
4. Write a C program to read age of 20 people and count total baby age, school age and adult age.
Hint:
a) Still a baby- age 0 to 4
b) Attending school - age 5 to 17
c) Adult life-age 18 & over
[Using while loop]

Program Code:

1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. int cnt_baby=0,cnt_school=0,cnt_adult=0;
6. int count=0;

7. while(count<15)
8. {
9. printf("Enter age of person [%d]: ",count+1);
10. scanf("%d",&age);

11. if(age>=0 && age<=5)


12. cnt_baby++;
13. else if(age>=6 && age<=17)
14. cnt_school++;
15. else
16. cnt_adult++;

17. //increase counter


18. count++;
19. }
20. printf("Baby age: %d\n",cnt_baby);
21. printf("School age: %d\n",cnt_school);
22. printf("Adult age: %d\n",cnt_adult);

23. return 0;
24. }

Output:

5. Write a C-program to read a random number and then ask user to guess it (from 0 to 100).

Hint:

User guess correct number, which is to be generated randomly. The program will give 7 attempts to the
user. On each attempt, program will inform the user that entered number is less than or greater than
the random generated number.

Program Code:

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <time.h>

4. int main()
5. {
6. int random_genNo=0,count=0,num;
7. int stime;
8. long ltime;
9. //initialise srand with current time, to get random number on every run
10. ltime = time(NULL);
11. stime = (unsigned) ltime/2;
12. srand(stime);

13. //generate random number


14. random_genNo=rand()%100;

15. //run infinite loop


16. while(1)
17. {
18. //increase counter
19. count+=1;

20. //read number from user


21. printf("\n\nGuess a number from (0 to 100): ");
22. scanf("%d",&num);

23. //compare entered number with generated number

24. if(random_genNo==num){
25. printf("You have guessed a correct number.");
26. break;
27. }
28. else if(random_genNo<num){
29. printf("Generated number is less than entered number, try again...");
30. }
31. else if(random_genNo>num){
32. printf("Generated number is greater than entered number, try again...");
33. }

34. if(count==7){
35. printf("\n\n### Maximum limit of attempt reached\n");
36. break;
37. }
38. }

39. return 0;
40. }
Output:

You might also like