CP Module
CP Module
Start
Get A&B
Compute a+b,a-
b,a*b,a/b
Display
Results
Stop
1. Implement basic C programs using data types
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
Void main()
int a,b,sum,difference,product;
float quotient;
clrscr();
scanf(“%d%d”,&a,&b);
sum=a+b;
difference=a-b;
product=a*b;
quotient=a/b;
getch();
Result:
Thus, the C program perform basic arithmetic operations is developed and executed
successfully.
Flow Chart:
Start
Get
Radius
Compute
area=Pi*r*r
Display
area
Stop
1-b) Create a C program to calculate the area of a circle (floating-point arithmetic).
Aim:
To develop a C program to find the area of the circle using the formula.
Algorithm:
#include<stdio.h>
#include<conio.h>
define Pi 3.14
Void main()
int r;
float area;
clrscr();
scanf(“%d”,&r);
area=Pi*r*r;
getch();
Result:
Thus, the C program for area of the circle for the given radius is developed and executed
successfully.
Flow Chart:
Start
Get a
character
find is it True
a vowel
False
Display
Vowel
Display
Consonant
Stop
1-c) Implement a C program that takes a character as input and checks whether it is a vowel or a
consonant.
Aim:
Algorithm:
Step4: Use if else to find whether the given character is vowel or consonant.
Enter a Character: i
#include<stdio.h>
#include<conio.h>
void main()
char find[2];
printf(“Enter a Character:”);
scanf(“%c”,&find);
if (find == ‘a’ || find == ‘A’ || find == ‘e’ || find == ‘E’ || find == ‘i’ || find == ‘I’ || find == ‘o’
|| find == ‘O’ || find == ‘u’ || find == ‘U’)
else
getch();
Result:
Thus, the C program to find whether the entered character is vowel or consonant is developed and
executed successfully.
Flow Chart:
Start
Get A&B
Compute c=a%b
Display C
Stop
2. Implement programs using Operators and Expressions
2-a) Create a program that calculates and prints the remainder when dividing two integers.
Aim:
Algorithm:
The Remainder is 1
Program:
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
clrscr();
scanf(“%d%d”,&a,&b);
c=a%b;
getch();
Result:
Start
Get A&B
Compute a++,b--
Display
a,b
Stop
2-b) Write a program that demonstrates the use of increment and decrement operators.
Aim:
Algorithm:
After increment of A is 6
After decrement of B is 5
Program:
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
clrscr();
scanf(“%d%d”,&a,&b);
getch();
Result:
Thus, the C program for increment and decrement operator is developed and executed
successfully.
Flow Chart:
Start
Get
num1,num2
False
If
num1>num2
True
Display Num2 is
greater
Display Num1 is
greater
Stop
2-c) Implement a program to compare two numbers using relational operators (>, <, >=, <=, ==,!=)
Aim:
Algorithm:
Step3: Assign the two numbers with a suitable variable as num1, num2.
Step4: Compute the result using relational operators using if else condition.
1 is less than 2
Program:
#include <stdio.h>
#include<conio.h>
int main()
else
getch();return 0;
Result:
Thus, the C program for using relational operators is developed and executed successfully.
Flow Chart:
3. Develop Programs using Branching statements
3-a) Write a program that takes an integer as input and prints whether it is positive, negative, or
zero using if-else statements.
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
void main()
int a;
clrscr();
scanf(“%d”,&a);
if(a>0)
else if(a==0)
else
getch();
Result: Thus, the C program for finding whether the given integer is positive or negative or zero is
developed and executed successfully.
Flow Chart:
Start
Get a
character
if year True
%4==0
False
Display
Leap Year
Display Not
Leap Year
Stop
3-b) Implement a program that checks whether a given year is a leap year using conditional
statements.
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
void main()
int year;
scanf(“%d”,&year);
if(year%4==0)
else
getch();
Result:
Thus, the C program to find leap year or not is developed and executed successfully.
Flow Chart:
3-c) Implement a calculator program to do simple commercial calculator operations like addition,
subtraction, multiplication and division using a switch case statement.
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
void main()
int a,b,x;
clrscr();
printf(“1.Addition\n2.Subtraction\n3.Multiplication\n4.Division”);
scanf(“%d”,&x);
scanf(“%d%d”,&a,&b);
switch(x)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
default:
printf(“Invalid Choice”);
getch();
Result:
Thus, the C program for Simple Calculator is developed and executed successfully.
Flow Chart:
3-d) Implement a program that uses a ‘for’ loop to calculate the factorial of a given number.
Aim:
To develop a C program to calculate factorial of the given number using for loop.
Algorithm:
Step3: Assign it as n .
Enter a Number: 5
#include<stdio.h>
#include<conio.h>
void main()
int n,i,fact=1;
printf(“Enter a number:”);
scanf(“%d”,&a);
for(i=1;i<=n;i++)
fact=fact*i;
getch();
Result:
Thus, the C program for factorial has developed and eecuted successfully.
4. Implement Programs using Control Structures
4a) Write a program to generate the Fibonacci series up to a given number using a while loop.
Aim:
Algorithm:
Fibonacci Series: 0 1 1 2 3 5
Program:
#include <stdio.h>
#include<conio.h>
void main()
int t1 = 0, t2 = 1, t3 = 0, n;
scanf("%d", &n);
t3 = t1 + t2;
while (t3<= n) {
t1 = t2;
t2 = t3;
t3 = t1 + t2;
getch();
Result:
Thus, the C program for Fibonacci series is developed and executed successfully.
4-b) Write a program that checks whether a given number is an Armstrong number or not using
loops and conditionals.
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void armstrong(int);
void main()
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
Armstrong(n);
void armstrong(int n)
int x,y,r,s=0;
x=n;
y=n;
while(y!=0)
r=y%10;
s=s+(r*r*r);
y=y/10;
}
Output:
else
getch();
Result:
Thus, the C program to find Armstrong number is developed and executed successfully.
5. Develop programs using Arrays
5-a) Create a program that finds the largest and smallest elements in an array of
numbers.
Aim:
To develop a C program to perform operations on one dimensional
and two dimensional array.
Algorithm:
Step 1 : Create two intermediate variables small and large.
Step 2. Initialize the small and large variable with arr[0].
Step 3. Now traverse the array iteratively and keep track of the smallest
and largest element until the end of the array.
Step 4. In the last, will get the smallest and largest number in the variable
small and large respectively.
Step 5. Print both the variables.
Output:
Largest element is :150
Smallest element is : 2
Program:
#include <stdio.h>
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
int main()
{
int arr[] = {3, 18, 10, 4, 2, 22, 150};
int i, small, large;
const int N = ARRAY_SIZE(arr);
small = arr[0];
large = arr[0];
for (i = 1; i < N; i++)
{
if (arr[i] < small)
{
small = arr[i];
}
if (arr[i] > large)
{
large = arr[i];
}
}
printf("Largest element is : %d\n", large);
printf("Smallest element is : %d\n", small);
return 0;
}
Result:
Aim:
Algorithm:
#include<stdio.h>
#include<conio.h>
void man()
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("Matrix Addition\n");
Output:
Matrix Addition
2 2
2 2
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i[j]);
printf("\n");
getch();
Result: