Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

BCSE102L & BCSE102P –Structured and Object Oriented

Programming (Lab)
(Contents as per the syllabus)

Module I – Demo Programs in Code chef & Online GDB

Programs using basic control structures, branching and looping

Overview of C : Operators, Expressions, Input/output Statements

1. Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}

The operators +, - and * computes addition, subtraction, and multiplication respectively as


you might have expected.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.


It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder
is 1. The % operator can only be used with integers.

2. C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
// Working of increment and decrement operators
#include <stdio.h>

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}
3. C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0, c=c%a;
printf("c = %d\n", c);
return 0;
}

4. C Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}

5. C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


printf("!(a == b) is %d \n", result);

return 0;
}

Explanation of logical operator program


(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).

6. C Bitwise Operators
https://www.programiz.com/c-programming/bitwise-operators

7. Ternary or Conditional Operator


Simple decision making statements in C programming can be carried out by using ternary
operator (?:). The ternary operator is also known as conditional operator since it takes three
operands.
The general syntax for conditional operator is:
Condition? True_Expression: False_Expresion;

#include< stdio.h >


#include< conio.h >

void main()
{
float x,y;
clrscr();
printf("Enter x: ");
scanf("%f", &x);
y = x < 0 ? x*x+4 : x*x-4;
printf("f(%f) = %f", x, y);
getch();
}

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


8. Expressions – Programs

Expressions and if –else loop

1. #include <stdio.h>
2. int main()
3. {
4.
5. int x=4;
6. if(x%2==0)
7. {
8. printf("The number x is even");
9. }
10. else
11. printf("The number x is not even");
12. return 0;
13. }
9. Expressions and if –else loop

1. #include <stdio.h>
2. int main()
3. {
4. int x = 4;
5. int y = 9;
6. if ( (x <6) || (y>10))
7. {
8. printf("Condition is true");
9. }
10. else
11. printf("Condition is false");
12. return 0;
13. }
10.Expressions and if –else loop

1. #include<stdio.h>
2. #include<string.h>
3. int main()
4. {

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


5. int age = 18;
6. char status;
7. status = (age>18) ? 'E': 'N';
8. if(status == 'E')
9. printf("Vote Eligible");
10. else
11. printf("Not Eligible");
12. return 0;
13. }
11. Identify the logic of the program using if-else loop for ATM transactions for the following
denominations 100, 500, 1000 using If-Else / Else- if / Nested if / if Loop and write a similar
program for denominations 200, 500, 2000.
#include<stdio.h>
1. int totalThousand =1000;
2. int totalFiveFundred =1000;
3. int totalOneHundred =1000;
4. int main(){
5. long withdrawAmount;
6. long totalMoney;
7. int thousand=0,fiveHundred=0,oneHundred=0;
8. printf("Enter the amount in multiple of 100: ");
9. scanf("%lu",&withdrawAmount);
10. if(withdrawAmount %100 != 0){
11. printf("Invalid amount;");
12. return 0;
13. }
14. totalMoney = totalThousand * 1000 + totalFiveFundred* 500 + totalOneHundred*100;
15. if(withdrawAmount > totalMoney){
16. printf("Sorry,Insufficient money");
17. return 0;
18. }
19. thousand = withdrawAmount / 1000;
20. if(thousand > totalThousand)
21. thousand = totalThousand;
22. withdrawAmount = withdrawAmount - thousand * 1000;
23. if (withdrawAmount > 0){
24. fiveHundred = withdrawAmount / 500;

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


25. if(fiveHundred > totalFiveFundred)
26. fiveHundred = totalFiveFundred;
27. withdrawAmount = withdrawAmount - fiveHundred * 500;
28. }
29. if (withdrawAmount > 0)
30. oneHundred = withdrawAmount / 100;
31. printf("Total 1000 note: %d\n",thousand);
32. printf("Total 500 note: %d\n",fiveHundred);
33. printf("Total 100 note: %d\n",oneHundred);
34. return 0;
35. }
12. Write the C program for calculator application using switch case statement
#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


default:
printf("Error! operator is not correct");
}

return 0;
}
13. C Program to check whether an alphabet is vowel or Consonant
#include <stdio.h>
#include <conio.h>
int main(){
char c;
printf("Enter a Character: ");
scanf("%c", &c);
/* Check if input alphabet is member of set{A,E,I,O,U,a,e,i,o,u} */
if(c == 'a' || c == 'e' || c =='i' || c=='o' || c=='u' || c=='A'
|| c=='E' || c=='I' || c=='O' || c=='U'){
printf("%c is a Vowel\n", c);
} else {
printf("%c is a Consonant\n", c);
}
getch();
return 0;
}

14. Write a C program to enter month number and print number of days in month.

/**
* C program to print number of days in a month
*/

#include <stdio.h>

int main()
{
int month;

/* Input month number from user */


printf("Enter month number (1-12): ");
scanf("%d", &month);

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


if(month == 1)
{
printf("31 days");
}
else if(month == 2)
{
printf("28 or 29 days");
}
else if(month == 3)
{
printf("31 days");
}
else if(month == 4)
{
printf("30 days");
}
else if(month == 5)
{
printf("31 days");
}
else if(month == 6)
{
printf("30 days");
}
else if(month == 7)
{
printf("31 days");
}
else if(month == 8)
{
printf("31 days");
}
else if(month == 9)
{
printf("30 days");
}
else if(month == 10)
{
printf("31 days");
}
else if(month == 11)
{
printf("30 days");
}
else if(month == 12)
{

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


printf("31 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}

return 0;
}
15. Write a C program to generate Fibonacci series up to n value using for loop.

#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

16. Write a C program to find the factorial of the given number using for loop.
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}
17. Write a C program to print total number of digits in the given number using while loop.
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);

// iterate at least once, then until n becomes 0


// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
++count;
} while (n != 0);

printf("Number of digits: %d", count);


}
18. Write the C program to find product of all digits.
#include <stdio.h>

int main()
{
int Number, Reminder, Product;

printf("\n Please Enter any Number that you wish : ");


scanf("%d", & Number);

for(Product = 1; Number > 0; Number = Number / 10)


{
Reminder = Number % 10;
Product = Product * Reminder;
}

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


printf(" \n The Product of Digits of a Given Number = %d", Product);

return 0;
}

19. Write a C program to find largest of 3 numbers.

#include <stdio.h>

int main()
{
int A, B, C;

printf("Enter the numbers A, B and C: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

if (B >= A && B >= C)


printf("%d is the largest number.", B);

if (C >= A && C >= B)


printf("%d is the largest number.", C);

return 0;
}
20. Write the answer for the following.
// An example of implicit conversion
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0;
}

21. Write the answer for the following

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


// C program to demonstrate explicit type casting
#include<stdio.h>

int main()
{
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

printf("sum = %d", sum);

return 0;
}

22. Expressions – Evaluation


#include <stdio.h>
//main method for run the C application
int main()
{
//declaring variables
int a,b;
double output;
//Asking user to enter 2 numbers as input
printf("Please enter any 2 numbers \n");
//store 2 numbers in 2 variables
scanf("%d\n\t%d",&a,&b);
//assigning resultant of operators to a variable
output=a+b*b-a/b%a;
//displaying output
//first precedence given to *, followed by /, %, + and -
printf("Output of %d and %d is =%lf ",a, b,output);
return 0;
}
Output :

23. Write a c program for bit-wise operator – complete the below given program
#include <stdio.h>

void main()
{
long number, tempnum;

printf("Enter an integer \n");


scanf("%ld", &number);
tempnum = number;
/* left shift by two bits */

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


number = number << 2;
printf("%ld x 4 = %ld\n", tempnum, number);
}
Test cases :
Enter an integer
450
450 x 4 = 1800

Output :

24. Write a C Program finds sum of first 50 natural numbers using for loop.
#include <stdio.h>

void main()
{
int num, sum = 0;

for (num = 1; num <= 50; num++)


{
sum = sum + num;
}
printf("Sum = %4d\n", sum);
}
25. C program for continue statement
#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {

if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}

printf("value of a: %d\n", a);


a++;

} while( a < 20 );

return 0;

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


}

26. C program for Go-to Statement


#include <stdio.h>
27. int main()
28. {
29. int num,i=1;
30. printf("Enter the number whose table you want to print?");
31. scanf("%d",&num);
32. table:
33. printf("%d x %d = %d\n",num,i,num*i);
34. i++;
35. if(i<=10)
36. goto table;
37. }

27. C program for Go to Statement – When to use it

1. #include <stdio.h>
2. int main()
3. {
4. int i, j, k;
5. for(i=0;i<10;i++)
6. {
7. for(j=0;j<5;j++)
8. {
9. for(k=0;k<3;k++)
10. {
11. printf("%d %d %d\n",i,j,k);
12. if(j == 3)
13. {
14. goto out;
15. }
16. }
17. }
18. }

28. Program for continue and break

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, it's not added to the result

#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;

for (i = 1; i <= 10; ++i) {


printf("Enter a n%d: ", i);
scanf("%lf", &number);

if (number < 0.0) {


continue;
}

sum += number; // sum = sum + number;


}

printf("Sum = %.2lf", sum);

return 0;
}

29. Program for else-if ladder


1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
7. printf("number is equals to 10");
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
12. else if(number==100){

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE


13. printf("number is equal to 100");
14. }
15. else{
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }

30. Progrm to find grade of students according to specified marks


1. #include <stdio.h>
2. int main()
3. {
4. int marks;
5. printf("Enter your marks?");
6. scanf("%d",&marks);
7. if(marks > 85 && marks <= 100)
8. {
9. printf("Congrats ! you scored grade A ...");
10. }
11. else if (marks > 60 && marks <= 85)
12. {
13. printf("You scored grade B + ...");
14. }
15. else if (marks > 40 && marks <= 60)
16. {
17. printf("You scored grade B ...");
18. }
19. else if (marks > 30 && marks <= 40)
20. {
21. printf("You scored grade C ...");
22. }
23. else
24. {
25. printf("Sorry you are fail ...");
26. }
27. }

Dr.R. Priyadarshini, AP (Sr.Gr. II), SCOPE

You might also like