0% found this document useful (0 votes)
17 views

CP Lab

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CP Lab

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

BASICS:

THEORY: A basic structure of a C program typically includes:

1.Preprocessor Directives: These include any necessary header files, such as


#include <stdio.h> for input/output functions.

2.Main Function: Every C program must have a main() function, where the
program execution begins.

3.Declaration Section: This section includes variable declarations and function


prototypes, if any.

4.Executable Statements: This is where the actual logic of the program resides. It
consists of statements that perform tasks like calculations, input/output
operations, and control flow.

5.Return Statement: The return statement at the end of the main() function
indicates the successful termination of the program. Typically, return 0; is used to
indicate successful execution.
a) AIM: Write a program to find the area of the rectangle, Circle and
Surface area of a Cylinder.

ALGORITHM:

INPUT: Length and breadth of rectangle,radius of circle,radius and height of


cylinder.
OUTPUT: Area of rectangle,circle and surface area of cylinder
STEP-1: Start
STEP-2: Read length and breadth of rectangle
STEP-3: area_rectangle=length*breadth
STEP-4: Display area of rectangle as area_rectangle
STEP-5: Read radius of circle
STEP-6: area_circle=3.14*radius*radius
STEP-7: Display area of circle as area_circle
STEP-8: Read radius and height of cylinder as cylinder_radius and height
respectively
STEP-9: area_cylinder=(2*3.14* cylinder_radius*height)
+(3.14*cylinder_radius*cylinder_radius)
STEP-10: Display surface area of cylinder as area_cylinder
STEP-11: Stop

PROGRAM:
#include<stdio.h>
int main()
{
float length,breadth,radius,cylinder_radius,height,area_rectangle,area_circle,
area_cylinder;
printf("Enter length and breadth of rectangle:");
scanf("%f %f",&length,&breadth);
area_rectangle=length*breadth;
printf("\nArea of rectangle is:%.2f",area_rectangle);
printf("\nEnter radius of circle:");
scanf("%f",&radius);
area_circle=3.14*radius*radius;
printf("\nArea of circle is:%.2f",area_circle);
printf("\nEnter radius and height of cylinder:");
scanf("%f %f",&cylinder_radius,&height);

area_cylinder=(2*3.14*cylinder_radius*height)
+(2*3.13*cylinder_radius*cylinder_radius);
printf("\nSurface area of cylinder is:%.2f",cylinder);
return 0;
}
OUTPUT:

Enter length and breadth of rectangle:15 20


Area of rectangle is:300.00
Enter radius of circle:5
Area of circle is:78.50
Enter radius and height of cylinder:4 8
Surface area of cylinder is:301.12

b) AIM: WAP to check whether the number is odd and even.

ALGORITHM:

INPUT: Any integer


OUTPUT: Display whether the number entered is odd or even
STEP-1: Start
STEP-2: Read a number as num
STEP-3: if(num%2==0) then print The number is even
STEP-4: else print The number is odd
STEP-5: Stop

PROGRAM:

#include<stdio.h>

int main()

int num;

printf("Enter a number:\n");

scanf("%i",&num);

if(num%2==0)

printf("The number is even");

else
printf("The number is odd");

return 0;
}

OUTPUT:

Enter a number:

19
The number is odd

c) AIM: Swap 2 nos. (i)with temp variable.(ii) without temp variable.

(i)with temp variable:

ALGORITHM:
INPUT: Two numbers
OUTPUT: Display the two numbers after swapping
STEP-1: Start
STEP-2: Read 2 numbers as a and b
STEP-3: Print a and b before swapping
STEP-4: temp=a
STEP-5: a=b
STEP-6: b=temp
STEP-7: Print a and b after swapping
STEP-8: Stop

PROGRAM:

#include<stdio.h>

int main()

int a,b,temp;

printf("Enter two numbers:\n");

scanf("%i",&a);

scanf("%i",&b);
printf("The two numbers before swapping are:");

printf("%i",a);

printf(",");

printf("%i",b);

temp=a;

a=b;

b=temp;

printf("\nThe two numbers after swapping are:");

printf("%i",a);

printf(",");

printf("%i",b);
}

OUTPUT:

Enter two numbers:

7 16

The two numbers before swapping are:7,16


The two numbers after swapping are:16,7

(ii) without temp variable

ALGORITHM:
INPUT: Two numbers
OUTPUT: Display the two numbers after swapping
STEP-1:Start
STEP-2: Read two numbers as a and b
STEP-3: Display a and b before swapping
STEP-4: a=a+b
STEP-5: b=a-b
STEP-6: a=a-b
STEP-7: Display a and b after swapping
STEP-8: Stop
PROGRAM:

#include<stdio.h>

int main()

int a,b;

printf("Enter two numbers:\n");

scanf("%i",&a);

scanf("%i",&b);

printf("The two numbers before swapping are:");

printf("%i",a);

printf(",");

printf("%i",b);

a=a+b;

b=a-b;

a=a-b;

printf("\nThe two numbers after swapping are:");

printf("%i",a);

printf(",");

printf("%i",b);
}

OUTPUT:

Enter two numbers:

48
The two numbers before swapping are:4,8
The two numbers after swapping are:8,4

2. SELECTION:

THEORY:

Selection statements in C are fundamental constructs that allow you to control the
flow of your program based on certain conditions. There are three main types of
selection statements in C: if, else if, and else.
1.if statement:

The if statement evaluates a condition and executes a block of code if the condition
is true.

If the condition is true, the code inside the curly braces {} is executed. If the
condition is false, the code block is skipped, and program execution continues after
the if statement.

2.else if statement:

The else if statement allows you to check additional conditions if the preceding if
condition is false. You can have multiple else if blocks to check different conditions.

If condition1 is true, the corresponding block of code is executed. If condition1 is


false, condition2 is evaluated. If condition2 is true, its corresponding block of code is
executed, and so on.

3.else statement:

The else statement is optional and is used to execute a block of code if none of the
preceding conditions are true. It must be used after an if or else if statement.

If none of the preceding conditions are true, the code inside the else block is
executed.
These selection statements allow you to make decisions and perform different
actions based on different situations in your program, making your code more
dynamic and responsive.

a) AIM: WAP to accept three numbers and display the greatest number.

ALGORITHM:

INPUT: Three numbers


OUTPUT: Display greatest among three numbers
STEP-1: Start
STEP-2: Read three numbers as a,b,c
STEP-3: if(a>b&&a>c) then print a is greatest
STEP-4: else if(b>c&&b>a) then print b is greatest
STEP-5: else print c is greatest
STEP-6: Stop

PROGRAM:

#include<stdio.h>

int main()

int a,b,c;

printf("Enter three numbers:\n");

scanf("%i",&a);

scanf("%i",&b);

scanf("%i",&c);

if(a>b && a>c)

printf("\n %d is greatest",a);

else if(b>a && b>c)

printf("\n %d is greatest",b);

else

printf("\n %d is greatest",c);

return 0;
}

OUTPUT:

Enter three numbers:


20 84 56

84 is greatest

b)AIM: Write a Program to Test whether a given Number is Prime or Not


a Prime.

ALGORITHM:
INPUT: A number
OUTPUT: Display whether the number is prime or not
STEP-1: Start
STEP-2: Read a number as n
STEP-3: if (n==0||n==1) then flag=1
STEP-4: for i=2 to n/2 , if(n%i==0) then flag=1
STEP-5: if(flag==0) print “n is a prime number”
STEP-6: else print “n is not a prime number”
STEP-7: Stop

PROGRAM:

#include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;

for (i = 2; i <= n / 2; ++i)

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number


if (n % i == 0)

flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

c)AIM: Write a Program to find the Factorial of a Number

ALGORITHM:

INPUT: Any integer

OUTPUT: Factorial of the number

STEP-1: Start

STEP-2: Read any integer as n

STEP-3: fact=1

STEP-4: for i=1 to n fact=fact*i

STEP-5: Display factorial of given number as fact

STEP-6: Stop
PROGRAM:

#include<stdio.h>

int main()

int fact=1,n,i;

printf("Enter a number:");

scanf("%d",&n);

for(i=1;i<=n;i++)

fact=fact*i;

printf("Factorial of given number is:%d ",fact);

OUTPUT:

Enter a number:7

Factorial of given number is:5040

3. Conditional Operator:

THEORY:

The conditional operator, also known as the ternary operator, is a type of operator
used in many programming languages to make decisions based on conditions. It
provides a compact way to express conditional statements.The general syntax is as
follows:

condition ? expression1 : expression2


Condition: This is the expression that evaluates to either true or false. If the
condition is true, the value of the entire expression is the result of expression1;
otherwise, it's the result of expression2.

?: This is the question mark symbol, which serves as the conditional operator itself. It
separates the condition from the expressions to be evaluated.

Expression1: This is the value to be returned if the condition is true. It can be any
valid expression.

“:”: This is the colon symbol, which separates expression1 from expression2.

Expression2: This is the value to be returned if the condition is false. Like


expression1, it can be any valid expression.

The conditional operator is typically used as a shorthand for simple if-else


statements

Some key points about the conditional operator:

It is an expression, not a statement, meaning it can be used inline within larger


expressions or statements.

It provides a concise way to express simple conditional logic, especially when


assigning values based on conditions.

Overuse of the conditional operator can make code less readable, so it's best used
for simple conditions. For more complex logic, traditional if-else statements may be
clearer.

a) AIM: Whether the character entered through the keyboard is a lower


case alphabet or not

ALGORITHM:

INPUT: A character
OUTPUT: Display whether the character entered is a lowercase alphabet or not
STEP-1: Start
STEP-2: Read a character as ch
STEP-3: if(ch>=’a’&&ch<=’z’) print “ch is a lowercase alphabet”
STEP-4: else print “ch is not a lowercase alphabet”
STEP-5: Stop
PROGRAM:

#include<stdio.h>

int main()

char ch;

printf("Enter a character:");

scanf("%c",&ch);

if(ch>='a'&& ch<='z')

printf("%c is a lowercase alphabet",ch);

else

printf("%c is not a lowercase alphabet",ch);

return 0;
}

OUTPUT:

Enter a character:s
s is a lowercase alphabet

b) AIM: Whether the entered year is a leap year or not.

ALGORITHM:

INPUT: An year
OUTPUT: Display whether the entered year is a leap year or not
STEP-1: Start
STEP-2: Read an year as year
STEP-3: if(year%400==0) print “It is a leap year”
STEP-4: else if(year%100==0) print “It is not a leap year”
STEP-5: else if(year%4==0) print “It is a leap year”
STEP-6: else print “It is not a leap year”
STEP-7: Stop

PROGRAM:

#include <stdio.h>

int main()

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0)

printf("%d is a leap year.", year);

else if (year % 100 == 0)

printf("%d is not a leap year.", year);

else if (year % 4 == 0)

printf("%d is a leap year.", year);

else
{

printf("%d is not a leap year.", year);

return 0;
}

OUTPUT:

Enter a year: 2024


2024 is a leap year.

Switch Case:

THEORY:
The switch statement in C is another selection statement that provides a way to
execute different code blocks based on the value of a variable or expression. It's
particularly useful when you have multiple possible conditions to check against a
single variable.
Here's the general syntax of a switch statement:
switch (expression)
{
case constant1:
// code to execute if expression equals constant1
break;
case constant2:
// code to execute if expression equals constant2
break;
// more case statements as needed
default:
// code to execute if expression doesn't match any case
}

Explanation of each part:

1. switch (expression): This is the expression or variable whose value will be


evaluated against the various case constants.

2. case constant: Each case statement represents a possible value of the expression.
If the value of the expression matches the constant, the corresponding code block
will be executed. You can have multiple case statements.

3. // code to execute: This is the block of code that will be executed if the value of
the expression matches the corresponding case constant.

4. break; : The break statement is used to terminate the switch block.

5. default: The default case is optional and is used to execute code when none of the
case constants match the value of the expression.

AIM: WAP to design an interactive simple calculator.

ALGORITHM:

INPUT: An operator and two numbers


Output: Result of the desired operation
STEP-1: Start
STEP-2: Read the operator and two numbers as op,num1 and num2 respectively
STEP-3: switch(op)
STEP-4: case ‘+’: print num1+num2 and break;
STEP-5: case ‘-’: print num1-num2 and break;
STEP-6: case ‘*’: print num1*num2 and break;
STEP-7: case ‘/’: print num1/num2 and break;
STEP-8: default: print “Invalid input”
STEP-9: Stop
PROGRAM:

#include <stdio.h>

int main()

char op;

double num1,num2;

printf("Enter an operator(+, -, *, /): ");

scanf("%c",&op);

printf("Enter two operands: ");

scanf("%lf %lf",&num1,&num2);

switch (op)

case '+':

printf("%.2lf + %.2lf = %.2lf", num1,num2, num1+num2);

break;

case '-':

printf("%.2lf - %.2lf = %.2lf", num1,num2, num1-num2);

break;

case '*':

printf("%.2lf * %.2lf = %.2lf", num1,num2, num1*num2);

break;

case '/':

printf("%.2lf / %.2lf = %.2lf", num1,num2, num1/num2);


break;

default:

printf("Invalid input!");

return 0;
}

OUTPUT:

Enter an operator(+, -, *, /): *

Enter two operands: 9.8 4.6


9.80 * 4.60 = 45.08

4.ITERATION:

THEORY:

Iteration in C refers to the process of repeating a block of code multiple times. There
are several ways to implement iteration in C, including for, while, and do-while
loops.
1. for loop:
The for loop is used when the number of iterations is known before the loop
starts. Its syntax consists of three parts: initialization, condition, and increment
(or decrement) statement.
for (initialization; condition; increment/decrement)
{
// code to be executed repeatedly
}
The initialization step is executed only once at the beginning of the loop. The
condition is evaluated before each iteration, and if it's true, the loop continues;
otherwise, the loop terminates. After each iteration, the increment (or
decrement) statement is executed.
2. while loop:
The while loop is used when the number of iterations is not known beforehand,
and the loop continues until a specified condition becomes false. It evaluates the
condition before each iteration.
while (condition)
{
// code to be executed repeatedly
}
The loop continues as long as the condition evaluates to true. If the condition is
false initially, the code inside the loop will not execute at all.

3. do-while loop:

The do-while loop is similar to the while loop, but it evaluates the condition at
the end of each iteration. This means that the code inside the loop will always
execute at least once.

do{

// code to be executed repeatedly

} while (condition);

The loop first executes the code block, then evaluates the condition. If the condition
is true, the loop continues; otherwise, it terminates.
a) AIM: WAP to print first n prime nos.

ALGORITHM:
INPUT: Range
OUTPUT: Print all prime numbers within the range
STEP-1: Start
STEP-2: Read the range as n
STEP-3: for num=1 to n count=0
for i=2 to num/2
if(num%i==0) count++
STEP-4: if(count==0&&num!=1) print num
STEP-5: Stop

PROGRAM:

#include<stdio.h>

int main()
{

int i, num, n, count;

printf("Enter the range: ");

scanf("%d", &n);

printf("The prime numbers in between the range 1 to %d:",n);

for(num = 1;num<=n;num++)

count = 0;

for(i=2;i<=num/2;i++)

if(num%i==0)

count++;

break;

if(count==0 && num!= 1)

printf("%d ",num);

return 0;
}

OUTPUT:
Enter the range: 50
The prime numbers in between the range 1 to 50:2 3 5 7 11 13 17 19 23 29 31 37 41
43 47

b) AIM: WAP to print all three digit Armstrong nos

ALGORITHM:
INPUT:None
OUTPUT: Print all three digit Armstrong numbers
STEP-1: Start
STEP-2: count=0
STEP-3: for i=100 to 1000
num=i
while(num!=0)
num/=10
count++
num=i
sum=pow(num%10,count)+pow((num%100- num%10)/10,count)
+pow((num%1000-num%100)/100,count);
if(sum==i) print i
STEP-4: Stop

PROGRAM:

#include <math.h>

#include <stdio.h>

int main()

int i, sum, num, count = 0;

printf("Three digit armstrong numbers are:\n");

for (i = 100; i <1000; i++)

num = i;
while (num != 0)

num /= 10;

count++;

num=i;

sum=pow(num%10,count)+pow((num%100-

num%10)/10,count)+pow((num%1000-num%100)/100,count);

if (sum == i)

printf("%d ", i);

count = 0;

return 0;
}

OUTPUT:

Three digit armstrong numbers are:


153 370 371 407

c) AIM: Print the following pattern

*
* *
* * *
* * * *

ALGORITHM:

INPUT: None
OUTPUT: Print the given pattern
STEP-1: Start
STEP-2: for i=1 to 4
for j=1 to 4-i
Print “ “
for k=1 to i
Print “ * “
Print “\n”
Step-3: Stop

PROGRAM:

#include<stdio.h>

int main()

int i,j,k;

for(i=1;i<=4;i++)

for(j=1;j<=4-i;j++)

printf(" ");

for(k=1;k<=i;k++)

printf(" * ");

printf("\n");

}
}

OUTPUT:
*
* *
* * *
* * * *

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy