0% found this document useful (0 votes)
2 views

Lab Session - 2 [Various Operators, Conditional Statements]

The document contains various C programming exercises focusing on operators, conditional statements, and input/output operations. It includes programs for increment/decrement operators, finding the largest of three numbers, determining even/odd status, calculating grades, checking leap years, and identifying vowels/consonants. Each program is accompanied by code snippets and explanations of the logic used.

Uploaded by

p20242202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Session - 2 [Various Operators, Conditional Statements]

The document contains various C programming exercises focusing on operators, conditional statements, and input/output operations. It includes programs for increment/decrement operators, finding the largest of three numbers, determining even/odd status, calculating grades, checking leap years, and identifying vowels/consonants. Each program is accompanied by code snippets and explanations of the logic used.

Uploaded by

p20242202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab session - 2

-----------------------------------------------------
-
Various operators in C:

1
Assignment operators:

Increment and Decrement operators:


Increment +
operator +
Decrement -
operator -

1. WAP to show the working of increment and decrement operators

#include <stdio.h>
int main()
{
int a=5, b=5, c=5, d=5;
clrscr();
printf("%d\n", a++); // 5 is displayed. Then, a is increased to 6.
printf("%d\n", a);
printf("%d\n", ++b); // b is increased to 6. Then, it is displayed.
printf("%d\n", c--);
printf("%d\n", c);
printf("%d\n", --d);
getch();
return 0;
}

2. Write a program to find the largest of three numbers with ternary


operators.
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c,L;
system("cls");
2
printf("Enter three integers: ");
scanf("%d%d%d",&a,&b,&c);
L=a>b?a:b;
L=L>c?L:c;
printf("Largest no. is: %d",L);
getch();
return 0;
}

Alternatively

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,L;
clrscr();
printf("Enter 3 integers: ");
scanf("%d %d %d",&a,&b,&c);
L=a>b && a>c?a:b>c?b:c; // alternative: a>b?(a>c?a:c):(b>c?b:c)
printf("%d is largest",L);
getch();
}

3. Write a program to find the largest of three numbers with without ternary
operators. [Using simple if-else statements]
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,L;
clrscr();
printf("Enter 3 integers: ");
scanf("%d %d %d",&a,&b,&c);
if (a>b)
{
L=a;
}
else
{
L=b;
}
if (L>c)
{
printf("%d is largest",L);
}
else
{
printf("%d is largest",c);
}
getch();
}

Alternatively
#include<stdio.h>
3
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 integers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
else if(b>c)
{
printf("%d is largest",b);
}
else
{
printf("%d is largest",c);
}
getch();
}

4. WAP to ask for any number and declare it as even or odd.


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number: ");
scanf("%d",&a);
if (a%2==0) //Use of the modulo operator (%) which gives remainder after
division
{
printf("Even no.");
}
else
{
printf("Odd no.");
}
getch();
}

5. Write a program to input marks of 5 subjects of a student and display the


total marks scored, percentage scored and the class of result [Use of if-
else if ladder]
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e;
float p;
clrscr();
printf("Enter marks (out of 100) in 5 subjects: ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
4
printf("Total marks = %d\n",a+b+c+d+e);
printf("Percentage = %.2f%\n",p=((float)a+b+c+d+e)*100/500);
//(float)a changes ‘a’ from int to float. Now all other interactions
of int variables with ‘a’ will yield float only.
//This is called as 'Typecasting' i.e. upgrading a variable further
down the program after its initial declaration.
if (p<40)
{printf("Class: Failed");}
else if(p<60)
{printf("Class: 2nd");}
else if(p<80)
{printf("Class: 1st");}
else
{printf("Class: Distinction");}
getch();
}

6. WAP to get percentage from user and declare the class [using nested if-else
statements]

#include<stdio.h>
#include<conio.h>
void main()
{
float p;
clrscr();
printf("Enter percentage: ");
scanf("%f",&p);
if (p>=40)
{
if (p>=60)
{
if (p>=80)
{
printf("Distinction");
}
else
printf("1st class");
}
else
printf("2nd class");
}
else
printf("Fail");
getch();
}

7. WAP to ask for a character from keyboard and declare if it is an alphabet


or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr();
printf("Enter a character: ");
5
scanf("%c",&a);
if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
printf("This is an Alphabet"); // for single statements within if-else
there is no need for {}.
else
printf("This is NOT an Alphabet");
getch();
}

ASCII table of characters for reference

6
8. Write a program to compute grade of students using if else ladder. The
grades are assigned as followed:
Marks Grade
Marks < 50 F
50 < Marks < 60 C
60 < Marks < 70 B
70 < Marks < 80 B+
80 < Marks < 90 A
90 < Marks < 100 A+

#include<stdio.h>
#include<conio.h>
void main()
{
float m;
clrscr();
flag: // returns here from goto command
printf("Enter marks: ");
scanf("%f",&m);
if (m>100)
{
printf("Invalid marks. Enter <= 100.\n\n");
goto flag;
}
if (m<50)
printf("Grade: F");
else if (m<60)
printf("Grade: C");
else if (m<70)
printf("Grade: B");
else if (m<80)
printf("Grade: B+");
else if (m<90)
printf("Grade: A");
else
printf("Grade: A+");
getch();
}

9. Write a program to check whether the entered year is leap year or not.

//To determine whether a year is a leap year, follow these steps:


//1. If the year is evenly (i.e. completely) divisible by 4, go to step
2. Otherwise, go to step 5.
//2. If the year is evenly divisible by 100, go to step 3. Otherwise, go
to step 4.
//3. If the year is evenly divisible by 400, go to step 4. Otherwise, go
to step 5.
//4. The year is a leap year (it has 366 days).
//5. The year is not a leap year (it has 365 days).

#include<stdio.h>
#include<conio.h>
void main()
{
7
int y;
printf("Enter year: ");
scanf("%d",&y);
if(y%4==0&&y%100!=0)
printf("%d is a Leap Year",y);
else if(y%4==0&&y%100==0&&y%400==0)
printf("%d is a Leap Year",y);
else
printf("%d is a NOT a Leap Year",y);
}

10. Write a program to find whether a character is consonant or vowel using


Switch – Case statement.

#include<stdio.h>
#include<conio.h>
int main()
{
char alph;
clrscr();
printf("Enter an alphabet: ");
scanf("%c",&alph);
switch(alph)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
8
getch();
return 0;
}

Alternatively

#include<stdio.h>
#include<conio.h>
void main()
{
char alph;
clrscr();
printf("Enter Character: ");
alph = getchar();
switch(alph)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("Character %c is a Vowel",alph);
break;
default: printf("Character %c is a Consonant",alph);
}
getch();
}
----------------------------------X----------------------------------

You might also like