Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab No.

3 (Basic)

Name: YOGESH PARTH Dept: Avionics

Roll No. SC11B128 Sub: C Programming Lab

Question 1: Program to find whether year entered through keyboard is leap year or not. Program code: #include<stdio.h> #include<conio.h> void main() { int year; clrscr(); printf("Enter a year:\n"); scanf("%d",&year); if ((year%100 != 0 && year%4 == 0)||(year%100 == 0 && year%400 == 0)) { printf("It is a Leap year"); } else { printf("It is not a leap year"); } getch(); } Result: When you enter a year it will display it is leap year or not. For example:Output1:1900 is not a leap year. Output2: 2016 is a leap year. Discussion: Using if else statements and concept of leap year , programming is done to detect a leap year.

Question 2: Program to check the type of character, whether it is digit, capital letter, small letter or a special character. Program code: #include<stdio.h> #include<conio.h> void main() { int x;

clrscr(); printf("Enter charachter\n"); x = getchar(); if (x>=48 && x<=57) { printf("It is a digit"); } else if (x>=65 && x<= 90) { printf("It is a Capital letter"); } else if (x>=97 && x<= 122) { printf("It is a Small letter"); } else { printf("It is a special character"); } getch(); } Result: When you enter a character it will display result. For example: 1. Enter the character F It is capital letter. 2. Enter the character * It is a special character. Discussion: getch(), getchar() functions, is used . The range of values of various characters were taken into account.

Question 3: Program to find the real roots of a quadratic equation with appropriate error messages. Program code: #include<stdio.h> #include<conio.h> #include<math.h> Void main() { float a,b,c,discriminant,root1,root2; printf("input values of a,b,and c\n");

scanf("%f %f %f",&a,&b,&c); discriminant=b*b-4*a*c; if(discriminant<0) printf("\n\nroots are imaginary\n"); else { root1=(-b+sqrt(discriminant))/(2.0*a); root2=(-b-sqrt(discriminant))/(2.0*a); printf("\n\nroot1=%5.2f\n\nroot2=%5.2f\n",root1,root2); } getch(); } Result: When you enter the value of a, b, c it will display the result as follow: 1. Input values of a, b, and c 123 Roots are imaginary. 2. Input value of a, b, and c 144 Root 1= -2.00 Root 2= -2.00 Discussion: Various cases of the roots of a quadratic equation were taken into consideration using if else statements. Question 4: Program to reverse a 3 digit number and check whether it is palindrome or Not. Program code: #include<stdio.h> #include<conio.h> Void main() { int no,a,b,c,sum; printf("Enter the 3 digit no:"); scanf("%d",&no); a=no%10; b=no%100; b=b/10; c=no/100; sum=a*100+b*10+c; if(sum==no) printf("\n%d is palindrome",no); else printf("\n%d is not palindrome\n",no);

getch(); } Result: 1. Enter the three digits no: 656 656 is palindrome 2. Enter the three digits no: 123 123 is not palindrome. Discussion: If a no. is palindrome then if we write it from backward or forward it doesnt make a difference to the number.

Question 5: Program to convert the digits in figures to digits in letters. E.g., 9 Nine Program code: #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("Enter any single digit\n"); scanf("%d",&n); switch (n) { case 0: printf("Zero"); break; case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; case 6:

printf("Six"); break; case 7: printf("Seven"); break; case 8: printf("Eight"); break; case 9: printf("Nine"); break; default: printf("invalid character"); break; } getch(); } Result: When you enter a year it will display it is leap year or not. For example:Output1: Enter any single digit : 8 Eight Output2: Enter any single digit: 12 Invalid character. Discussion: Switch case and break command application is being studied and analysed.

Question 6: (Bonus) Write a program for grade sheet of your class. Program code: #include<stdio.h> #include<conio.h> void main() { int marks; clrscr(); printf("Enter your mark \n"); scanf("%d",&marks); if (marks<= 40) { printf("grade= FAIL"); }

else if (marks > 40 && marks<= 50) { printf("\n grade=E"); } else if (marks> 50 && marks <= 60) { printf("\n grade=D"); } else if (marks > 60 && marks <= 70) { printf("\n grade=C"); } else if (marks > 70 && marks <= 80) { printf("grade=B"); } else if (marks > 80 && marks <= 90) { printf("grade=A"); } else{ printf("grade=S"); } getch(); } Result: Enter your mark : 89 Grade=A Discussion: This is the best example showing the use of if-else if-else command to calculate grade.

You might also like