CP Lab Programs With Input and Output
CP Lab Programs With Input and Output
1a) Installation and working Tool to draw Flowcharts (Like Flowgorithm, Raptor etc.)
Overview
Flowgorithm is a popular flowchart tool. In this tutorial, we will download and install the Flowgorithm
flowchart tool on Windows operating system. This is a step-by-step guide to downloading and installing
the tool on a Windows machine.
Download Flowgorithm
Open a web browser.
Navigate to the Flowgorithm official website.
http://www.flowgorithm.org/
Check the architecture type of the machine to download 32-bit or 64-bit installers. To download the
installer for 64-bit architecture, choose the 64-bit installer and download the file.
Install Flowgorithm
The downloaded extracted folder contains the setup files. one is the .msi installer and the other one is
the setup.exe
To install the tool, double-click on the setup.exe file to install the tool. This will launch the install
wizard screen.
The Microsoft Defender might prevent you from running the setup. This is due to the fact that the intaller
is not signed.
The alternative is to click on the More info link. To proceed with the setup click on the Run
anyway button.
Norton File Insight
We have checked the Norton Rating of the Flowgorithm Installer. The rating is Good and
recommended for users by the Norton community.
Accept the default location and click on the Next button to install the tool.
*Optional*
To customize the install location of the tool, click on the Browse… button. Choose the new location for
the tool.
1b) Draw a flowchart to perform arithmetic operations like – sum, average, product, difference,
quotient and remainder of given numbers.
START
COMPUTE SUM=A+B
COMPUTE AVG=SUM/2
COMPUTE DIFFERENCE=A-B
COMPUTE QUOTIENT=A/B
COMPUTE REMAINDER=A%B
START
INPUT DAYS
STOP
WEEK 2
2) Sample Programs on Flowchart design
2a) Draw a flowchart to read input name, marks of 5 subjects of a student and display the name of
the student, the total marks scored, percentage scored.
START
INPUT NAME
INPUT 5 MARKS
STOP
2b) Draw a flowchart to find the largest and smallest among three entered numbers and also display
whether the identified largest/smallest number is even or odd.
START
READ A, B, C
No If A > B Yes
IF B > C IF A > C
PRINT LARGE
PRINT ODD PRINT EVEN
2c) Draw a flow chart to check whether the given number is palindrome or not.
START
INPUT A NUMBER
READ NUMBER
REVERSE = 0
TEMPNUM = NUMBER
NO
NUMBER != 0
YES
REM = NUMBER % 10
REVERSE * = 10 + REM
NUMBER = NUMBER / 10
REVERSE =
YES
TEMPNUM
NO
WEEK 3
3) Sample Programs on C Tokens
3a) Write a C program to read the input(N) as floating-point number from the user and display the
number in one-, two- and three-digit floating point numbers. Example: If N = 12.345678, then the
output is 12.3, 12.34, 12.345.
# include <stdio.h>
void main ()
float num;
scanf(“%f”,&num);
printf(“%f\n”,num);
printf("%0.1f\n", num);
printf("%0.2f\n", num);
printf("%0.3f\n", num);
OUTPUT:
12.3
12.32
12.321
32.4
32.46
32.459
3b) Write a C program to swap values of two variables with and without using third variable.
#include<stdio.h>
void main()
int num1,num2,temp;
printf("Enter number1:");
scanf("%d",&num1);
printf("Enter number2:");
scanf("%d",&num2);
temp=num1;
num1=num2;
num2=temp;
OUTPUT:
1) Enter number1: 12
Enter number2: 15
Enter number2: 15
#include<stdio.h>
void main()
int num1,num2;
printf("Enter number1:");
scanf("%d",&num1);
printf("Enter number2:");
scanf("%d",&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
1) Enter number1: 12
Enter OUTPUT:
number2: 15
Enter number2: 15
#include<stdio.h>
#include<conio.h>
int main()
{
float p, t, r, si, ci;
clrscr();
printf("Enter principal amount (p): ");
scanf("%f", &p);
printf("Enter time in year (t): ");
scanf("%f", &t);
printf("Enter rate in percent (r): ");
scanf("%f", &r);
/* Calculating simple interest */
si = (p * t * r)/100.0;
WEEK 4
4) Sample Programs on Decision statements
4a) Formulate a C program to find whether a given year is a leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
int year;
scanf("%d", &year);
if((year%4==0&&year%100!=0)||year%400==0)
}
else
getch();
OUTPUT:
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>
void main()
scanf("%u",&marks);
if(marks>=90)
printf("Grade A+");
else if(marks>=80)
printf("Grade A");
else if(marks>=70)
printf("Grade B+");
else if(marks>=60)
printf("Grade B");
else if(marks>=50)
printf("Grade P");
else
printf("Fail");
Else
printf("Invalid marks");
OUTPUT:
Fail
4c) Write a program that presents the user a choice of 5 favorite drinks: (Coke, Water, Sprite, Juice,
and Milk). Then allow the user to choose a drink by entering a number 1-5. Output which drink they
chose. If the user enters a choice other than 1-5 then it will output "Error. Choice was not valid."
#include<stdio.h>
#include<conio.h>
void main()
int choice;
printf("Menu:\n");
printf("1.coke\n");
printf("2.water\n");
printf("3.sprite\n");
printf("4.juice\n");
printf("5.milk\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("coke");
break;
case 2:
printf("water");
break;
case 3:
printf("sprite");
break;
case 4:
printf("juice");
break;
case 5:
printf("milk");
break;
default:
break;
OUTPUT:
Test 1:
Menu:
1. Coke
2. Water
3. Juice
4. Sprite
5. Milk
Coke
WEEK 5
5) Sample Programs on Loop statements
5a) Ramya is learning about units and tens digit in school. Her teacher gives a task to find the sum of
the ones and tens digit of any given number.
Example: Given number N = 2456, number 6 is in one’s position and the number 5 is at tens position.
Sum of the digits at one’s position and tens position in the given number is: 6 + 5 = 11.
Write a C program that finds the sum of the digits at ones and tens position in the given number.
#include<stdio.h>
void main()
int num,temp,ones_digit,tens_digit,sum=0;
scanf("%d",&num);
temp=num;
ones_digit=temp%10;
temp=temp/10;
tens_digit=temp%10;
temp=temp/10;
sum=ones_digit+tens_digit;
}
OUTPUT:
#include<stdio.h>
void main()
int f1=0,f2=1,f,num,i;
scanf("%d",&num);
printf("%d\t%d\t",f1,f2);
for(i=2;i<num;i++)
f=f1+f2;
printf("%d\t",f);
f1=f2;
f2=f;
}
OUTPUT:
Enter n Value: 5
01123
#include <stdio.h>
#include <math.h>
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return term;
int main() {
int i,terms;
double x, sum = 1.0; // Start with the first term of the series
// Input values
printf("Enter the value of x: ");
scanf("%lf", &x);
scanf("%d", &terms);
}
OUTPUT:
WEEK 6
6. Sample Programs on Nested Loop statements
6a) Design a C Program that prints the Floyd triangle in ‘n’ rows.
Example: If n = 4
23
456
7 8 9 10
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k=1;
clrscr();
for(i=1;i<=4;i++)
for(j=1;j<=4-i;j++)
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",k++);
printf("\n");
}
}
OUTPUT:
1
23
456
7 8 9 10
131
13531
#include<stdio.h>
#include<conio.h>
void main()
{
int rows,i,space,j,num;
//scanf("%d", &rows);
clrscr();
num = 1;
// Leading spaces
printf(" ");
}
// Left side of the pyramid
printf("%3d", num);
num += 2;
}
// Right side of the pyramid
num -= 4;
printf("%3d", num);
num -= 2;
printf("\n");
}
OUTPUT:
131
13531
*
***
*****
*******
*********
*******
*****
***
*
#include<stdio.h>
#include<conio.h>
void main()
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=5-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
for(i=4;i>=1;i--)
{
for(j=1;j<=5-i;j++)
printf(" ");
}
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
OUTPUT:
***
*****
*******
*********
*******
*****
***
*
WEEK 7
7. Sample Programs on arrays
7a. Construct a program which finds the kth smallest element among the given set of elements.
#include<stdio.h>
#define MAX 100
void main()
{
int a[MAX],i,n,j,kth,temp,pos;
printf("Enter how many values you want to read : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
{
pos=i;
for(j=i+1; j<n; j++) if(a[j]<a[pos])
{
pos=j;
}
temp=a[i]; a[i]=a[pos]; a[pos]=temp;
}
printf("%d is the %dth smallest element",a[kth],kth);
}
OUTPUT:
7b. Several people are standing in a row and need to be divided into two teams. The first person goes
into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and
so on. You are given an array of positive integers (the weights of the people). Return an array of two
integers, where the first element is the total weight of team 1, and the second element is the total
weight of team 2 after the division is complete.
Write a C program that finds the weight of team 1 and team 2 after the division for the above
scenario.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,wt1,wt2;
clrscr();
printf("how many number of elements u want\n");
scanf("%d",&n);
printf("enter elements into the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]); wt1=wt2=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
wt1+=a[i];
else
wt2+=a[i];
}
printf("sum of team1 = %d and team 2 = %d\n",wt1,wt2);
}
OUTPUT:
12345
int A[10][10],B[10][10],C[10][10],r1,c1,r2,c2,i,j,k;
system("cls");
scanf("%d",&B[i][j]);
}
}
printf("multiply of the matrix=\n"); for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{ C[i][j]=0;
for(k=0;k<c1;k++)
{ C[i][j]+=A[i][k]*B[k][j];
}
}
}
//for printing result for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",C[i][j]);
}
printf("\n");
}
}
else
{
}
OUTPUT:
enter the number of rows and cols of matrix A=2 3
123
456
78
9 10
11 12
58 4
139 4
8a) Write a C program to Insert a sub string into a given main string from a given position.
#include <stdio.h>
#include <string.h>
int main()
puts("Enter a string");
gets(a);
gets(b);
scanf("%d", &pos);
la = strlen(a);
lb = strlen(b);
l = pos + lb;
lc = la + lb;
for (i = 0; i < pos; i++)
c[i] = a[i];
j = 0;
c[i] = b[j];
j++;
j = pos;
c[i] = a[j];
j++;
c[i] = '\0';
printf("%s", c);
return 0;
OUTPUT:
Enter a string
Hello World
, beautiful
#include <string.h>
int main()
gets(str);
len = strlen(str);
flag = 1;
break;
if(flag == 0)
else
return 0;
OUTPUT:
level
8C) Given a string, return the character that appears the minimum number of times in the string. The
string will contain only ascii characters, from the ranges (“a”-”z”,”A”-”Z”,0-9), and case matters. If there
is a tie in the minimum number of times a character appears in the string return the character that
appears first in the string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
int i,j;
clrscr();
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= '0' && str[i] <= '9'))
count[(int)str[i]]++;
minChar = (char)j;
minFreq = count[j];
return minChar;
int main() {
if (input == NULL) {
return 1;
}
scanf("%s", input);
printf("%c\n", findMinFrequencyChar(input));
free(input);
getch();
return 0;
OUTPUT:
hello world
WEEK 9
9a) Create a calculator that takes a number, a basic math operator (+, -, *, /, ^), and a second number all from user
input, and have it print the result of the mathematical operation. The mathematical operations should be
wrapped inside of functions.
#include <stdio.h>
#include <math.h>
}
// Function to subtract second number from the first double
subtract(double num1, double num2)
if (num2 != 0)
else
int main() {
scanf("%lf", &num1);
scanf("%lf", &num2);
switch (operator)
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
case '^':
break;
default:
OUTPUT:
Result: 15.00
9b) Write a C program that accepts a decimal number and display its binary representation using user defined
functions.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10],n,i;
system ("cls");
scanf("%d",&n);
for(i=0;n>0;i++)
a[i]=n%2;
n=n/2;
for(i=i-1;i>=0;i--)
printf("%d",a[i]);
return 0;
OUTPUT:
9c) Write a c Program to find the sum of a five-digit number using command line arguments.
#include <stdio.h>
#include <stdlib.h>
if (argc != 2) {
int sum = 0;
while (num != 0) {
num /= 10;
OUTPUT:
./program_name 12345
WEEK 10
#include<stdio.h>
int main()
int no1,no2;
int *ptr1,*ptr2;
int sum,sub,mult;
float div;
printf("Enter number1:\n");
scanf("%d",&no1);
printf("Enter number2:\n");
scanf("%d",&no2);
sub=(*ptr1) - (*ptr2);
mult=(*ptr1) * (*ptr2);
div=(*ptr1) / (*ptr2);
printf("sum= %d\n",sum);
printf("subtraction= %d\n",sub);
printf("Multiplication= %d\n",mult);
printf("Division= %f\n",div);
return 0;
OUTPUT:
Enter number1:
10
Enter number2:
sum= 15
subtraction= 5
Multiplication= 50
Division= 2.000000
10 b) Define a structure called cricket that describes the following information: Player name, team
name and batting average Using the structure Cricket, declare an array player and write a C program
that reads information about all the players and print team-wise list containing names of players
with their batting average?
#include <stdio.h>
#define N 100
// STRUCTURE DECLARATION
typedef struct
char playerName[20];
char teamName[20];
int battingAvg;
} cricket;
int main()
{
int n, i;
scanf("%d", &n);
scanf("%s", player[i].playerName);
scanf("%s", player[i].teamName);
return 0;
OUTPUT:
Player 1
TeamA
45
Player 2
Alice
TeamB
32
Player 3
Bob
TeamA
38
John TeamA 45
Alice TeamB 32
Bob TeamA 38
WEEK 11
11 a) Write a C program to print the bill details of ‘N’ number of customers with the following data:
meter number, customer name, no of units consumed, bill date, last date to deposit and city. The
bill is to be calculated according to the following conditions: No. of units Charges For first 100 units
Rs.0.75 per unit for the next 200 units Rs.1.80 per unit for the next 200 units Rs. 2.75 per unit.
#include <stdio.h>
#define N 100
typedef struct {
int meterNumber;
char customerName[50];
int unitsConsumed;
char billDate[15];
char lastDate[15];
char city[20];
} Customer;
Customer customers[N];
} else {
return bill;
int main() {
int n,i;
scanf("%d", &n);
scanf("%d", &customers[i].meterNumber);
scanf("%s", customers[i].customerName);
scanf("%d", &customers[i].unitsConsumed);
scanf("%s", customers[i].billDate);
scanf("%s", customers[i].lastDate);
printf("City: ");
scanf("%s", customers[i].city);
printf("\n\nBill Details:\n");
printf("%-15s %-20s %-20s %-15s %-15s %-10s %-10s\n", "Meter Number", "Customer Name",
"Units Consumed", "Bill Date", "Last Date", "City", "Bill Amount");
}
return 0;
OUTPUT:
Customer 1 details:
City: Anantapur
Customer 2 details:
City: Hindupur
Bill Details:
Meter Number Customer Name Units Consumed Bill Date Last Date City Bill Amount
11 b) Write a C program that takes input for a student’s credits and marks to determine whether
the student is eligible for promotion to the nest year and calculate their CGPA.
#include <stdio.h>
// Function to determine eligibility and calculate CGPA
{
float cgpa;
Else
printf("Sorry, you are not eligible for promotion to the next year.\n");
int main() {
int credits;
float marks;
scanf("%d", &credits);
scanf("%f", &marks);
calculatePromotionAndCGPA(credits, marks);
return 0;
OUTPUT:
int main()
char fName[20];
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
putc('A',fp);
putc('B',fp);
putc('C',fp);
printf("\nData written successfully.");
fclose(fp);
fp=fopen(fName,"r");
if(fp==NULL)
exit(0);
printf("%c",getc(fp));
printf("%c",getc(fp));
printf("%c",getc(fp));
fclose(fp);
return 0;
OUTPUT:
Contents of file is :
ABC
ABC
#include <stdio.h>
int main()
scanf("%s", filename);
if (fptr1 == NULL)
exit(0);
scanf("%s", filename);
if (fptr2 == NULL)
exit(0);
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
OUTPUT:
Enter the filename to open for reading:
input.txt
Enter the filename to open for writing:
output.txt
Contents copied to output.txt
12 c) Write a C program to merge two files into the third file using command-line arguments.
#include <stdio.h>
#include <stdlib.h>
int main()
char c;
exit(0);
fputc(c, fp3);
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp
1);
fclose(fp
2);
fclose(fp
3);
return 0;
}
OUTPUT:
Hello,World!