IT Lab-1
IT Lab-1
solving using C
Laboratory
Workbook
Name
Yuvanvenkatesh G
Roll No.
20BIT030
Branch
B.Tech IT
Year/Section
2020/B Section
Semester
1st Semester
Academic Year
2020-2025
19ITSN2101 – Problem solving using C
Name : Yuvanvenkatesh G
Class : B.Tech IT
RollNo. : 20BIT030
Certified that this is bonafide record of work done by the above student
of the
during theyear
_
Head oftheDepartment StaffIn-
Charge
Submitted for the Final Assessment heldon
List of Exercises
Average(out of 75)
Needs
Criteria Excell Good Satisfact
Improvem
ent ory
ent
Marks: Marks: Marks:15 Marks:10
20 18
Preparati Procedure for Procedure for Procedure for Procedure for
implementatio implementation implementatio implementatio
on (20 n is clearly is clearly defined n is partially n is partially
defined. with missing defined. defined with
Marks) parameters. missing
parameters.
Marks: Marks: Marks:19 Marks:15
25 23
Observati Implementation Implementation Implementation Implementat
addresses all addresses addresses most ion
on (25 requirements of almost all of the addresses
the problem requirements of requirements of few
Marks) statement. the problem the problem requirement
statement. statement. s of the
problem
statement.
Marks: Marks: Marks:10 Marks:5
Interpretation
20 15
of Result Successful Successful Successful Execution of
execution of all execution of execution of few test cases
(20 Marks)
test cases almost all test most of the
cases testcases
Ex.No:1
Data Types, Operators and Expression
Date:
Evaluation
Aim:
Problem Statement:
Algorithm:
Step 1:Start
Step 5: Stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
inta,b;
clrscr();
printf(“Enter Length of Rectangle : “);
scanf(“%d”,&a);
scanf(“%d”,&b);
getch();
Output:
Algorithm:
Step 1:Start
Step 6: Stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
intp,n,r,si;
clrscr();
scanf(“%d”,&p);
printf(“Enter No Of Years:”);
scanf(“%d”,&n);
scanf(“%d”,&r);
si=(p*n*r)/100;
getch();
Output:
• To find the CompoundInterest:
Algorithm:
Flowchart:
Program:
Output:
b. If the marks of Robert in three subjects are 78,45 and 62 respectively (each
out of 100 ), write a program to calculate his total marks and percentage
marks.
d. Write a program to enter the values of two variables from the keyboard and
then interchange the values of the two variables. E.g.-
If entered value of x is 5 and y is 10 then
printf("%d %d",x,y )should print 10 5.
Algorithm:
Step 1: Start
Step 7: stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
float m1=78,m2=45,m3=62,t,p;
clrscr();
t=m1+m2+m3;
p=(t/300)*100;
printf(“\nPersentage : %f”,p);
getch();
Output:
To find Size of given data types:
Algorithm:
Step 1: Start
Step 7: stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
clrscr()
getch();
Output:
Algorithm:
Step 1: Start
Step 8: Stop
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b;
clrscr();
scanf(“%d”,&a);
scanf (“%d”,&b);
a=b+a;
b=a-b;
a=a-b;
getch();
Output:
Ex.No:2
Decision and Looping Statements
Date:22.12.2020
Aim:
To develop C programs to implement decision making and looping statements.
a. C program to check whether the entered year is leap year or not using if..else
Algorithm:
Step1: Start
Step2: Delclare variable y
Step3:Get value of y
Step 4: if y%4==0 and y%100!=0 or y%400%==0 then print leap year
Step5: else then print not a leap year
Step6: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("Enter the year : ");
scanf("%d",&y);
if((y%4==0) && ((y%100!=0) || (y%400==0)))
{
printf("Year %d is Leap Year",y);
}
else
{
printf("Year %d is not Leap Year",y);
}
getch();
}
Output:
b. C program to get 4 numbers as input and find the largest among the 4 numbers using
if..else if ladder
Algorithm:
Step1: Start
Step2: Declare variable a,b,c,d
Step3: Get value of a,b,c,d
Step4: if (a>=b and a>=c and a>=d) then print a is largest
Step5: else if (b>=a and b>=c and b>=d) then print b is largest
Step6: else if (c>=a and c>=b and c>=d) then print c is largest
Step7: else then print d is largest
Step8: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("Enter 1st Value : ");
scanf("%d",&a);
printf("Enter 2nd Value : ");
scanf("%d",&b);
printf("Enter 3rd Value : ");
scanf("%d",&c);
printf("Enter 4th Value : ");
scanf("%d",&d);
if(a>=b && a>=c && a>=d)
{
printf("The largest among the 4 numbers = %d",a);
}
else if(b>=a && b>=c && b>=d)
{
printf("The largest among the 4 numbers = %d",b);
}
else if(c>=a && c>=b && c>=d)
{
printf("The largest among the 4 numbers = %d",c);
}
else
{
printf("The largest among the 4 numbers = %d",d);
}
getch();
}
Output:
c. Using Switch statement, write a C program that displays the following menu
for the food items available to take order from the customer:
• B= Burger
• F= French Fries
• P= Pizza
• S= Sandwiches
The program inputs the type of food and quantity. It finally displays the
total charges for the order according to following criteria:
• Burger = Rs. 200
• French Fries= Rs. 50
• Pizza= Rs. 500
• Sandwiches= Rs. 150
Algorithm:
Step1: Start
Step2: Declare variable c,q
Step3: Display food items available
Step4: Get food item code in c
Step5: Get amount of quantity required in q
Step6: Display total amount of order by switch statement
Step7: Display Thankyou For Your Order
Step8: Stop
Program:
#include<stdio.h>
#nclude<conio.h>
void main()
{
char c;
int q;
clrscr();
printf("Foods Items Available\n");
printf("1.Burger(B)\n");
printf("2.French Fries(F)\n");
printf("3.Pizza(P)\n");
printf("4.Sandwiches(S)\n");
printf("Enter your order item code : ");
scanf("%c",&c);
printf("Enter no of quantity required : ");
scanf("%d",&q);
switch(c)
{
case 'B':
{ printf("\nTotal amount for your order : %d",(200*q));
break;}
case 'F':
printf("\nTotal amount for your order : %d",(50*q));
break;
case 'P':
printf("\nTotal amount for your order : %d",(500*q));
break;
case 'S':
printf("\nTotal amount for your order : %d",(150*q));
break;
default:
printf("\nInvalied Choice");
}
printf("\nThanks For Your Order");
getch();
}
Output:
Algorithm:
Step1:Start
Step2:Declare variable a=0,b=1,c,n
Step3:Get n value
Step4:Run the loop till n-2
Step5:Set c=a+b
Step6:Display c
Step7: Set a=b,b=c
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a=0,b=1,c;
clrscr();
printf("Enter Limit of Fibonacci Series : ");
scanf("%d",&n);
printf("\n%d\n%d",a,b);
for(i=0;i<(n-2);i++)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
}
getch();
}
Output:
2. Write a program in C to display the pattern like right angle triangle using an
asterisk.
*
**
***
****
Algorithm:
Step1:Start
Step2:Declare variable i=1,j=1
Step3:Run outer loop untill i<5
Step4:Run inner loop until j<=i
Step5:display *
Step6:stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}
Output:
3. Write a program in C to display the pattern like right angle triangle with a
number.
1
12
123
1234
Algorithm:
Step1:Start
Step2:Declare variable i=1,j=1
Step3:Run outer loop untill i<5
Step4:Run inner loop until j<=i
Step5:display j
Step6:Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
Output:
Ex.No:3
Arrays and Strings
Date:19.1.2021
Aim:
To develop C programs to implement arrays and Strings.
Algorithm:
Step1:Start
Step2:Get N numbers from user and store in a[100] 1d array
Step3:Get the value to be search in variable p
Step4:Compare p with one by one index value of a[n]
Step5:if p=a[i] go to step 6 else go to step 7
Step6:display value found in index i
Step7:else display not found
Step8:End
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[100],n,i,p,f=0;
clrscr();
printf("Enter the Limit : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter value of a[%d] : ",i);
scanf("%d",&a[i]);
}
printf("Enter Element to be search : ");
scanf("%d",&p);
for(i=0;i<n;i++)
{
if(p==a[i])
{
printf("The value %d is in index a[%d]",p,i);
f=1;
break;
}
}
if(!f)
{
printf("The value %d is not found",p);
}
getch();
}
Output:
b. To compute matrix addition (2 D array)
Algorithm:
Step1:Start
Step2:Get row size and coloumn size
Step3:Get the first matrix value in a[row][col] and second matrix value in b[row][col]
Step4:for i=0 to row
Step5:for j=0 to col
Step6:Set ans[i][j]=a[row][col]+b[row][col]
Step7:display ans[i][j]
Step8:End
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],row,col,ans[10][10],i,j;
clrscr();
printf("Enter the No Of Row:");
scanf("%d",&row);
printf("Enter the No Of Culomn:");
scanf("%d",&col);
printf("Enter 1st Matrix Values\n");
for( i=0;i<row;i++)
{for( j=0;j<col;j++)
{
printf("Enter [%d][%d] Value:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter 2nd Matrix Value\n");
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
{printf("Enter [%d][%d] Value:",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Addision of two Matrix");
for( i=0;i<row;i++)
{ printf("\n");
for( j=0;j<col;j++)
{
ans[i][j]=a[i][j]+b[i][j];
printf("%d ",ans[i][j]);
}
}
getch();
}
Output:
Ex.No:4
Functions and Strings
Date:
Aim:
To develop C programs to implement Functions and Strings.
Program:
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("Enter A and B value:");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("The Values Swaped A=%d and B=%d",a,b);
getch();
}
void swap(int *x,int *y)
{
*y=*y+*x;
*x=*y-*x;
*y=*y-*x;
}
Output:
b. To find the cube of a number using recursion
Algorithm:
Step1:Start
Step2:Get value in the variable a
Step3:Send value of a to function cube
Step4:Set static i=0
Step5:Set i=i+1
Step6:if i<3 go to step7 else goto step8
Step7:return b*cube(b)
Step8:return b
Step9:Stop
Program:
#include<stdio.h>
#include<conio.h>
int cube(int b);
void main()
{
int a;
clrscr();
printf("Enter integer value : ");
scanf("%d",&a);
printf("Cube value of %d : %d",a,cube(a));
getch();
}
int cube(int b)
{
static int i=0;
i++;
if(i<3)
{
return (b*cube(b));
}
else
{
return b;
}
}
Output:
Algorithm:
Step1:Start
Step2:Include string.h header file
Step3:Get 2 strings in variable a and b
Step4:Do string operation using inbuild string functions
Step5:Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[20];
clrscr();
printf("Enter string1 : ");
gets(a);
printf("Enter string2 : ");
gets(b);
printf("Length of string1 : %d\n",strlen(a));
printf("Copy \"Night\" to string2 : %s\n",strcpy(b,"Night"));
printf("Combine two strings and store in string1 : %s\n",strcat(a,b));
printf("Check string1 and string2 are same : %d\n",strcmp(a,b));
printf("First oocurrence of \"odNi\" in string1 : %s\n",strstr(a,"odNi"));
printf("String1 in upper case : %s\n",strupr(a));
printf("String2 in lower case : %s\n",strlwr(b));
printf("Reverse the string1 : %s\n",strrev(a));
printf("Enter string1 : ");
gets(a);
printf("Enter string2 : ");
gets(b);
printf("Combine 1st 5 character of string2 to string1 : %s\n",strncat(a,b,5));
printf("Copy 1st 5 character of string1 to string2 : %s\n",strncpy(b,a,5));
printf("Compare 1st 5 character of string1 and string2 : %d",strncmp(a,b,5));
getch();
}
Output:
Algorithm:
Step1:Start
Step2:Get String in variable s
Step3:Set i=0,j=length of s – 1 repeat Step4 till i<=j
Step4:if s[i]==s[j] goto step5 else goto step6
Step5:Set f=0
Step6:Set f=1 goto step 7
Step7:if f=0 print palindrome else goto spep8
Step8:print not palindrome
Step9:Stop
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[50];
int i,j,f;
clrscr();
printf("Enter the string : ");
gets(s);
j=strlen(s)-1;
for(i=0,j;i<=j;i++,j--)
{
if(s[i]==s[j])
{
f=0;
}
else
{
f=1;
break;
}
}
if(f==0)
{
printf("The given string is palindrome");
}
else
{
printf("The given string is not palindrome");
}
getch();
}
Output:
Result: