PIC Instructor Manual
PIC Instructor Manual
PIC Instructor Manual
Instructor Manual
COURSE NAME: PRINCIPLES OF
PROGRAMMING USING C
COURSE CODE:22CS23
Pg PART A
no Practice Programs
1. Familiarization with programming environment, concept of naming the
program files storing, compilation, execution and debugging. Create a new
file using gedit/vi filename.c
a. Write C statement(s) in the file which produces the following output:
Errors in C/C++
Error is an illegal operation performed by the user which results in abnormal
working of the program.
Programming errors often remain undetected until the program is compiled or
executed. Some of the errors inhibit the program from getting compiled or executed.
Thus errors should be removed before compiling and executing.
The most common errors can be broadly classified as follows.
Type of errors
1. Syntax errors: Errors that occur when you violate the rules of writing C/C++
syntax are known as syntax errors. This compiler error indicates something that
must be fixed before the code can be compiled. All these errors are detected by
compiler and thus are known as compile-time errors.
Most frequent syntax errors are:
Missing Parenthesis (})
Printing the value of variable without declaring it
Missing semicolon like this
// C program to illustrate syntax error
a)
#include<stdio.h>
void main()
{ int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
Error:
error: expected ';' before '}' token
b)
#include<stdio.h>
int main(void)
{
// while() cannot contain "." as an argument.
while(.)
{ printf("hello");
}
return 0;
}
Error:
error: expected expression before '.' token
while(.)
3. Linker Errors: These error occurs when after compilation we link the different
object files with main’s object using Ctrl+F9 key(RUN). These are errors generated
when the executable of the program cannot be generated. This may be due to wrong
function prototyping, incorrect header files. One of the most common linker error is
writing Main() instead of main().
// C program to illustrate
// linker error
#include<stdio.h>
void Main() // Here Main() should be main()
{ int a = 10;
printf("%d", a);
}
Error:
(.text+0x20): undefined reference to `main'
5. Semantic errors : This error occurs when the statements written in the program are
not meaningful to the compiler.
// C program to illustrate semantic error
void main()
{ int a, b, c;
a + b = c; //semantic error
}
Error
error: lvalue required as left operand of assignment
a + b = c; //semantic error
#include <stdio.h>
int main( )
{
inta,b ,c;
a= 5.54;
b =a+2;
scanf( "%d", &c);
printf( "%d %d %d\n",a, b,c);
return 0;
}
b. Find the errors in the following program:
/* There are errors in /*the*/ code.*/
#include <stdio.h>
#define CUBE(Y) (Y)*(Y)*(Y) ;
#define SQUARE(Y) (Y)*(Y);
int main()
{ double Int,y,_y1,2yb,a-b,y3z;
int Float,char,a,b,c,d,xy@c,qa.b; check it in the question variable names
char int,u,_2v,w=t;
a=2,b=3;
a+b;
c=a+b;
a+b=1;
b-a==c;
d=w;
a=CUBE(d);
b=SQUARE(d)
u=d+62;
c=u-1;
u=’y’;
_2v=z;
y3z=CUBE(c);
y=SQUARE(c);
_y1=SQUARE(c)*2;
c=y+u;
return 0;
}
Soln An error free code for the same is as follows
/* There are errors in the code.*
/ #include <stdio.h>
#define CUBE(Y) (Y)*(Y)*(Y)
#define SQ(Y) (Y)*(Y)
int main()
{
double Int,y,_y1,2yb,a_b,y3z;
int Float,cHar,a,b,c,d,xy_c,qab;
char inT,u,_2v,w=’t’;
a=2,b=3;
a+b;
c=a+b;
a+b==1;
b-a==c;
d=w;
a=CUBE(d);
b=SQ(d);
u=d+62;
c=u-1;
u=’y’;
_2v=’z’;
y3z=CUBE(c);
x=SQ(c);
_x1=2*SQ(c);
c=x+u;
return 0;
}
3.c Type the program in a file and examine the output of the following code:.
#include<stdio.h>
int main(void)
{
int a=234,b= -234,c=54321;
printf("%2d\n",c);
printf("%10.2d\n",c);
printf("%-10.2d\n",c);
printf("%-7d\n",a);
printf("%07.2d\n",a);
printf("%07d\n",a);
printf("%+0-9.4d\n",a);
printf("%+09.4d\n",a);
printf("%+07d\n",a);
printf("%+07.4d\n",a);
printf("%+-07.4d\n",a);
printf("%-08d\n",b);
printf("%-08.2d\n",b);
printf("%-8.4d\n",b);
return 0;
}
4 Implementation and execution of simple programs to understand working of
operators like:
Unary.
Arithmetic.
Logical.
Relational.
Conditional.
Bitwise.
a C program to demonstrate working of Unary arithmetic operators
#include <stdio.h>
int main()
{ int a = 10, b = 4, res;
// post-increment example:
// res is assigned 10 only, a is not updated yet
res = a++;
printf("a is %d and res is %d\n", a, res); // a becomes 11 now
// post-decrement example:
// res is assigned 11 only, a is not updated yet
res = a--;
printf("a is %d and res is %d\n", a, res); // a becomes 10 now
// pre-increment example:
// res is assigned 11 now since a is updated here itself
res = ++a;
// a and res have same values = 11
printf("a is %d and res is %d\n", a, res);
// pre-decrement example:
// res is assigned 10 only since a is updated here itself
res = --a;
// a and res have same values = 10
printf("a is %d and res is %d\n", a, res);
return 0;
}
Output:
a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10
// not equal to
if (a != b)
printf("a is not equal to b\n");
else
printf("a is equal b\n");
return 0;
}
Output:
a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
return 0;
}
Output:
AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero
Note: Following additional exercises can be carried out by students who finish
above programs
Run 2:
Enter the coefficients:
1
5
4
2 Develop a C program that reads N integer numbers and arrange them in ascending or
descending order using selection sort and bubble sort technique.
Output(BUBBLE SORT)
Run 1:
Enter the size of n:
5
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
Output Matrix:
24 29
6 25
4 Develop a C program to search an element using Binary search and linear search techniques.
// BINARY SEARCH
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
//LINEAR SEARCH
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
OUTPUT
BINARY SEARCH
Enter number of elements
7
Enter 7 integers
-4 5 8 9 11 43 485
Enter the value to find
11
11 found at location 5.
LINEAR SEARCH
Enter the number of elements in array
5
Enter 5 numbers
56429
Enter the number to search
4
4 is present at location
5 Using functions develop a C program to perform the following tasks by parameter passing to
read a string from the user and print appropriate message for palindrome or not palindrome
sol #include <stdio.h>
#include <string.h>
void check(char [], int);
int main()
{
char word[15];
printf("Enter a string to check if it is a palindrome\n");
scanf("%s", word);
check(word, 0);
return 0;
}
void check (char word [], int index)
{
intlen = strlen(word) - (index + 1);
if (word[index] == word[len])
{
if (index + 1 == len || index == len)
{
printf("The entered word is a palindrome\n");
return;
}
check(word, index + 1);
}
else
{
printf("The entered word is not a palindrome\n");
}
}
}
Output
Run 1:
Madam
Run 3:
Enter a string to check if it is a palindrome
mam
6 Develop a C program to compute average marks of ‘n’ students (Name, Roll_No, Test Marks)
and search a particular record based on ‘Roll_No’.
sol #include<stdio.h>
#include<string.h>
struct student
{
char name[50];
int roll;
float marks[3];
}st[60];
int main()
{
int i,n,rollno;
float sum=0,avg;
printf("Enter the number of
students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter The name of %d
student\n",i+1);
scanf("%s",st[i].name);
printf("Enter The Roll no of
%d student\n",i+1);
scanf("%d",&st[i].roll);
printf("Enter The Marks of %d
student\n",i+1);
scanf("%f%f%f",&st[i].marks[0],&st[i]
.marks[1],&st[i].marks[2]);
}
for(i=0;i<n;i++)
{
sum= sum+ st[i].marks[0] +
st[i].marks[1] + st[i].marks[2];
}
avg= sum/n;
printf("The Average Marks Of %d
Students is : %f\n",n,avg);
printf("Enter the roll no. of student
detail required : ");
scanf("%d",&rollno);
for(i=0;i<n;i++)
{
if(st[i].roll==rollno)
{
printf("The name of
student is : %s\n",st[i].name);
printf("The 3 test
scores are :\n %f %f
%f",st[i].marks[0],st[i].marks[1],st[i].marks[2]
);
break;
}
else
continue;
}
if(i==n)
printf("Roll No. not found\n");
}
OUTPUT
enter the name of 1 student= Ramesh
7 Develop a C program using pointers to function to find given two strings are equal or not.
#include<stdio.h>
int compare_string(char*, char*);
main()
{
char first[100], second[100], result;
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}
OUTPUT
Run 1
Enter first string
RVCE
Enter second string
BANGALORE
Entered strings are not equal.
Run 2
Enter first string
CSE
Enter second string
CSE
Both strings are same.
8 Develop a C program using recursion, to determine GCD , LCM of two numbers and to
perform binary to decimal conversion.
sol (a)GCD and LCM
#include <stdio.h>
int find_gcd(int,int);
int find_lcm(int,int);
int main()
{
int num1,num2,gcd,lcm;
printf("\nEnter two numbers:\n ");
scanf("%d %d",&num1,&num2);
gcd=find_gcd(num1,num2);
if(num1>num2)
lcm = find_lcm(num1,num2);
else
lcm = find_lcm(num2,num1);
return temp;
}
void main ()
{
int decimalnum=0,binarynum,bitweight=0;
printf("Enter the binary number\n");
scanf("%d",&binarynum);
decimalnum=binary_to_decimal( binarynum,decimalnum,bitweight);
printf("%d in binary %d",decimalnum, binarynum);
}
-------------------------------------------------------------------------------------------------------------------------
Output:
int main() {
int n, i, *ptr, sum = 0;
return 0;
}
-----------------------------------------------------------------------------------------------------------
Output
(b)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
-----------------------------------------------------------------------------------------------------------
Output
(c)
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
free(ptr);
return 0;
}
-----------------------------------------------------------------------------------------------------------
Output
Enter size: 2
Addresses of previously allocated memory:
26855472
26855476
10 C Program to implement Singly Linked List to insert and delete a node from front and
display the contents of the Singly List.
Sol #include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
NODE getnode();
NODE insert_front(NODE , int);
NODE delete_front(NODE);
void display(NODE);
void main()
{
NODE first;
int choice, item;
first = NULL;
while(1)
{
printf("Enter\n");
printf("1. Insert Front\n");
printf("2. Delete Front\n");
printf("3. Display the list\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter item to be inserted\n");
scanf("%d", &item);
first = insert_front(first, item);
break;
case 2:
first = delete_front(first);
break;
case 3:
display(first);
break;
default:
exit(0);
}
}
}
NODE getnode()
{
NODE x;
if(x == NULL)
{
printf("Node creation error\n");
return;
}
return x;
}
temp = getnode();
temp->info = item;
temp->link = first;
return temp;
}
if(first == NULL)
{
printf("Cannot delete. Empty List\n");
return first;
}
temp = first;
first = first->link;
return first;
}
if(first == NULL)
{
printf("Cannot print. Empty list\n");
return;
}
temp = first;
while(temp != NULL) //as long as there are elemens in the linked list
{
printf("%d\t", temp->info);
temp = temp->link;
}
printf("\n");
}
---------------------------------------------------------------------------------------------------------
Output
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Cannot delete. Empty List
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
Cannot print. Empty list
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
10
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
20
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
30
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
40
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
1
Enter item to be inserted
50
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
50 40 30 20 10
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 50
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 40
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
2
Deleted node is 30
Enter
1. Insert Front
2. Delete Front
3. Display the list
4. Exit
3
Contents of linked list is:
20 10
11 Write a C program to count no of lines, blank lines and comments in a given program
using files.
Sol
#include <stdio.h>
void main()
{
int line_count = 0, n_o_c_l = 0, n_o_n_b_l = 0, n_o_b_l = 0, n_e_c = 0;
FILE *fp1;
char ch;
fp1 = fopen("file.txt", "r");
department of
CSE RVCE
//this is a comment
--------------------------------------------------------------------------------------------------------------------
Output:
Total no of lines: 5
Total no of comment line:1
Total no of blank lines:2
Total no of non blank lines:3
Total no of lines end with semicolon:0
PART B
Design and development of a working model using any of the following combination of hardware
and software.
Develop a model that helps the user to monitor weather, health condition, environment
parameters, etc. using Arduino board.
Develop a simple Robot that can assist the user to perform simple activities home sanitization,
lifting things etc. using Raspberry pi.
Hardware interfacing (Ardunio Board, Finch, Lego WeDo 2.0) with scratch to design various
models to solve simple problems.
Develop applications using Nvidia Jetson Kit.
Laboratory Evaluation
Each program is evaluated for 10 marks.
Lab Write-up &Execution rubrics (Max: 6 marks) and Viva Voce rubrics (Max: 4 marks)
PROGRAM BANK
SET 1
Sl Question
NO.
1 Distinguish between system software, application software and utility software.
2 Write the algorithm and flowchart to do the following :
(a) Check whether a year given by the user is a leap year or not.
(b) Given an integer number in seconds as input, print the equivalent time in hours,
minutes, and seconds as output. The recommended output format is something like:
7,322 seconds is equivalent to 2 hours 2 minutes 2seconds.
(c) Print the numbers that do not appear in the Fibonacci series. The number of terms to
be printed should be given by the user.
(d) Convert the binary equivalent of an integer number.
(e) Find the prime factors of a number given by the user.
(f) Check whether a number given by the user is a Krishnamurthy number or not. A
Krishnamurthy number is one for which the sum of the factorials of its digits equals the
number.
For example, 145 is a Krishnamurthy number.
(g) Print the second largest number of a list of numbers given by the user.
(h) Find the sum of N odd numbers given.
(i) Compute the sum of squares of integers from 1 to 50.
3 Which of the following is an incorrect assignment statement?
(a) n = m = 0
(b) value += 10
(c) mySize = x < y ? 9 : 11
(d) testVal = (x > 5 || x < 0)
(e) none of the above
4 What will be the output:
(a)
int main()
{
fl oat c= 3.14;
printf(“%f”, c%2);
return 0;
}
9. #include <stdio.h>
int main()
{
int i = 3;
switch (i)
{
case 0+1: printf("1");
break;
case 1+2: printf("2");
break;
default: printf("3");
}
return 0;
}
11. #include<stdio.h>
int main()
{
int n;
for (n = 9; n!=0; n--)
printf("n = %d", n--);
return 0;
}
12. #include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];
return 0;
}
14 #include <stdio.h>
int main()
{
int arr[2];
return 0;
}
15. Write the program to solve following Problems:
1. Accept a list of data items and find the second largest and second smallest elements
in it
2. Copy element of one array into another
3. Cyclically permute the elements of an array
4. Delete duplicate elements in an array
5. Delete the specified integer from the list
6. Find unique element in two arrays
7. Minimum element location in array
8. Accept an array of 10 elements and swap 3rd element with 4th element using
pointers
A. Sequential
B. Random
C. Sequential and Random
D. None of the above
19 void main()
{
char str1[] = "abcd";
char str2[] = "abcd";
if(str1==str2)
printf("Equal");
else
printf("Unequal");
}
20. What will be the output of the program ?
#include
void main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d", sizeof(arr)/sizeof(arr[0]));
}
21. The function that is used to find the first occurrence of a given string in another string is:
22. #include <stdio.h>
int main()
{
char st[] = "CODING";
printf("While printing ");
printf(", the value returned by printf() is : %d",
printf("%s", st));
return 0;
}
23 Print the following pattern on the screen
****
**
*
**
****
24 #include <stdio.h>
int main()
{
long int n = 123456789;
return 0;
}
25 #include <stdio.h>
int main()
{
char a[100], b[100], c[100];
return 0;
}
26 Write a program to reverse digits of a number.
27 Write a program to reverse an array or string.
28.
List of switch case statement programs in C:
C program to read weekday number and print weekday name.
C program to check whether a character is VOWEL or CONSONANT
29
Examples for sentinel control loop & counter control Loop?
30 #include <stdio.h>
void main()
{
int x=22;
if(x=10)
printf("TRUE");
else
printf("FALSE");
}
31 What will be the output of following program?
#include <stdio.h>
void main()
{
if(!printf(""))
printf("IF");
else
printf("ELSE");
}
32 #include <stdio.h>
int main()
{
if( (-100 && 100)||(20 && -20) )
printf("%s","Condition is true.");
else
printf("%s","Condition is false.");
return 0;
}
33 #include<stdio.h>
int main()
{
int i = 5, j = 6, k = 7;
if(i > j == k)
printf("%d %d %d", i++, ++j, --k);
else
printf("%d %d %d", i, j, k);
return 0;
}
34. # include <stdio.h>
int main()
{
int i = 0;
for (i=0; i<20; i++)
{
switch(i)
{
case 0:
i += 5;
case 1:
i += 2;
case 5:
i += 5;
default:
i += 4;
break;
}
printf("%d ", i);
}
return 0;
}
35. #include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
default:
a = 4;
case 6:
a--;
case 5:
a = a+1;
case 1:
a = a-1;
}
printf("%d n", a);
return 0;
}
36 Which combination of the integer variables x, y and z makes the variable a get the value 4
in the following expression?
a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z )
SET 2
1 Simple computational problems using arithmetic expressions and use of each operator
leading to implementation of a commercial calculator with appropriate output.
2 Write a C program to swap two numbers using pointers (Call by reference).
3 Create a structure called student with the following members student name, roll no, and a
structure with marks details in three tests. Write a C program to create N records and
a) Search on roll no and display all the records.
b) Average marks in each test.
c) Highest marks in each test.
4 Write a C program to check whether the entered year is leap or not.
5 Design a structure 'Complex' and write a C program to perform the following operations:
i. Reading a complex number.
ii. Addition of two complex numbers.
6 Write a C program to input a number and check whether the number is palindrome or not.
7 Compute the roots of the equation ax2 + bx + c = 0 and print using three-decimal places.
The roots are real −b±√D/ 2aif the discriminant D = b2−4ac is non-negative. If the
discriminate is negative, then the roots are complex conjugate−b /2a ±√−Di/ 2a.The
program proceeds in the following steps.
a) The program should accept the values of a, b and c from the keyboard.
a) No solution if both a and b are zero. The program terminates with appropriate
message.
b) Linear equation if a = 0 but b ≠ 0 and the root is −c/b. The program prints out the
root with appropriate message and the program terminates.
c) Calculate the discriminant D and determines the corresponding roots.
d) Display all possible roots of a quadratic equation with appropriate message.
8 Write a program to print the prime numbers in a range entered by user.
9 Design and develop using an iterative problem solving approach for Taylor series
approximation for five decimal digits to compute Sin(x)= x - x3/3! + x5/5! - x7/7! +
x9/9!. ............ Xn / n!. Read the value for N.
10 Write a program to print out a multiplication table as given below.
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
11 Write a C program to read a list of N integer numbers in an array and print the following:
(i) the maximum value
(ii) the minimum value
(iii) the range
Hint: This is computed as maximum-minimum.
(iv) the average value
Hint: To compute this, add all the numbers together into sum and count them all in Count.
The average is Sum/Count
12 Write a C program to find the length of the string without using library function.
13 Write a C program to count vowels and consonants in a string using pointer.
14 Write a C program to generate any one patterns as given below : (Accept the number of rows
from user)
i) ( to print * if it is even number) ii)
1 55555
** 4444
333 333
**** 22
55555 1