0% found this document useful (0 votes)
28 views21 pages

Exp1 2

C programming

Uploaded by

rubasudhap
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)
28 views21 pages

Exp1 2

C programming

Uploaded by

rubasudhap
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/ 21

LAB MANUAL

CS3101 - PROGRAMMING FOR PROBLEM SOLVING USING C LABORATORY


EX.NO: 1A AREA OF THE CIRCLE

AIM: To write a C program to compute find the Area of the circle. ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type. Area->integer
Radius->integer
Step 3: Get the radius.
Step 4: Calculate area=3.14*radius*radius Step 5:
Step 5: Print the area of the circle.
Step 6: Stop the Program.

PROGRAM: /*Area of Circle*/


#include<stdio.h>
#include<conio.h>
void main()
{
float radius,area;
printf("Enter the Radius of the circle:\n");
scanf("%f",&radius);
printf(“\nradius=%f”,radius);
area=3.14*radius*radius;
printf("\nArea=%f",area);
getch();
clrscr();
}

OUTPUT:
Enter the Radius of the circle: 5
Radius=5
Area=78.500000

RESULT:

Thus, the program was compiled and executed


EX.NO: 1B Implement different I/O Functions

AIM: To write a C program to implement different I/O Functions.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
Step 3: Get the variable value with different Input functions.
Step 4: Print the value with different Output functions.
Step 5: Stop the program.

PROGRAM-1:
#include <stdio.h>
void main()
{
char ch;
printf(“\nEnter any character : “);
ch = getchar();
printf(“\nYou have entered : %c”,ch);
}

OUTPUT: Enter any character : A


You have entered : A

PROGRAM-2:
#include<stdio.h>
void main()
{
char ch;
printf("\nEnter any character : ");
ch = getch();
putchar(ch);
}

OUTPUT:
Enter any character : A
You have entered : A

PROGRAM-3:
#include <stdio.h>
void main()
{
char name[30];
printf("\nEnter your website: ");
gets(name);
puts(name);
}

OUTPUT:
Enter your website: www.saec.ac.in
www.saec.ac.in

PROGRAM-4:
#include <stdio.h>
int main()
{
int num;
printf("C Programming"); //displays the content inside
quotation scanf(“%d”,&num);
printf(%d”,num);
return 0;
}

OUTPUT:
C Programming
5
5

RESULT:
Thus, the program was compiled and executed successfully.

EX.NO: 1C USAGE OF SIZEOF ()

AIM: To write a C program to find the size of variables using sizeof function.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
Step 3: Get the variable value with different Input
functions. Step 4: Print the variable size using sizeof
function.
Step 5: Stop the program.
C PROGRAM
#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
char charType;

// sizeof evaluates the size of a variable


printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n",
sizeof(floatType)); printf("Size of double: %zu bytes\
n", sizeof(doubleType)); printf("Size of char: %zu
byte\n", sizeof(charType));
return 0;
}

OUTPUT:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

RESULT:

Thus, the program was compiled and executed successfully.

EX.NO: 1D ASCII VALUE OF CHARACTER


AIM: To write a C program to find ASCII value of a character.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variables of respective data
type. Step 3: Print the variable ASCII value
with variable.
Step 4: Stop the program.

C PROGRAM
//Program to Print ASCII Value
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a
character // %c displays the actual character
printf("ASCII value of %c = %d", c,);
return 0;
}
OUTPUT:
Enter a character: G
ASCII value of G = 71

RESULT: Thus, the program was compiled and executed successfully.

EX.NO: 1E SWAPPING OF 2 NUMBERS WITHOUT THIRD VARIABLE


AIM: To write a C program to swap two numbers without using third variable.
ALGORITHM: -

Step 1: Start the program.


Step 2: Declare the variables x,y and get two values
from user. Step 3: swap two numbers with the
following logic.
x = x + y;
y = x - y;
x = x - y;
Step 4:Print the variables x,y.
Step 5: Stop the program.
C PROGRAM
#include<stdio.h>
int main()
{
int x, y;
printf("Enter the value of x and y?");
scanf("%d %d",&x,&y);
printf("before swapping numbers: %d %d\n",x,y);
/*swapping*/
x = x + y;
y = x - y;
x = x - y;
printf("After swapping: %d %d",x,y);
return 0;
}

OUTPUT: Enter the value of x and y?


13
22
before swapping numbers: 13 22
After swapping: 22 13
RESULT:
Thus, the program was compiled and executed successfully.
EX.NO: 1F ILLUSTRATION OF UNARY OPERATORS
AIM: To write a C program to illustrate the unary operators.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the variable and get a value from the user.
Step 3: implement the unary operator logic with the variable.
Unary -/+: negative number becomes positive and positive number becomes
negative ++/--: increment or decrement value by 1 before assigning the value
Step 4: Print the variable value after operation.
Step 5: Stop the program.

PROGRAM-1- Unary minus


//used to include basice c library files
//unary minus operation performed, negative number becomes positive and positive
number becomes negative
#include <stdio.h>
int main()
{
int a, unaryMinus;
printf("Please enter any number for unary - symbol: \n");
scanf("%d",&a);
unaryMinus=-(a);
printf("Unary minus operation of %d is = %d ",a, unaryMinus);
return 0;
}
OUTPUT:
Please enter any number for unary - symbol:
-56
Unary minus operation of -56 is = 56

PROGRAM-2- Unary increment operator


//used to include basice c library files
#include <stdio.h>
int main()
{
int a, pre_increment,post_increment;
printf("Please enter any number \n");
scanf("%d",&a);
//take temp variable for showing actual number
in output int temp=a;
//increment value by 1 before assigning the value
pre_increment=++a;
//displaying output
printf("Pre increment operation of %d is =%d ",temp,
pre_increment); return 0;
}

OUTPUT:
Please enter any number

10

Pre increment operation of 10 is =11

RESULT:

Thus, the program was compiled and executed successfully.


EX NO:- 2A A PASCAL’S TRIANGLE
AIM:
To write the c Program to generate pascal’s triangle.
ALGORITHM

1. Start the program


2. Declare variables n,row,r,space,ncr.
3. Take input (num) for number of rows
4. Iterate loop1 for 'num' times
5. Iterate loop2 inside loop1 for (num-1) times
6. Iterate loop3 inside loop1 after loop2 for 0-i times
7. Inside loop3 print num if(n==0||r==0)
8. Inside loop3 else than condition 7 calculate coefficients according to this
formula ncr=ncr*(n-r+1)/r;
9. Print numbers after 3 spaces under loop2
10. Print new line under loop1
11. End
PROGRAM:
#include <stdio.h>
int main()
{
int n,row,r,space,ncr;
printf("enter the row value");
scanf("%d",&row);
for(n=0;n<row;n++)
{
for(space=1;space<row-n;space++)
{ printf(" "); }
for(r=0;r<=n;r++) {
if(n==0||r==0)
{ ncr=1;
printf("%d ",ncr);
}

else
{ ncr=ncr*(n-r+1)/r;
printf("%d ",ncr);
}
} printf("\n");
}
return 0;
}

OUTPUT:
enter the row value: 5
1
11
121
1331
14641

RESULT: Thus the c Program to generate Pascal’s triangle is executed successfully.

EXNO: - 2B) ARITHMETIC CALCULATOR


AIM:-
To write a C program to design a calculator to perform the operations
namely, addition, subtraction, multiplication, division of number.

ALGORITHM:-

Step 1: Start the program.

Step 2: Declare the variables of respective data type.

Step 3: Get the Number.

Step 4: Read the choice to perform the operation using switch case.

Step 5: Print the result.

Step 6: Stop.

PROGRAM:-

#include<stdio.h>

#include<conio.h>

void main()

int a,b,choice;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");

printf("Enter the values of a & b: ");


scanf("%d %d",&a,&b);

printf("Enter your Choice : ");

scanf("%d",&choice);

switch(choice)
{

case 1 :

printf("Sum of %d and %d is : %d",a,b,a+b);


break;
case 2 :

printf("Difference of %d and %d is : %d",a,b,a-b);


break;
case 3 :

printf("Multiplication of %d and %d is :
%d",a,b,a*b); break;
case 4 :

printf("Division of Two Numbers is %d : ",a/b);


break;
default :

printf(" Enter Your Correct Choice.");

break;

getch();
clrscr();

OUTPUT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 7
2
Enter your Choice : 1
Sum of 7 and 2 is : 9
RESULT:
Thus a C program to design a calculator to perform the operations
namely, addition, subtraction, multiplication, division of number is executed.

EXNO:- 2C) SUM OF DIGITS

AIM:-
To write a program to find sum of digits

ALGORITHM:-
Step 1: Start the program.
Step 2: Declare the variables of respective data type.
n,r,sum->integer.
Step 3: Set sum=0
Step 4: If n>0 then
Step 5: Calculate r=n%10
Sum=sum+r
n=n/10
Step 6: Print sum
Step 7: Stop

PROGRAM:-

#include<stdio.h>
#include<conio.h>
void main()
{
int number, r, sum=0;
printf("Enter the number:\n");
scanf("%d",&number);
while(number>0)
{
r=number%10;
sum=sum+r;
number=number/10;
}
printf("\nSum of digits =%d",sum);
getch();
clrscr();
}

OUTPUT :-
Enter the number: 92
Sum of digits=11

RESULT:-

Thus the sum of digits using C program has been executed and verified successfully

EX NO:- 2D) FIBONACCI SERIES

AIM:
To write a c program to compute Fibonacci series.

Algorithm:
Step 1: Start
Step 2: Declare variable n, first, second, next, c;
Step 3: Initialize variable first = 0, second = 1
Step 4: Read n from user
Step 5: Print fib series
Step 6: Repeat until c < n : next = first + second; first = second; second = next;
Step 7: Stop

Program:
#include<stdio.h>
main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
Output:
Enter the number of terms
5
First 5 terms of Fibonacci series are :-
01123

Result:
Thus a c program to compute fibonacci series is executed successfully.

EXNO:- 2E) PRIME NUMBERS BETWEEN 1 TO N

Aim:
Write a c program to find Prime Numbers between
1 to N.
Algorithm:
Step 1:START the program
step2:Declare the variablenum,i,count,n;
Step 3perform num%i==0 and track the count.
Step 4:exactly find the factors, if(count==0 &&
num!= Step 5 print num
Step 6:stop

program:
#include<stdio.h>
void main(){
int i, num, n, count;
printf("Enter the range: \n");
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);
}
}

Output:
Enter max range: 30
2 3 5 7 11 13 17 19 23 29

Result:
Thus a C program to find Prime Numbers between 1 to N is executed Successfully.

You might also like