Week3-Section-F
Week3-Section-F
LABORATORY MANUAL
Week 3
1. Write a program that checks whether a given character is a vowel or a consonant using switch-
case..
--------------
CODE:
#include<stdio.h>
int main()
{
char character;
printf("enter a character\n");
scanf("%c/n",&character);
if(character>='A' && character<='Z'||character>='a' && character<='z')
{
printf("it is an alphabet\n");
}
else
{
printf("not an alphabet\n");
return 0;
}
switch(character)
{
case 'A':
printf("vowels");
break;
case 'a':
printf("vowels");
break;
case 'E':
printf("vowels");
break;
case 'e':
printf("vowels");
break;
case 'I':
printf("vowels");
break;
case 'i':
printf("vowels");
break;
case 'O':
printf("vowels");
break;
case 'o':
printf("vowels");
break;
case 'U':
printf("vowels");
break;
case 'u':
printf("vowels");
break;
default:
printf("consonant");
}
return 0;
}
--------------
Output :
enter a character
s
it is an alphabet
consonantnithishy@master-To-be-filled-by-O-E-M:~$ ./a.out
enter a character
1
not an alphabet
--------------
2. Write a program to calculate the factorial of a given number using a loop. Use if to check for
negative input.
--------------
CODE:
#include<stdio.h>
int main()
{
int n,i,fact;
printf("enter a number\n");
scanf("%d",&n);
fact=1;
if (n<0)
{
printf("negative numbers\n");
}
else
{
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial is %d",fact);
}
return 0;
}
-------------
Output :
enter a number
6
factorial is 720
--------------
3. Write a program to check whether a given number is prime or not using loops and control
structures.
--------------
CODE:
--------------
Output :
--------------
4. Write a program to compute both the GCD and LCM of two numbers. Use the Euclidean algorithm
for GCD and the relation: LCM×GCD=product of the numbers..
--------------
CODE:
--------------
Output :
--------------