BPOP103_203_C Programming Lab Manual
BPOP103_203_C Programming Lab Manual
LAB MANUAL
COMPUTER PROGRAMMING LABORATORY
(BPOPS103/203)
Semester II
Course Objectives
1. Explain problem statements and identify appropriate solutions
2. Demonstrate the use of IDE, C Compiler, and identify and rectify the syntax and syntactic errors
during programming.
3. Development of algorithms and programs using constructs of C programming language
4. Reporting the observations
Prerequisites
Prog P P P P
P P P P P P P P P P P P
rams S S S S
Course Outcomes O O O O O O O O O O O O
cove O O O O
1 2 3 4 5 6 7 8 9 10 11 12
red 1 2 3 4
Develop algorithm, 3 2 2 2 2 - - - 2 - - 2 2 - 2 -
flowchart for
implementation of 1-11
solution of the given
CO1 problem in C Language
CORRELATION
PROGRAM OUTCOMES (PO), PROGRAM SPECIFIC OUTCOMES (PSO)
LEVELS
PSO1 Design and develop applications using different stacks of web and programming technologies.
PSO2 Design and develop secure, parallel, distributed, networked, and digital systems.
PSO3 Apply software engineering methods to design, develop, test and manage software systems.
PSO4 Design and develop intelligent applications for business and industry.
Introduction
C is a high level language, and it needs a compiler to convert it into an executable code. Then, we
can execute the program by running the executable code or file.
A compiler is a software that converts a program written in High Level Language to Machine
Language. Few examples of compiler are cc, gcc, cpp, java etc.
I Execution of C program:
1. To edit a C program
If gedit icon is not present in the taskbar, click on ‘Search your Computer’ icon (The very first
icon at the top left corner of the monitor) and type ‘e’ in the search box. Text editor icon is
displayed. Drag the icon and place it in the taskbar and double click the gedit icon.
The editor opens with an untitled document. Now, type the code for the program. Let us type the
very first program of C - The Hello World program. Type the following program in the editor.
Note: The C compiler is case sensitive, it treats the file as C program file, only if the extension
name is “.c” and not “.C”.
To compile the program, open terminal window and at the prompt, type the following command
and press enter key.
$ cc hello.c
An output file (executable file) with name a.out is created if there are no errors in the program. In
case there are errors/warnings, they are displayed on the terminal window.
$./a.out
The program gets executed and the output is displayed on the terminal window.
List files and/or directories. If no argument is given, the contents of current directory are shown.
$ ls
example file1.txt file2.txt file3.txt
If a directory is given as an argument, files and directories in that directory are shown.
$ ls /usr
bin games include lib lib64 local sbin share src
‘ls -l’ displays a long listing of the files.
$ ls -l
total 4
2. cd
The cd command - change directory - will allow the user to change between file directories. As
the name command name suggest, you would use the cd command to circulate between two
different directories.
Ex:
$ pwd
/home/raghu
$ cd /usr/share/
$ pwd
/usr/share
$ cd doc
$ pwd
/usr/share/doc
$cd .. To exit from current directory
$pwd
/usr/share
3. mv – move command
$ mv source destination
Move files or directories. The 'mv' command works like 'cp' command, except that the original file
is removed. But, the mv command can be used to rename the files (or directories).
4. pwd - present working directory
‘pwd’ command prints the absolute path to current working directory.
$ pwd
/home/raghu
5. mkdir
rm files|directories
$ rm file2
'rmdir' command removes any empty directories, but cannot delete a directory if a file is present
in it. To use ‘rmdir’ command, you must first remove all the files present in the directory you
wish to remove (and possibly directories if any).
7. cat
The 'cat' command is actually a concatenator but can be used to create and view the contents of a
file..
$cat filename
$ cat>filename
Press enter, then you can write something in the file and then to save the file contents press
ctrl+d then enter.
8. touch
12. clear
The clear command does exactly what it says. The clear command clears the screen and wipes the
board clean. Using the clear command will take the user back to the start prompt of whatever
directory you are currently operating in. To use the clear command simply type clear.
ALGORITHM:
Algorithm:
Step 1: Start
Step 2: Read two operands and an arithmetic operator
Step 3: Check if operator equals ‘+’, if yes, then goto step 4 else goto step 5
Step 4: Compute addition operation - res = num1 + num2 and goto step 18
Step 5: Check if operator equals ‘-’, if yes, then goto step 6 else goto step 7
Step 6: Compute subtraction operation - res = num1 – num2 and goto step 18
Step 7: Check if operator equals ‘*’, if yes, then goto step 8 else goto step 9
Step 8: Compute multiplication operation - res = num1 * num2 and goto step 18
Step 9: Check if operator equals ‘/’, if yes, goto step 10 else goto step 13
Step 10: Check if num2 equals Zero, if yes, then goto step 11 else goto step 12
Step 11:Display – “Divide by zero error.“ and goto step 19
Step 12: Compute division operation - res = num1 / num2 and goto step 18
Step 13: Check if the operator equals ‘%’, if yes, then goto step 14 else goto step 17
Step 14: Check if num2 equals zero, if yes, then goto step 15 else goto step 16
Step 15: Display - “Divide by zero error.” and goto step 19.
Step 16: Compute modulus operation – res = num1 % num2 and goto step 18
FLOWCHART:
if (op == '+')
{
result=num1+num2;
}
else if (op == '-')
{
result=num1-num2;
}
else if (op == '*')
{
result=num1*num2;
}
else if (op == '/')
{
if (num2 == 0)
{
printf("Divide by zero error \n");
return (1);
}
else
Department of Computer Science & Engineering, CMRIT, Bangalore
{
result=num1/num2;
}
}
else if (op == '%')
{
if (num2 == 0)
{
printf("Divide by zero error \n");
return (2);
}
else
{
result=num1%num2;
}
}
else
{
printf("Invalid operator...\n");
return (3);
}
SAMPLE OUTPUT:
AIM: Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
ALGORITHM:
1. Start
2. Read three non zero coefficients a,b and c.
3. Check if a is equal to zero. If true, go to step 4 else go to step 5.
4. Display the equation is linear and go to step 11.
5. Calculate the discriminant as follows:
D= b*b - 4* a*c
6. Check if discriminant is 0. If true, go to step 7, else go to step 8.
7. Compute roots as follows:
Root1 = -b/2*a
Root2 = root 1
Display: Roots are real and equal. Root1. Go to step 11.
8. Check is discriminant is greater than 0. If true, go to step 9 else go to step 10.
9. Compute roots as follows:
root1= -b + sqrt(D)/(2*a)
Root2 = -b -sqrt(D)/(2*a)
Display: Roots are real and distinct. Root1 and Root2. Go to step 11.
10. Calculate
Rpart = -b/2*a
Ipart = sqrt(-D)/(2*a)
Display. Roots are imaginary. Rpart and ipart. Go to step 11.
11. Stop
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,desc,r1,r2,realpart,imgpart;
if(a == 0)
{
printf("Coefficient of a cannot be zero....\n");
printf("Please try again....\n");
return 1;
}
desc=(b*b)-(4.0*a*c);
if(desc==0)
{
printf("The roots are real and equal\n");
r1=r2=(-b)/(2.0*a);
printf("The two roots are r1=r2=%f\n",r1);
}
else if(desc>0)
{
printf("The roots are real and distinct\n");
r1=(-b+sqrt(desc))/(2.0*a);
r2=(-b-sqrt(desc))/(2.0*a);
printf("The roots are r1=%f and r2=%f\n",r1,r2);
}
else
{
printf("The roots are imaginary\n");
realpart=(-b)/(2.0*a);
imgpart=sqrt(-desc)/(2.0*a);
printf("The roots are r1=%f + i %f\n",realpart,imgpart);
printf("r2=%f - i %f\n",realpart,imgpart);
}
SAMPLE OUTPUT:
cc Prog2.c -lm
./a.out
Enter the coefficients of a, b and c :1 2 1
The roots are real and equal
The two roots are r1=r2=-1.000000
./a.out
Enter the coefficients of a, b and c :1 -2 1
The roots are real and equal
The two roots are r1=r2=1.000000
./a.out
Enter the coefficients of a, b and c :1 7 12
The roots are real and distinct
The roots are r1=-3.000000 and r2=-4.000000
./a.out
Enter the coefficients of a, b and c :1 1 1
The roots are imaginary
The roots are r1=-0.500000 + i 0.866025
r2=-0.500000 - i 0.866025
ALGORITHM:
Input: Customer name and unit consumed
Output: Customer name, units consumed and total amount to be paid
1. Start
2. Read the name of customer and the unit consumed by the customer
3. Check if the unit consumed is greater than 1 and less than 200,if true goto step 4 else goto
step 5
4. Compute: Charge=0.8*unit consumed
5. Check if unit is greater than or equal 201 and less than 300,if true goto step 6 else goto
step 7
#include <stdio.h>
int main()
{
float unit,total,charge;
char name[20];
charge=0;
if(unit>=1&&unit<=200) // Charge 80 paise per unit for the first 200 units
{
charge=0.8 * unit;
}
else if(unit>=201&&unit<=300) // Charge 80 paise per unit for the first 200 units and
{ // 90 paise per unit for the next 100 units
charge = 0.8*(200) + 0.9*(unit-200);
}
else if (unit > 300) //Charge 80 paise per unit for the first 200 units,
{ // 90 paise per unit for the next 100 units and
// one rupee per unit for all units above 300
charge = 0.8*(200) + 0.9*(100) + 1*(unit-300);
}
total=charge+100; // Add Meter charge of Rs. 100
if(total>400)
{
total = total + (0.15*total); // Add additional surcharge of 15 percent of total
amount
}
return 0;
}
SAMPLE OUTPUTS:
1.
$ cc prog3.c
$ ./a.out
Enter the name : Anitha
Enter the units : 0
ELECTRICITY BILL
----------------
Name : Anitha
No. of units: 0.00
Total Bill Amount: Rs. 100.00
----------------
2.
$ ./a.out
Enter the name : Harish
Enter the units : 167
ELECTRICITY BILL
----------------
Name : Harish
No. of units: 167.00
Total Bill Amount: Rs. 233.60
----------------
3.
$ ./a.out
Enter the name : Anuradha
ELECTRICITY BILL
----------------
Name : Anuradha
No. of units: 598.00
Total Bill Amount: Rs. 745.20
----------------
4.
$ ./a.out
Enter the name : Rajeev Gupta
Enter the units : 876
ELECTRICITY BILL
----------------
ALGORITHM:
1. Start
2. Read size of the array and list of elements which are not sorted.
3. Use swap function in a loop to sort the given array.
4. Display the sorted list of elements.
5. Read the element to be searched. key.
6. Check if first is not equal to last. If true, find the middle element.
7. A. Check if the key is equal to the middle element. If true, element is found at n/2 index
and exit loop. Go to step 8.
B. Else, check if the key is greater than middle element. If true, change first to mid. Go to
step 6.
C. Else, check if the key is smaller than middle element. If true, change last to mid. Go to
step 6.
8. Stop.
#include <stdio.h>
int main()
{
// Define variables
int a[20];
int n,i,j,temp,key;
int first,mid,last;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
SAMPLE OUTPUT:
Sample Output 1: Search for the first element in the array
$ ./a.out
Enter the size of the array :5
Enter 5 elements :87
0
4
2
3
The elements of the array before sorting is ----
87 0 4 2 3
$ cc prog6.c
$ ./a.out
Enter the size of the array :5
Enter 5 elements :2
3
4
87
0
The elements of the array before sorting is ----
2 3 4 87 0
$ ./a.out
Enter the size of the array :5
Enter 5 elements :2
0
4
3
87
The elements of the array before sorting is ----
2 0 4 3 87
$ ./a.out
Enter the size of the array :5
Enter 5 elements :3
2
4
0
87
The elements of the array before sorting is ----
3 2 4 0 87
PROBLEM STATEMENT: Write a C program to input matrices A and B. Check for the number
of columns in first matrix is equal to number of rows in second matrix. If true, perform matrix
multiplication.
ALGORITHM:
Input: Two matrices A and B.
Output: Result of A x B.
1. Start
2. Input 2 matrices A and B and store them in 2D arrays. Display A and B.
3. Check multiplication condition where the number of columns in A is equal to the number
of rows in B. If true, go to step 4. If false, display that matrix multiplication is not possible.
4. Perform matrix multiplication. Display resultant matrix.
5. Exit.
FLOWCHART
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q;
int i,j,k;
return 0;
}
SAMPLE OUTPUT:
1.
$cc prog5.c
$./a.out
Enter the order of matrix A :2
2
Enter the order of matrix B:2
5 11
11 25
2.
$./a.out
Enter the order of matrix A :3
2
Enter the order of matrix B:2
3
16 38 60
10 24 38
4 10 16
AIM
Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the built-in
library function. Print both the results with appropriate inferences.
PROBLEM STATEMENT
Draw the flowchart and write a C program to compute Sin(x) using Taylor series approximation
given by Sin(x) = x - (x3/3!) + (X5/5!) - (x7/7!) + …. Compare your result with the built-in library
function. Print both the results with appropriate messages
ALGORITHM
Input: The value of x in degree
Output: To compute Sin(x) using Taylor Series.
1. Start
2. Read the value of x in degree
3. Compute the value of x in radian: x = degree* (3.142857/180.0)
4. Initialise:
sum=0;
nume=x;
deno=1.0;
i=1;
5. compute term as , term=nume/deno;
Add term value to sum: sum=sum+term;
i=i+2;
compute next numerator and denominator values
nume= - nume*x*x
deno=deno*i*(i-1)
print term value
6. Check if the absolute value of term is greater than 0.0001. If true goto Step 5 else goto Step
7.
7. Display sin(x) using iteration : sum and display sin(x) using library function: sin(x)
8. Stop
Start
Initialize:
sum=0, nume=x, deno=1.0, i=1
do
// compute term
term=nume/deno;
// Add term value to sum
sum=sum+term;
i=i+2;
// compute next numerator and denominator
values
nume= - nume*x*x;
deno=deno*i*(i-1);
display term value
while (fabs(term)
>= 0.00001)
True
Stop
—-------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
#define PI 3.142857
int main()
do
{
// compute term
term=nume/deno;
// Add term value to sum
sum=sum+term;
i=i+2;
// compute next numerator and denominator values
nume= -nume*x*x;
deno=deno*i*(i-1);
// printf("Term=%f\n",term);
} while (fabs(term) >= 0.00001);
return 0;
}
SAMPLE OUTPUT:
1.
$cc prog6.c -lm
$./a.out
Enter degree:60
2.
$ ./a.out
Enter degree:30
Computed value of Sin(30.000000)=0.500182
Value from library function is sin(30.000000) = 0.500182
3.
$ ./a.out
Enter degree:90
Computed value of Sin(90.000000)=1.000000
Value from library function is sin(90.000000) = 1.000000
4.
$ ./a.out
Enter degree:45
Computed value of Sin(45.000000)=0.707330
Value from library function is sin(45.000000) = 0.707330
5.
$ ./a.out
Enter degree:0
Computed value of Sin(0.000000)=0.000000
Value from library function is sin(0.000000) = 0.000000
*/
PROBLEM STATEMENT
Develop an algorithm, implement and execute a C program that reads N integer numbers and
arrange them in ascending order using BUBBLE SORT
ALGORITHM
1. Start
2. Read the size of array: a[n]
3. Read the elements of array: n
4. Check if i<n-1 where i is the position of elements in the array and n is the total count of
elements in the array. If the condition is true go to step 5 and increment i value. Repeat this step
until the condition is false. If the condition is false go to step 8.
5. Check if j<n-1-i, if true go to step 6 and increment j value. Repeat this step until the condition
is false. If the condition is false go to step 4.
6. check if (a[j] > a[j+1]), if true go to step 7 else go to step 5
7. Compute:
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
8. Display the elements of array in sorted order: a[n]
9. Stop
#include<stdio.h>
int main()
{
int a[20],n,i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
SAMPLE OUTPUT
1.
$ cc prog7.c
$ ./a.out
Enter the number of elements :5
Enter 5 integers :1
3
4
2
5
The sorted array is ....
1 2 3 4 5
2.
$ ./a.out
Enter the number of elements :6
Enter 6 integers :23
55
10
-10
0
-400
The sorted array is ....
-400 -10 0 10 23 55
AIM: Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
ALGORITHM:
10.a.String length:
1. Start
2. Read the string given by the user
3. To find the length of string, call the function str_length (str)
4. In str_length (str): count the characters in the string one by one until it encounters null
character then goto step 5
5. Display the length of string.
10.b String Compare:
1. Start
2. Read two strings str1 and str2 given by the user
3. To find comparison between two strings goto step 4
4. Call the function str_compare(str1,str2)
a. Compare ASCII value of a character in string1 with string2,if both value equals then
goto step 5
b. Check if ASCII value of character in string1 is greater than string2 character then
goto step 6
c. Check if ASCII value of character in string1 is lesser than string2 character then goto
step 7
5. Display both strings are same
6. Display String1 is greater than String2
7. Display String1 is lesser than String2
8. Stop
10. c. String Concatenation
1. Start
2. Read two strings str1, str2 from user
3. Call the concatenation function str_concat (str1, str2)
a. Find the last character in string1 by checking the null character,then copy each
character in string2 to string1
b. After concatenation of string2 to string1, append null character at the end of string1.
FLOWCHART
#include <stdio.h>
// Prototypes for the user defined functions
int str_length(char []);
int str_compare(char [], char []);
void str_concat(char [], char []);
int main()
{
// Declare the variables
char str[50];
char str1[50], str2[50];
char str_des[100], str_src[50];
// Invoke string compare function to compare the str1 and str2 strings
comp_res=str_compare(str1,str2);
if (comp_res < 0)
{
printf("String \"%s\" is less than string \"%s\"\n",str1,str2);
}
else if (comp_res == 0)
{
printf("String \"%s\" is same as string \"%s\"\n",str1,str2);
return 1;
}
for(j=0;s2[j] != '\0';i++,j++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
}
SAMPLE OUTPUT:
1.
$ cc prog8.c
$ ./a.out
2.
$ ./a.out
AIM
Implement array of structures to read, write, compute average- marks and the students scoring
above and below the average marks for a class of N students.
PROBLEM STATEMENT
Write a C program to maintain a record of n student details using an array of structures with fields
to store student id, name, marks of subjects and the average. Assume appropriate data type for
each field. Print the marks of the student scoring above and below average.
ALGORITHM
Input: Student details such as student id, name and marks
Output: To print the details of those students scoring above and below average
1. START
2. Read the number of students
3. For each student, read the student id, name and marks for all subjects.
4. Calculate the average of the marks and store in the avg field.
5. Print results.
6. Initialise loop
7. Read the average of each student
8. Check if avg>35.00
9. If yes print result else go to next iteration
10. Initialise loop
11. Read average of each student
12. Check if avg<35.00
13. If yes print result else go to next iteration
14. STOP
#include<stdio.h>
int main()
{
struct student
{
int stu_id;
char name[20];
float lang1_marks;
float lang2_marks;
float sc_marks;
float mat_marks;
float sst_marks;
float comp_marks;
float avg;
};
for(i=0;i<n;i++)
{
printf("\n\nEnter student ID :"); // Student ID
scanf("%d",&s[i].stu_id);
for(i=0;i<n;i++)
{
if(s[i].avg>=35.0)
printf("%d\t%s\t%f\n",s[i].stu_id,s[i].name,s[i].avg);
}
for(i=0;i<n;i++)
{
if(s[i].avg<35.0)
printf("%d\t%s\t%f\n",s[i].stu_id,s[i].name,s[i].avg);
}
return 0;
}
SAMPLE OUTPUT
./a.out
ID Name Average
AIM
Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
PROBLEM STATEMENT
Write a C program using pointers to compute the sum, mean and standard deviation of all elements
stored in an array of n real numbers.
ALGORITHM
Input: An array of numbers
Output: To compute sum, mean and standard deviation
1. START
2. Read the value of n
3. Read the numbers into the array ‘a’ using the pointer ‘p’.
4. Initialise: i=0 and sum=0
5. Iterate the loop and perform the following
a. Sum = sum + *p
b. Increment i
6. Display the sum
7. Calculate the mean using the formula mean = sum/n
8. Display mean
9. Initialise i=0 and var=0
10. Iterate the loop and perform the following
a. var=var+pow((*p-mean),2);
b. Increment i
11. Display the variance
12. Compute
a. var = var/n
b. Sd = sqrt(var)
13. Display sd
14. STOP
#include<stdio.h>
#include<math.h>
int main()
{
int i,n;
float a[10],mean,sd,sum,var;
float *p; // p is a pointer to float value
sum=sd=mean=var=0;
// Find variance
p=a;
for(i=0;i<n;i++)
{
var=var+pow((*p-mean),2);
p++;
}
var=var/n;
SAMPLE OUTPUT
1.
$ cc prog10.c -lm
$ ./a.out
The Sum=15.000000
The mean=3.000000
2.
$ ./a.out
The Sum=165.000000
The mean=55.000000
PROGRAM CODE:
#include<stdio.h>
int main()
{
int binary,decimal;
return 0;
}
SAMPLE OUTPUT:
$ cc prog11.c
$ ./a.out
Enter binary input :01
Decimal equivalent = 1
$ ./a.out
Enter binary input :100
Decimal equivalent = 4
$ ./a.out
Enter binary input :1001
Decimal equivalent = 9
$ ./a.out
Enter binary input :1101
Decimal equivalent = 13
$ ./a.out
Enter binary input :110011001
Decimal equivalent = 409
*/
Aim:
To develop a C Program to display the pattern by reading the number of rows as
input,
1
12 1
12321
1234321
nth row
Algorithm:
Step 1: Start
● Begin the execution of the program.
Step 2: Declare Variables
● Declare three integer variables i, j, and n:
○ i will be used as the row counter.
○ j will be used as the column counter.
○ n will store the number of rows entered by the user.
Step 3: Input the Number of Rows
● Print the message: "Input number of rows : ".
● Read the user's input for n using scanf("%d", &n);.
Step 4: Outer Loop (for rows)
● Create a for loop for i starting from 0 up to n. This loop will iterate n+1 times,
where i represents the current row.
Step 5: Inner Loop 1 (Leading Spaces)
Department of Computer Science & Engineering, CMRIT, Bangalore
● Inside the outer loop, create another loop to print spaces. The loop runs from j = 1
to n - i. This loop prints spaces in decreasing order as the row number increases.
Step 6: Inner Loop 2 (Ascending Numbers)
● After printing the leading spaces, create another loop to print ascending numbers.
The loop runs from j = 1 to i, printing numbers from 1 up to i.
Step 7: Inner Loop 3 (Descending Numbers)
● After the ascending numbers, create a loop that prints descending numbers. The
loop runs from j = i - 1 down to 1, printing numbers from i-1 to 1.
Step 8: Newline Character
● After printing spaces and numbers for the current row, print a newline character
(\n) to move to the next row.
Step 9: Repeat
● Repeat steps 5 to 8 for the next rows by continuing the loop for i until all rows are
printed.
Step 10: End
● Once the outer loop is complete, the program ends.
#include <stdio.h>
void main()
{ int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0; i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
} for(j=i-1;j>=1;j--)
{
printf("%d",j);
} printf("\n");
}
}
12: Write a C program to copy a text file to another, read both the
input file name and target file name.
AIM:
To develop a c program to copy a text file to another, read both the input filename
and target filename
Algorithm:
Input: Source file name and Target file name
Output: Contents of the source file is copied to the target file
Step 1: Start
Step 2: Read the source file name and target file name
Step 3: Open the source file in read mode
Step 4: if fp1 == NULL go to step 11, else go to step 5
Step 5: Open the target file in write mode
Step 6: if fp2 == NULL go to step 11, else go to step 7
Step 7: ch = fgetc(fp1)
Step 8: while ch != EOF (End of File) go to step 9, else go to step 11
Step 9: write the character ch to target file - fputc(ch, fp2), go to step 7
Step 10: Close the source file and target file
Step 11: Stop
Output
Prerequisite: create two text
files 1.txt Hello world
2.txt----?
Enter the filename to open for
reading 1.txt
Enter the filename to open for
writing 2.txt
File copied successfully
#include <stdio.h>
int checkPrimeNumber(int n);
int main() {
if (flag == 1) {
printf("%d ", i);
}
}
return 0;
}
if (n % j == 0) {
flag = 0;
Sample Output:
Enter the lower value and upper value : 2
10
The Prime numbers from 2 and 10 are:
3, 5, 7
#include <stdio.h>
int gcd(int x, int y); //function prototype
int main()
{
int num1, num2, hcf, lcm;
Sample Output:
Enter two integer values:
10
25
GCD:5
LCM:50
LCM of 24 and 36 = 72
Viva Questions
1. What is an algorithm?
2. What is flowchart?
3. Explain the function of switch statement?
4. What is a data type?
5. What is a variable?
6. Difference between case and default?
7. What are input and output operations?
8. Explain the purpose of header files.
9. What is printf statement?
10. What is scanf statement?
11. Explain the different cases in quadratic equation program.
12. What is clrscr ( )?
13. What is “\n” (slash n)?
14. What is math.h ?
15. What are decision making conditions of C language?
16. Write the syntax of “ if ”statement?
17. Difference between if and switch statements?
18. Write an unconditional control statement in C.
19. Write a flowchart for ‘ if’ conditional construct?
20. Difference between “=” & “= =”.
21. What is a keyword?
22. Explain the syntax of “scanf”.
23. What is modulus (%) operation?
24. Explain the syntax of if-else statement.
25. What are Looping control statements?
26. Explain while loop.
27. What is the difference between while and for loops?
28. What are the Entry controlled and Exit controlled loops in C ?
29. Write the flowchart for ‘while’ loop.
30. Write the flowchart for ‘do while’ loop
**********
Department of Computer Science & Engineering, CMRIT, Bangalore