Lab 4 (C Programming) Solution
Feb 18, 2019programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
1)Write a program to check whether input alphabet is vowel or not using if-else and switch statement.
Source Code(using if-else):
/*check vowel or not using if else */ #include<stdio.h> int main() { char ch; printf("Enter the character to check:"); scanf(" %c", &ch); /* checking alphabet or not you can also check for ascii values too if((ch >= 65 && ch <= 90) ||(ch >= 97 && ch <= 122)) */ if((ch >= 'A' && ch <= 'Z') ||(ch >= 'a' && ch <= 'z')){ /* checking if it is vowel */ if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){ printf("It is vowel."); }else{ printf("It is not vowel."); } }else{ printf("The Entered character is not alphabet."); } return 0; }
Output:
Enter the character to check:f It is not vowel.
Source Code(using switch-statement):
#include <stdio.h> int main() { char ch; printf("Enter the character:"); scanf(" %c", &ch); switch(ch){ case 'A': case 'E': case 'I': case 'O': case 'U': case 'a': case 'e': case 'i': case 'o': case 'u': printf("It is vowel."); break; default: printf("It is not vowel."); break; } return 0;
}
Output:
Enter the character:E It is vowel.
2)Write a program to get input of two or higher digit integer number and display in reverse order, Also check if the number is pallindrome or not?
Source Code:
#include <stdio.h> int main() { unsigned long int number, temp_number, rev_number = 0; short int rem; printf("Enter the number to check:"); scanf("%lu", &number); temp_number = number; do{ rem = temp_number % 10; temp_number /= 10; rev_number = rev_number * 10 + rem; }while(temp_number != 0); printf("Reverse of the given number = %lu\n", rev_number); if(number == rev_number){ printf("Entered number is pallindrome.\n"); }else{ printf("Entered number is not pallindrome.\n"); } return 0; }
Output:
Enter the number to check:123454321 Reverse of the given number = 123454321 Entered number is pallindrome.
3)Write a program that asks a number and test the number whether it is multiple of 5 or not, divisible by 7 but not by eleven.
Source Code:
#include <stdio.h> int main() { int number; printf("Enter the number to check:"); scanf("%d", &number); if((number % 5 == 0) && (number % 7 == 0) && (number % 11 != 0)){ printf("Entered number is multiple of 5 divisible by 7 and not divisible by 11."); }else{ printf("Entered number does not meet the above requirement."); } return 0; }
Output:
Enter the number to check:35 Entered number is multiple of 5 divisible by 7 and not divisible by 11.
4)Write a program to check whether the entered year is leap year or not (a year is leap if it is divisible by 4 and divisible by 100 or 400.)
Source Code:
/*program to check leap year*/ #include <stdio.h> int main() { int year; printf("Enter the year:"); scanf("%d", &year); if(year % 4 == 0){ if(year % 100 == 0){ if(year % 400 == 0){ printf("It is leap year."); }else{ printf("It is not leap year."); } }else{ printf("It is leap year."); } }else{ printf("It is not leap year."); } return 0; }
Output:
Enter the year:800 It is leap year.
5)Write a program to read the values of coefficients a, b and c of a quadratic equation ax2 +bx+c=0 and find roots of the equation.
Source Code:
#include <stdio.h> #include <math.h> int main() { float a, b, c, d, r1, r2, rpart, ipart; printf("Enter a, b and c of quadratic equation:"); scanf(" %f %f %f", &a, &b, &c); d = pow(b, 2) - 4 * a * c; if( d > 0){ r1 = (-b + sqrt(d)) / 2*a; r2 = (-b - sqrt(d)) / 2*a; printf("Roots are real and are %f and %f", r1, r2); }else if(d == 0){ r1 = -b/2*a; printf("The roots are real and equal and is:%f", r1); }else{ rpart = -b/2*a; ipart = sqrt(-d)/2*a; printf("Roots are imaginary and are.\n"); printf("%0.2f+i%0.2f and %0.2f+i%0.2f", rpart, ipart, rpart, ipart); } return 0; }
Output:
Enter a, b and c of quadratic equation:1 2 3 Roots are imaginary and are. -1.00+i1.41 and -1.00+i1.41
6)Write a program to check if the character entered by the user is alphabet, digit or special character.
Source Code:
#include <stdio.h> int main() { char ch; printf("Enter the character to check:"); scanf(" %c", &ch); if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')){ printf("It is alphabet."); }else if(ch >= '0' && ch <= '9'){ printf("It is digit."); }else{ printf("It is special character."); } return 0; }
Output:
Enter the character to check:D It is alphabet.
7)Write a program to find the largest of three numbers entered by the user, using if else statement.
Source Code:
#include <stdio.h> int main() { int a, b, c; printf("Enter three numbers."); scanf(" %d %d %d", &a, &b, &c); if(a > b){ if(a > c){ printf("%d is greatest.",a); }else{ printf("%d is greatest.",c); } }else{ if(b > c){ printf("%d is greatest.",b); }else{ printf("%d is greatest.",c); } } return 0; }
Output:
Enter three numbers.45 67 55 67 is greatest.