Facebook Experiences Widespread Outages on March 5 2024

ERC First Semester C Programming LAB SHEET NO.3 [To be familiar with selective structure (branching)]

ERC First Semester C Programming LAB SHEET NO.3 [To be familiar with selective structure  (branching)]

Question

  1. WAP to check whether a number is negative, positive or zero.
  2. WAP to find maximum between three numbers entered by the user.
  3. WAP to input a character from the user and check whether the character is vowel or consonant.
  4. WAP to input a character from the user and check whether the character is Alphabet or not. If the character is Alphabet then show whether it is uppercase or lowercase.
  5. WAP to check whether the year entered by the user is leap year or not.
  6. WAP to check whether the number entered by the user is divisible by 5 and 11 or not.
  7. WAP to find the all the roots of a quadratic equation.
  8. WAP to input two numbers and operator among [ + , - ,* , / ]. If user enters + then the program should perform the addition of the number and display the sum. If user enters – then the program should perform subtraction of number and display the difference and so on for * and /.
  9. WAP in C to input marks of five subjects C-programming, Physics, Maths, Applied Mechanics and Basic electrical. Display whether the student passed or failed. Take F.M=100 and P.M.=40 For passed students calculate percentage and grade according to following:
    Percentage >= 90% : A
    Percentage >= 80% : B
    Percentage >= 70% : C
    Percentage >= 60% : D
    Percentage >= 40% : E
  10. WAP to input a number from user. If user enters a number less than or equal to zero then program should just display the number. If user enters 1 the program should display output as neither prime nor composite, if user enters 2 the program should display output as smallest and only even prime number. If user enters any number greater than 2 the program should check whether the number is prime or not, also if the number is not prime the program should display whether it is even or odd.

Complete Lab Sheet 3:

Objective(s): To be familiar with selective structure (branching) in C.

Solution:

1. WAP to check whether a number is negative, positive or zero.

Algorithm

Step 1: Start
Step 2: Enter number.
Step 3: Check number is greater than equal to 0 or not. If true, then positive otherwise negative and if it is 0 then number is 0.
Step 4: End.

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
int a;
printf("Enter any Number: \t");
scanf("%d",&a);
if(a>0)
{
printf("Positive.");
}
else if(a<0)
{
printf("Negative");
}
else
{
printf("Zero");
}
return 0;
}

Output:

Enter any Number: -5
Negative

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a number, then checks if the number is positive, negative, or zero. It uses an if-else statement to determine the output message based on the input value.
Conclusion:
This code is a simple program that determines the sign of a given number. It effectively uses conditional statements to provide the appropriate output based on the input.

2. WAP to find the maximum between three numbers entered by the user.

Algorithm

Step 1: Start
Step 2: Declare a, b and c and initialize it.
Step 3: If a is greater than b and c, display a is maximum.
Step 4: If b is greater than c and a, display b is maximum.
Step 5: If c is greater than a and b, display c is maximum.
Step 6: End

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter any three number:\t");
scanf("%d %d %d",&a, &b, &c);
if (a>b)
{
if (a>c)
{
printf("%d is greatest among all.",a);
}
else
{
printf("%d is greatest among all.",c);
}
}
else if (b>c)
{
printf("%d is greatest among all.",b);
}
else
{
printf("%d is greatest among all.",c);
}
}

Output:

Enter any three number:  19
20
60
60 is greatest among all.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter three numbers and then compares them to find the greatest number. It uses nested if-else statements to determine the largest value among the three inputs.
Conclusion:
This code effectively determines the largest number among three given numbers using nested if-else statements. It provides the correct output by comparing the values and selecting the greatest one based on the conditions. However, it is important to note that the code is missing a return statement at the end of the main function, which could result in undefined behavior.

3. WAP to input a character from the user and check whether the character is vowel or consonant.

Algorithm

Step 1: Start
Step 2: Declare the character ‘z’ and initialize it.
Step 3: If z= ‘a’ or ‘e’ or ‘i’ or ‘o’ or ‘u’ or ‘A’ or ‘E’ or ‘I’ or ‘O’ or ‘U’, display vowel otherwise consonant.
Step 4: End

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if (ch=='a' || ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A' || ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
printf("%c is Vowel.",ch);
}
else
{
printf("%c is Consonant.",ch);
}
return 0;
}

Output:

Enter any character: A
A is Vowel.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a character and checks whether it is a vowel or a consonant using multiple conditional statements. It then prints the appropriate output message based on the character entered.
Conclusion:
This code effectively determines whether the input character is a vowel or a consonant by comparing it against multiple conditions. It provides the correct output message accordingly. The inclusion of a return statement at the end of the main function ensures proper program execution.

4. WAP to input a character from the user and check whether the character is Alphabet or not. If the character is Alphabet then show whether it is uppercase or lowercase.

Algorithm

Step 1: Start
Step 2: Declare the character ‘ch’ and initialize it.
Step 3: If ch ≥ ‘a’ and ch ≤ ’z’ or ch ≥ ‘A’ and ch ≤ ’Z’ , display the ch is an alphabet.
Otherwise display ch is not an alphabet.
Step 4: If ch ≥ ’a’ and ch ≤ ’z’ , display the ch is in lowercase otherwise display the ch is in uppercase.
Step 5: End.

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("The entered character is an alphabet ");
if (ch >= 'a' && ch <= 'z')
{
printf("and it is a lowercase letter.\n"); }
else
{
printf("and it is an uppercase letter.\n");
}
}
else
{
printf("The entered character is not an alphabet.\n");
}
return 0;
}

Output:

Enter a character: p
The entered character is an alphabet and it is a lowercase letter.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a character and checks whether it is an alphabet or not. It further determines if the character is a lowercase or uppercase letter and provides the corresponding output message.
Conclusion:
This code effectively determines whether the entered character is an alphabet or not by comparing its ASCII value range. Additionally, it correctly identifies whether the alphabet is a lowercase or uppercase letter and provides the appropriate output message. The inclusion of a return statement at the end of the main function ensures proper program execution.

5. WAP to check whether the year entered by the user is leap year or not.

Algorithm

Step 1: Start
Step 2: Read year
Step 3: If (year/400) gives remainder 0 or (year/4) gives remainder 0 and (year/100) does not give remainder 0
Step 4: Display a leap year otherwise not a leap year.
Step 5: End.

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
int a;
printf("Enter a year: ");
scanf("%d", &a);
if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0) {
printf("%d is a leap year.\n", a);
}
else
{
printf("%d is not a leap year.\n", a);
}
return 0;
}

Output:

Enter a year: 2023
2023 is not a leap year.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a year and checks whether it is a leap year or not. It uses the conditions for leap year calculation (divisible by 4 but not by 100, or divisible by 400) to determine the output message.
Conclusion:
This code effectively determines whether the entered year is a leap year or not by applying the leap year conditions. It provides the appropriate output message indicating whether the year is a leap year or not. The inclusion of a return statement at the end of the main function ensures proper program execution.

6. WAP to check whether the number entered by the user is divisible by 5 and 11 or not.

Algorithm

Step 1: Start
Step 2: Read the value ‘a’ and initialise it.
Step 3: If ‘a’ gives remainder 0 when divided by 5 and 11 both display a is divisible by 5 and 11 otherwise not.
Step 4: End

Flowchart

C Program: Code

#include <stdio.h>
int main()
{
int n;
printf("Enter any number:\n");
scanf("%d",&n);
if(n%5==0&&n%11==0)
{
printf("Entered number %d is Divisible by 5 and 11.",n);
}
else
{
printf("Entered number %d is not divisible by 5 and 11.",n);
}
return 0;
}

Output:

Enter any number:
55
Entered number 55 is Divisible by 5 and 11.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a number and checks whether it is divisible by both 5 and 11. It uses the modulus operator to determine the divisibility and provides the corresponding output message.
Conclusion:
This code effectively determines whether the entered number is divisible by both 5 and 11 by checking the remainders using the modulus operator. It provides the appropriate output message indicating whether the number is divisible by 5 and 11 or not. The inclusion of a return statement at the end of the main function ensures proper program execution.

7. WAP to find the all the roots of a quadratic equation.

Algorithm

Step 1: Start.
Step 2: Read the values of coefficients a, b, and c from the user.
Step 3: Calculate the discriminant using the formula dis = b2 - 4ac.
Step 4: Check the value of the discriminant:
a. If dis > 0, go to step 5.
b. If dis = 0, go to step 6.
c. If dis < 0, go to step 7.
Step 5: Calculate the two real and distinct roots using the formulas:
root1 = (-b + sqrt(dis)) / (2a)
root2 = (-b - sqrt(dis)) / (2a)
Display the roots root1 and root2.
Step 6: Calculate the single real root using the formula:
root = -b / (2a)
Display the root.
Step 7: Calculate the real and imaginary parts of the complex roots using the formulas:
real = -b / (2a)
img = sqrt(-dis) / (2a)
Display the complex roots as root1 = real + img i and root2 = real – img i.
Step 8: Stop.

Flowchart

C Program: Code

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,e,f,g;
printf("Enter the value of a,b, and c.\n");
scanf("%f%f%f",&a,&b,&c);
d=sqrt(b*b-4*a*c);
if(d==0)
{
printf("The roots are real and equal.\n");
e=(-b/2*a)+(d/2*a);
f=(-b/2*a)-(d/2*a);
printf("The root of given equation are %f and %f.\n",e,f);
}
else if(d>0)
{
printf("The roots are real and distinct.\n");
e=(-b/2*a)+(d/2*a);
f=(-b/2*a)-(d/2*a);
printf("The root of given equation are %f and %f.\n",e,f);
}
else
{
printf("The roots are Imaginary and distinct.\n");
e=(-b/2*a);
g=(sqrt(-(b*b-4*a*c))/2*a);
printf("The root of given equation is %f + %f i.\n",e,g);
printf("The root of given equation is %f - %f i.\n",e,g);
}
return 0;
}

Output:

Enter the value of a,b, and c.
1
5
7
The roots are Imaginary and distinct.
The root of given equation is -2.500000 + 0.866025 i.
The root of given equation is -2.500000 - 0.866025 i.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter the values of a, b, and c and solves a quadratic equation using the quadratic formula. It calculates the discriminant d and checks its value to determine the type of roots (real and equal, real and distinct, or imaginary and distinct). It then computes and displays the roots accordingly.
Conclusion:
This code effectively solves a quadratic equation by calculating the discriminant and applying the quadratic formula. It correctly determines the type of roots based on the value of the discriminant and displays the roots appropriately. The inclusion of a return statement at the end of the main function ensures proper program execution.

8. WAP to input two numbers and operator among [ + , - ,* , / ]. If user enters + then the program should perform the addition of the number and display the sum. If user enters – then the program should perform subtraction of number and display the difference and so on for * and /.

Algorithm

Step 1: Start
Step 2: Read the value of two numbers from user and assign them as a and b.
Step 3: Read the Character [+, -, *, /] from user as Addition, Subtraction, Multiplication and division respectively and assign this character as ‘ch’
Step 4: Check the character
a. If ch=’+’ then go to step 5
b. If ch=’-’ then go to step 6
c. If ch=’*’ then go to step 7
d. If ch=’/’ then go to step 8
e. If ch is other than above character then goto step 9
Step 5: Ans = a + b
Display Ans
Step 6: Ans = a-b
Display Ans
Step 7: Ans = a*b
Display Ans
Step 8: Ans = a/b
Display Ans
Step 9: Display Invalid Character
Step 10: Stop

Flowchart

C Program: Code

#include<stdio.h>
int main() {
float a, b, ans;
char ch;
printf("Enter any two numbers: ");
scanf("%f%f", &a, &b);
printf("Enter your choice: \n");
printf("Enter + for Addition \n");
printf("Enter - for Subtraction \n");
printf("Enter * for Multiplication \n");
printf("Enter / for Division \n");
scanf(" %c", &ch);
switch(ch)
{
case '+':
ans = a + b;
printf("Addition is %.2f", ans);
break;
case '-':
ans = a - b;
printf("Subtraction is %.2f", ans);
break;
case '*':
ans = a * b;
printf("Multiplication is %.2f", ans);
break;
case '/':
ans = a / b;
printf("Division is %.2f", ans);
break;
default:
printf("Invalid Character.");
}
return 0;
}

Output:

Enter any two numbers: 5
8
Enter your choice:
Enter + for Addition
Enter - for Subtraction
Enter * for Multiplication
Enter / for Division
-
Subtraction is -3.00

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter two numbers and a choice of operation (+ for addition, - for subtraction, * for multiplication, / for division). It uses a switch statement to perform the selected operation and display the result.
Conclusion:
This code effectively performs basic arithmetic operations (addition, subtraction, multiplication, division) based on user input using a switch statement. It provides the correct output for the selected operation and handles invalid input gracefully. The inclusion of a return statement at the end of the main function ensures proper program execution.

9. WAP in C to input marks of five subjects C-programming, Physics, Maths, Applied Mechanics and Basic electrical. Display whether the student passed or failed. Take F.M=100 and P.M.=40 For passed students calculate percentage and grade according to following:
Percentage >= 90% : A
Percentage >= 80% : B
Percentage >= 70% : C
Percentage >= 60% : D
Percentage >= 40% : E

Algorithm

Step 1: Start
Step 2: Read the marks secured by the student in all subjects and assign their values to variables a, b, c, d, e.
Step 3: Check if the student has passed in all subjects. If yes, go to step 4; otherwise, go to step 9.
Step 4: Calculate the total marks by summing up the marks in all subjects: total = a + b + c + d + e.
Step 5: Calculate the percentage: percentage = (total / 200) * 100.
Step 6: Check the percentage and calculate the grade:
If percentage >= 90, go to step 7.
If not, go to step 8.
Step 7: Display "Student secured A" and go to step 10.
Step 8: Check the percentage and calculate the grade:
If percentage >= 80, go to step 9.
If not, go to step 10.
Step 9: Display the corresponding grade based on the percentage:
If percentage >= 70, display "Student secured C" and go to step 10.
If not, go to step 11.
Step 10: Display the corresponding grade based on the percentage:
If percentage >= 60, display "Student secured D" and go to step 11.
If not, go to step 12.
Step 11: Display the corresponding grade based on the percentage:
If percentage >= 40, display "Student secured E" and go to step 12.
If not, go to step 12.
Step 12: Display "Student failed."
Step 13: End

Flowchart
WAP in C to input marks of five subjects C-programming, Physics, Maths, Applied Mechanics and Basic electrical. Display whether the student passed or failed. Take F.M=100 and P.M.=40 For passed students calculate percentage and grade according to following:

C Program: Code

#include<stdio.h>
int main()
{
float a,b,c,d,e,f;
printf("Enter marks obtained in C-Programming:\n");
scanf("%f",&a);
printf("Enter marks obtained in Physics:\n");
scanf("%f",&b);
printf("Enter marks obtained in Mathematics:\n");
scanf("%f",&c);
printf("Enter marks obtained in Applied Mechanics:\n");
scanf("%f",&d);
printf("Enter marks obtained in Electrical:\n");
scanf("%f",&e);
if(a>=40&&b>=40&&c>=40&&d>=40&&e>=40)
{
printf("Student has passed exam.\n");
f=(((a+b+c+d+e)/500)*100);
if(f>= 90)
{
printf("Student has secured A Grade.");
}
if(f>= 80&&f<90)
{
printf("Student has secured B Grade.");
}
if(f>= 70&&f<80)
{
printf("Student has secured C Grade.");
}
if(f>= 60&&f<70)
{
printf("Student has secured D Grade.");
}
if(f>= 40&&f<60)
{
printf("Student has secured E Grade.");
}
}
else
{
printf("Student is not qualified for grading.");
}
return 0;
}

Output:

Enter marks obtained in C-Programming:
75
Enter marks obtained in Physics:
75
Enter marks obtained in Mathematics:
75
Enter marks obtained in Applied Mechanics:
75
Enter marks obtained in Electrical:
75
Student has passed exam.
Student has secured C Grade.

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter marks obtained in five subjects and determines the grade based on the average marks. It checks if the student has passed the exam (all subjects with marks equal to or greater than 40) and calculates the average marks percentage. It then assigns a grade based on the percentage.
Conclusion:
This code effectively determines the grade of a student based on their marks in five subjects. It checks if the student has passed the exam and calculates the average marks percentage. It assigns a grade (A, B, C, D, E) based on the percentage and provides the appropriate output. The inclusion of a return statement at the end of the main function ensures proper program execution.

10. WAP to input a number from the user. If the user enters a number less than or equal to zero then the program should just display the number. If the user enters 1 the program should display the output as neither prime nor composite, if the user enters 2 the program should display the output as the smallest and only even prime number. If the user enters any number greater than 2 the program should check whether the number is prime or not, also if the number is not prime the program should display whether it is even or odd.

Algorithm

Code

Flowchart

C Program: Code

#include<stdio.h>
int main()
{
int n,i,c=0;
printf("Enter any Number:\n");
scanf("%d",&n);
if(n<=0)
{
printf("You have entered %d.\n",n);
}
if(n==1)
{
printf("Neither Prime Nor Composite.");
}
if(n==2)
{
printf("Smallest Prime Number.");
}
if (n>2)
{ for (i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if (c==2)
printf ("The number is PRIME");
else
{
printf ("The number is COMPOSITE.");
if(n%2==0)
printf("\nEven Number");
else
printf("\nOdd Number");
}
}
return 0;
}

Output:

Enter any Number:
15
The number is COMPOSITE. Odd Number

Discussion and Conclusion:

Discussion:
The given code prompts the user to enter a number and checks whether it is prime or composite. It also determines if the number is even or odd. It uses a for loop to check for factors and counts them to determine if the number is prime or composite.
Conclusion:
This code effectively determines whether the entered number is prime or composite by checking for factors. It also identifies if the number is even or odd. It provides the appropriate output message based on the conditions. The inclusion of a return statement at the end of the main function ensures proper program execution.
C Programming Lab
A free online educational resource provider.

1 comment

  1. I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks
    design agency indianapolis
We get inspired from your single comment.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.