Cse115 Assignment
Cse115 Assignment
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);
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);
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: