0% found this document useful (0 votes)
21 views75 pages

C Lab

The document contains multiple C programs demonstrating various programming concepts including I/O statements, operators, decision-making constructs (if-else, switch-case), looping statements (for, while, do-while), and one-dimensional arrays. Each section outlines the aim, algorithm, program code, output, and result of the execution. The programs cover tasks such as calculating sums of odd and even numbers, evaluating expressions, checking student grades, and finding factorials and Fibonacci series.

Uploaded by

chitra
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)
21 views75 pages

C Lab

The document contains multiple C programs demonstrating various programming concepts including I/O statements, operators, decision-making constructs (if-else, switch-case), looping statements (for, while, do-while), and one-dimensional arrays. Each section outlines the aim, algorithm, program code, output, and result of the execution. The programs cover tasks such as calculating sums of odd and even numbers, evaluating expressions, checking student grades, and finding factorials and Fibonacci series.

Uploaded by

chitra
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/ 75

Ex .

No:1 a
PROGRAM USING I/O STATEMENTS AND OPERATORS
Date:
AIM

To write a C Program to perform I/O statements and operators.

ALGORITHM

Step 1: Start
Step 2: Assign the values for Input and output process.
Step 3: Read the Input variable for finding the even and odd number of given upper
bound. Step 4: Using I/O statements and Operations for computational processing. Step 5:
Display the output of the calculations. Step 6: Stop

PROGRAM
#include <stdio.h>
void main ()
{ int s1 =
0; int s2 =
0; int ub;
int absDiff;
int n = 1;
clrscr();
printf("Enter the upper bound: ");
scanf("%d", &ub);
// compute the process
while(n <= ub)
{
if (n % 2 == 0)
{
S2 += n;
}
else
{
S1 += n;
}
++n;
}
if (s1> s2)
{
absDiff = s1– s2;
}
else
{ absDiff = s2–
s1;
}

// Print the results


printf("The sum of odd numbers is %d.\n", s1); printf("The
sum of even numbers is %d.\n", s2); printf("The absolute
difference is %d.\n", absDiff);
getch();
}
OUTPUT
Enter the
upper bound: 10
The sum of odd numbers is 25.
The sum of even numbers is 30.
The absolute difference is 5.

RESULT:
Thus the C Program using I/O statements and operators was executed and the
output was obtained.
Ex .No:1 b
PROGRAM USING EXPRESSIONS
Date:

AIM
To write a C Program to evaluate an expressions.

ALGORITHM

Step 1: Start the Program


Step 2: Declare variables for expression.
Step 3: Read the Input variable for given expression.
Step 4: Using expression for computational processing.
Step 5: Display the output of the calculations.
Step 6: Stop the program

PROGRAM:
#include<stdio.h>
#include<conio
.h> void main
() { int
a,b,c,d,e,f,g ;
clrscr(); printf
("\nEnter a
value:");
scanf("%d",&a);
printf("\nEnter b
value:");
scanf("%d",&b);
printf("\nEnter c
value:");
scanf("%d",&c);
printf("\nEnter d
value:");
scanf("%d",&d); printf
("\nEnter the e value:");
scanf("%d",&e); printf
("\nEnter the f value:");
scanf("%d",&f);
g=a*b/(b+c*b/d+a)
+e*(e/f);
printf ("\nthe total value of expression g is %d”, g);
getch(); }
OUTPUT:

Enter a value:2 Enter b


value:4 Enter c value:6
Enter d value:2 Enter
the e value:0 Enter the
f
value:3
the total value of expression g is 0

RESULT:

Thus the C program to evaluate an expression has been done successfully.


Ex .No:2a
Date: DECISION MAKING CONSTRUCTS (IF ELSE)

AIM
To write a C Program to perform decision making constructs using if else Condition.

ALGORITHM

Step1: Start the Program.


Step2: Declare variables for checking student grade. Step3:
Read the Input variable.
Step4: Check the condition grade<=10 by using if else condition.
Step5: If the condition is true the display the true condition statement otherwise display the
false condition statement.
Step6: Stop the program.

PROGRAM:
#include<stdio.h>
void main()
{ int
grade;
clrscr();
printf((“Enter your mark:”);
scanf("%d",&grade); if
(grade <= 10)
{
printf("YOU DID NOT STUDY.\n"); printf("YOU
FAILED ! \n");
}
else
{
if (grade < 60)
{
printf("You failed \n"); printf("Study
harder\n");
}
Else {
if (grade >= 60) printf("YOU
PASSED ! \n");
}}
getch()
;
}

OUTPUT:
Enter your mark:10 YOU
DID NOT STUDY. YOU
FAILED!
Enter your mark:30
You failed
Study harder

RESULT:
Thus the C Program to perform decision making constructs using if else Condition
executed successfully.

DECISION MAKING CONSTRUCTS


Ex.No:2.b
Date: (SWITCH CASE)

AIM
To write a C Program to perform decision making constructs using switch case statement.
ALGORITHM

Step 1: Start the program.


Step 2: Declare variables.
Step 3: Read the Input variable.
Step 4: Check the condition of my input by using switch case condition. Step 5: Choose
the case you want to display then display it into the output. Step 6: Stop the program,

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{ char
myinput;
int i;
clrscr();
printf("Which option will you choose:\n");
printf("a) Program 1 \n"); printf("b)
Program 2 \n"); scanf("%c", &myinput);
switch (myinput)
{ case
'a':
printf("Run program 1\n");
printf("Enter the i value:");
scanf("%d”, &i); if (i== 5)
{ printf("OK\n");
} else { printf("Not
OK\n");
} break; case 'b': { printf("Run
program 2\n"); printf("Please
Wait\n"); break;
}
default:
printf("I
nvalid
choice\
n");
break; }
getch();
}
OUTPUT:
Which option will you choose: a)
Program 1
b) Program 2 a
Run program 1
Enter the i value:5
OK
Which option will you choose: a)
Program 1
b) Program 2 a
Run program 1
Enter the i value:4
Not OK

RESULT:
Thus the C Program to perform decision making constructs using switch case statement
executed successfully.

DECISION MAKING CONSTRUCTS


Ex.No:2.c
Date: (CONTINUE STATEMENT)

AIM
To write a C Program to perform decision making constructs using continue statement.

ALGORITHM:

Step1: Start the program


Step2: Declare and Read the integer values for looping condition.
Step3: Check the condition i<=number if it’s true then proceed to next condition i %2
and then print iteration to output and go on.
Step4: Iterations are perform using for loop and break a line and continue the iteration
using continue command.
Step5: Display the output and Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{ int I,
number;
clrscr();
printf(“\n Please Enter any integer:”);
scanf(“%d”,&number); for (i=1;
i<=number;i++)
{ if(i
%2==0)
{
printf(“\n Even \n”,i);
continue; }
printf(“\n %d\n”,i);

getch();
}

OUTPUT:
Please Enter any integer:5 1
Even
3
Even
5
RESULT:
Thus the C Program to perform decision making constructs using continue statement
executed successfully.

DECISION MAKING CONSTRUCTS


Ex.No:2.d
Date: (GOTO STATEMENT)

AIM
To write a C Program to perform decision making constructs using Goto statement.

ALGORITHM

Step1: Start the program Step2:


Declare the variables.
Step3: Get the variable of mark for checking whether the student pass or fail. Step4: To
check the student mark whether they pass or fail using goto statement and display the
output.
Step5: Stop the program.

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{ int
totmark;
clrscr();
printf(“\n Enter the your mark:”);
scanf(“%d”,&totmark);
If(totmark>=50)
{ goto
pass; }
else{ Got
o
fail;
}
pass:
printf(“\n congratulation! You made it\n”); fail:
printf(“\n Better luck next time\n”); getch();
}
OUTPUT:

Enter the your mark: 56


congratulation! You made it
RESULT:
Thus C Program to perform decision making constructs using Goto statement was
executed successfully.

LOOPING STATEMENTS

Ex.No:3.a Date: (FOR LOOP)

AIM
To write a C Program to perform Looping statement using forloop condition.

ALGORITHM

Step1: Start the program Step2:


Declare the variables.
Step3: Read the variables for looping statement.
Step4: Perform the loop asper the condition and display the output. Step5:
Stop the program.

PROGRAM:
#include <stdio.h>
void main ()
{ int n,
n1;
clrscr();
Printf (“Enter the n value:”);
Scanf(“%d”,&n);
Printf (“Enter the n1 value:”);
Scanf(“%d”, &n1); for (int
a=1; a<n;a++)
{ for (int b=1; b<n1;
b++)
{ if(a==b) printf("%d,
%d\n",a,b);
}
}
getch();
}
PROGRAM 2:
/* For Loop in C Programming Example */
#include <stdio.h>
Voidmain () {
int i, number, total= 0;
clrscr();
printf ("\n Please Enter any integer\n");
scanf ("%d", &number); for (i=1; i<=
number; it+) total = total + i;
printf("\nSum of n natural numbers is: %d\n", total); getch
();
}

OUTPUT:
Please Enter any integer 20
Sum of n natural numbers is: 210

PROGRAM 3:
//Program for Nested for loop
#include <stdio.h>
#include<conio.h
> void main ()
{
int i, j;
clrscr();
for (i=9; i<=10; i++)
{ for (j=1; j<=10; j+
+)
{ printf ("%d * %d = %d\n”, i, j,
i*j);
}
} getch
();
}
OUTPUT:
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
RESULT:

Thus the C Program to perform Looping statement using for loop condition was executed
successfully.

LOOPING STATEMENTS

Ex.No:3.B Date: (WHILE LOOP)

AIM
To write a C Program to perform Looping statement using while loop.

ALGORITHM

Step1: Start the program


Step2: Declare the variables.
Step3: Read the variables for looping statement.
Step4: Perform the loop asper the condition and display the output. Step5:
Stop the program.

PROGRAM 1:
#include <stdio.h>
#include<conio.h>
void main()
{ int i,n,in;
i=1;
clrscr();
printf("Enter a value:");
scanf("%d",&n);
while(i<=n)
{ printf("\n");
in=i;
while(in<=n
)
{ printf("%d",in);
in=in+1; } i=i+1;
}
getch();
}

OUTPUT:
Enter the value:5
PROGRAM 2:
//program to find the factorial of numbers
#include <stdio.h> void
main()
{ int n,i,f;
f=i=1;
clrscr();
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{ f*=i;
i++; }
printf("The Factorial of %d is : %d",n,f);
getch(); }
OUTPUT:
Enter a Number to Find Factorial: 5 The
Factorial of 5 is: 120

PROGRAM 3:
//program to find the Fibonacci of numbers
#include<stdio.h> void
main ()
{ int count, n, t1=0, t2=1, Temp=0;
clrscr();
printf("Enter The Number of Terms :\n"); scanf("%d",&n);
printf("\nFibonacci Series : %d , %d , ", t1, t2);
count=2; while
(count<n)
{
Temp=t1+t2; t1=t2;
t2=Temp; ++count;
printf("%d ,
",Temp);
}
getch();
}

OUTPUT:
Enter the Number of Terms:
10
Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.
RESULT:

Thus the C Program to perform Looping statement using while loop was executed
successfully.

LOOPING STATEMENTS
Ex.No:3.c
Date: (DO-WHILE LOOP)

AIM
To write a C Program to perform Looping statement using do-while loop.

ALGORITHM

Step1: Start the program


Step2:Declare the variables.
Step3:Read the variables for looping statement.
Step4:Perform the loop asper the condition and display the output. Step5:Stop
the program.

PROGRAM 1:
//Program to calculate the interest
#include <stdio.h>
void main ()
{ float
a,b,c; char
ch='y'; do
{
printf("enter the principle:"); scanf("%f",&a);
printf("enter the Rate:"); scanf("%f",&b);
printf("enter the Tax:"); scanf("%f",&c);
printf("interest rate=%.5f",(a*b*c)/100);
printf("\n\n calculate interest again? ('y' for yes, 'n' for no): “); scanf("%c”,
&ch); } while(ch=='y');
}

PROGRAM 2:
#include <stdio.h>
#include <conio.h> void
main()
{ int i,
num;
clrscr();
printf("Enter a number: \n");
scanf("%d",&num); i=1; do
{
printf("%d\n", i); i+
+
} while (i<num);
getch();
}

PROGRAM 3:
#include <stdio.h>
#include <conio.h> void
main ()
{
int i;
clrscr();
printf("Even number upto 20 \n");
i=0; do { i=i+2; printf("%d\n",
i); }while (i<= 20); getch();
}

OUTPUT:
Even number up to 20 2
4
6
8
10
12
14
16
18
20
22
RESULT:
Thus the C Program to perform Looping statement using do-while loop was executed
successfully.

PROGRAM USING ONE-DIMENSIONAL ARRAY


Ex.No:4.a Date:

AIM
To implement a C Program to perform Array using 1-Dimensional array.

ALGORITHM

Step1:Start the program Step2:Assign


the variables.
Step3:Get the number of subject and the mark as an input. Step4:Perform the for-loop
asper the condition and display the output. Step5:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ()
{ int sub,marks,n,i,sum=0, tmp=0, arr[10],Percentage;
printf("\nEnter number of subject : \n");
scanf("%d", &n);
tmp=n*100;
printf("\nEnter The Marks: \n"); for
(i=0; i<n;i++)
{ scanf("%d",
&arr[i]);
} for (i=0;
i<n;i++)
{
sum=sum+arr[i];
}
Percentage = (sum * 100) / tmp;
printf("\nPercentage Of Student : %d\n", Percentage);
getch();
}
OUTPUT:

Enter number of subject :3


Enter the Marks:55
65
75
Percentage Of Student : 65

RESULT:
Thus the C Program to perform Array using 1-Dimensional array was executed
successfully.

PROGRAM USING TWO-DIMENSIONAL ARRAY


Ex.No:4.b
Date:

AIM
To implement a C Program to perform Array using 2-Dimensional array.

ALGORITHM
Step1:Start the program Step2:Assign
the variables.
Step3:Get the value of two matrices using 2-dimensional array. Step4:Adding two matrices
using 2-dimensional array and display the output. Step5:Stop the program.

PROGRAM 1:

#include <stdio.h>
void main ()
{ int
arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)

{ printf("\
n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}

OUTPUT:

Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34

Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....

56 10 30
34 21 34
45 56 78

RESULT:
Thus the C Program to perform Array using 2-Dimensional array was executed
successfully.

PROGRAM USING MULTI DIMENSIONAL ARRAY


Ex.No:4.c
Date:

AIM:
To implement a C Program to perform Array using multi-Dimensional array.

ALGORITHM:
Step1:Start the program
Step2:Assign the variables of test.
Step3:Print the value and store the input using multi- dimensional array.
Step4:Perform the for-loop asper the condition and display the values. Step5:Stop
the program.

PROGRAM:
#include <stdio.h>
int main() {
int grades[5][4];

// Prompt user to enter grades


for (int i = 0; i< 5; i++) {
printf("Enter grades for student %d:\n", i+1);
for (int j = 0; j < 4; j++)
{ printf("Subject %d: ", j+1);
scanf("%d", &grades[i][j]);
}
}

// Calculate average grade for each student printf("\


nAverage grade for each student:\n");
for (int i = 0; i< 5; i++) {
float sum = 0; for (int j
= 0; j < 4; j++) {
sum += grades[i][j];
}
float avg = sum / 4;
printf("Student %d: %.2f\n", i+1, avg);
}

// Calculate average grade for each subject printf("\


nAverage grade for each subject:\n");
for (int j = 0; j < 4; j++)
{ float sum = 0; for
(int i = 0; i< 5; i++) {
sum += grades[i][j];
}
float avg = sum / 5;
printf("Subject %d: %.2f\n", j+1, avg);
}

return 0;
}

OUTPUT:
Enter grades for student 1:
Subject 1: 80
Subject 2: 75
Subject 3: 90
Subject 4: 85
Enter grades for student 2:
Subject 1: 70
Subject 2: 85
Subject 3: 80
Subject 4: 75
Enter grades for student 3:
Subject 1: 90
Subject 2: 80
Subject 3: 85
Subject 4: 95
Enter grades for student 4:
Subject 1: 75
Subject 2: 90
Subject 3: 75
Subject 4: 80
Enter grades for student 5:
Subject 1: 85
Subject 2: 70
Subject 3: 80
Subject 4: 90
Average grade for each student:
Student 1: 82.50
Student 2: 77.50
Student 3: 87.50
Student 4: 80.00
Student 5: 81.25
Average grade for each subject:
Subject 1: 80.00
Subject 2: 80.00
Subject 3: 82.00
Subject 4: 85.00

RESULT:

Thus Program to using multi-Dimensional array executed successfully.

TREE TRAVERSAL USING ARRAY


Ex.No:4.d
Date:

AIM:
To implement a C Program to perform Traversal using array.

ALGORITHM:
Step1:Start the program
Step2:Assign the variables list and i variable for loop.
Step3:Get the value of array elements and perform the loop asper the loop condition.
Step4:Display the values(i.e., list) of traversal array.
Step5:Stop the program.

PROGRAM:
// C program to traverse the array
#include <stdio.h>
// Function to traverse and print the array
void printArray(int* arr, int n)
{ int
i;

printf("Array: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main() { int arr[] = { 2, -1,


5, 6, 0, -3 }; int n =
sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
return 0;
}

OUTPUT:

Array: 2 -1 5 6 0 -3
RESULT:

Thus the C Program to perform Traversal using array was executed successfully.

PROGRAM USING STRING OPERATIONS


Ex.No:5 Date:

AIM:
To implement a C program using strings and its operations.

ALGORITHMS:

Step1: Start the program.


Step2: Declare the values using string.
Step3: To perform the string operations using given values. Step4:
To Get the output of the string using string operations. Step5:
Stop the program.

PROGRAM 1:
//Program to count characters in given string using strlen function.
#include<stdio.h>
#include<string.h>
void main ()
{
char str [20] ="AHMEDABAD";
int i, cnt=0, len=0;
len=strlen(str);
printf(“Length of the string is %d”,len) for
(i=0; i < len; i++)
{ if(str[i] == 'A')
cnt++; } printf("Total A in given
string : %d",cnt); getch();
}

OUTPUT:
Length of the string is 9
Total A in given string: 3

PROGRAM 2:
//String Processing - Concatenation using strcat function.
#include<stdio.h>
#include<string.h>
int main()
{ char str1[10]="12345", str2[10]="abcd";
strcat(str1,str2);
puts(str1);
return 0;
}

OUTPUT:
12345abcd

PROGRAM 3:
//Count occurrences of alphabets in the given paragraph.
//This program will count only A to Z alphabets.
#include<stdio.h>
#include<string.h>
void main()
{ int i, counter[26]={}, ch; char
str[100]; puts("Enter your
paragraph:"); gets(str);

for(i=0; i<strlen(str); i++)


{ ch=str[i];
counter[ch-65]+
+;
} printf("Character and its frequency are as under:\
n"); for(i=0; i<26; i++)
{ if(counter[i]!=0) printf("%c = %d \n",
i+65,counter[i]);
}
getch();
}

OUTPUT:
Enter your paragraph:
HELLO STUDENT HOW ARE YOU
Character and its frequency are as under: A = 1
D=1
E =3
H=2
L=2
N=1
O=3
R=1
S =1
T =2
U=2
W=1
Y=1

PROGRAM 4:
#include <stdio.h>
#include
<string.h> void
main ()
{ char str1[10] = "12345";
char str2[10] = "World";
strcpy(str1, str2, 4);
str1[4]
='\0';
printf("strcpy( str1, str2, 3) = %s\n", str1 ); getch();
}

OUTPUT:
strncpy (str1, str2, 3) = Worl

PROGRAM 5:
//C program to print password as star ( ***** )
#include<stdio.h>
#include<string.h>
void main()
{ int i;
char ch1, password[10];
printf("Enter your
password:"); for(i=0; i<10; i+
+)
{ ch1=getch();
password[i]=ch1
;

if(ch1!=13) //13 is ASCII of Enter key printf("*");


if(ch1 == 13)
break;
}
password[i]='\0'; printf("\n%s",
password); getch();
}

PROGRAM 6:
// Use of built-in C string library function.
#include<stdio.h>
#include<string.h>
void main()
{ int len;
char str1[10], str2[10];
puts("Enter String 1:");
gets(str1); puts("Enter
String 2:");
gets(str2);
//Function to find length of given string 1. len =
strlen(str1); printf("\nLength of String 1 is %d.\n",
len); //Function to compare two strings
if(strcmp(str1,str2)==0) printf("\nString 1 and String
2 are same.\n"); else printf("\nString 1 and String 2
are not same.\n");
//Function to copy string 1 into string 2 strcpy(str2,str1); //this
will copy str1 into str2 printf("\nString 2 after execution of strcpy
= %s", str2); getch();
}

OUTPUT:
Enter String 1:
abc Enter
String 2:
xyz
Length of String 1 is 3.
String 1 and String 2 are not same. String 2 after
execution of strcpy = abc

PROGRAM 7:
// C Program to check given string is Palindrome or not.
#include <stdio.h>
#include <string.h>
void main()
{ char a[10], b[10]; printf("Enter
your string:\n"); gets(a);
strcpy(b,a);
strrev(b); if (strcmp(a,b) == 0) printf("Entered
string is a palindrome.\n"); else printf("Entered
string is not a palindrome.\n");
getch();
}

OUTPUT:
Enter your string:
ABA
Entered string is a palindrome.
RESULT :
Thus the C program to perform various string operation was executed and obtained output.
Ex .No:6.a C PROGRAM TO FIND THE GIVEN NUMBER AS AN ARMSTRONG
NUMBER OR NOT USING USER DEFINED FUNCTION
Date:

AIM:

To write a C program to find whether a given number in Armstrong or not

ALGORITHM:

Step 1: start
Step 2:read n
Step 3:assign sum 0,I m n,count =0
Step 4:if m>0 repeat
Step 4.1:m m/10
Step 4.2:count++
Step 4.3:until the condition fail
Step5: if I>0 repeat step 4 until condition fail
Step 5.1:rem I%10
Step 5.2:sum sum+pow(rem,count)
Step 5.3:I I/10
Step 6:if n=sum print Armstrong otherwise print not armstrong Step 7:stop

PROGRAM:

#include<stdio.h>

int isArmstrong(int number)


{

// declare
variables int
lastDigit = 0;
int power = 0;
int sum = 0;

// temporary variable to store


number int n = number;

while(n!=0) {

// find last digit


lastDigit = n % 10;

// find power of digit (order = 3) power


=
lastDigit*lastDigit*lastDigit;

// add power value into sum sum


+= power;

// remove last digit


n /= 10;
}

if(sum == number) return 0;


else return 1;
}

int main()
{ int
number;

printf("\nEnter number: ");


scanf("%d",&number);

if(isArmstrong(number) == 0) printf("\n
%d is an Armstrong number.\n",
number); else
printf("%d is not an Armstrong number.", number);
return 0;
}
OUTPUT:
Enter number:153
153 is an Armstrong number
RESULT:

Thus the C Program to find whether a given number in Armstrong or not was executed and
the output was obtained.
Ex .No:6.b C PROGRAM TO FIND MAXIMUM OF GIVEN THREE NUMBERS
USING FUNCTION WITH PARAMETER
Date:

AIM:

To find maximum of three number using sub function with three parameters

ALGORITHM:

main program
Step 1: start
Step 2: call subprogram as num1=input();num2=input();num3=input()
Step 3: call sub program as largest=large(num1,num2,num3)
Step 4: call subprogram as display(largest)
Step 5: stop
Sub program: large(float a, float b, float c)
Step 1: if a> b and a>c ; return a
Step 2: if b>a and b>c ; return b;
Step 3: other wise return c to main program
Sub program: input()
Step 1: read n
Step 2: return n
Sub program: display(float max)
Step 1: print max
PROGRAM:

#include<stdio.h>

// functions declaration float


input(); float large(float a,
float b, float c); void
display(float max);

// main
function
int main()
{ float num1, num2, num3, largest;
num1 = input();
num2 = input(); num3
=
input();

largest = large(num1, num2,


num3); display(largest);

return 0;
}

// function for taking input from user float


input()
{ float n; printf("\nEnter
number: "); scanf("%f",
&n); return n;
}

// function to find sum of two numbers


float large(float a, float b,
float c)
{ if(a>=b && a>=c) return a;
else if(b>=a && b>=c) return
b; else return c;
}

// function for displaying the result


void display(float max)
{ printf("\nLargest number = %.2f",max);
}
OUTPUT:
Enter number:10
Enter number:20
Enter number:4
Largest number =20
RESULT:
Thus the C Program to find Maximum of three numbers using function was executed and
the output was obtained.

Ex .No:6.c C PROGRAM TO SWAP TWO NUMBER USING PASS BY VALUE AND


REFERENCES
Date:

AIM:

To write a C program to swap to values using pass by value and pass by reference with
functions

ALGORITHM:

a)
main program
Step 1: start
Step 2: read a, b as integer value Step 3: call
sub program as swap(a,b) Step 4: stop
Sub program: swap(int a, int b)
Step 1: assign temp to a Step
2: assign a to b Step 3:
assign b to temp Step 4:
display a and b

b)
main program
Step 1: start
Step 2: read a, b as integer value
Step 3: call sub program as swap(&a,&b)
Step 4: stop
Sub program: swap(int *a, int *b)
Step 1: assign temp to *a Step 2:
assign *a to *b
Step 3: assign *b to temp
Step 4: display a and b
PROGRAM:

a) Pass by value #include


<stdio.h>
void swap(int , int); //prototype of the function
int main()
{ int a =
10; int b
= 20;
printf("Before swapping the values in main a = %d, b =
%d\n",a,b); swap(a,b);
printf("\nAfter swapping values in main a = %d, b = %d\n",a,b); } void
swap (int a, int b)
{ int
temp;
temp =
a; a=b;
b=temp;
printf("\nAfter swapping values in function a = %d, b = %d\n",a,b); }

b) Pass by reference #include


<stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{ int a =
10; int b
= 20;
printf("Before swapping the values in main a = %d, b =
%d\n",a,b); swap(&a,&b);
printf("\nAfter swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{ int
temp;
temp =
*a;
*a=*b;
*b=temp;
printf("\nAfter swapping values in function a = %d, b = %d\n",*a,*b); }
OUTPUT:
a)
Before swapping the values in main a = 10, b =
20 After swapping values in function a = 20, b =
10 After swapping values in main a = 10, b = 20

b)

Before swapping the values in main a = 10, b =


20 After swapping values in function a = 20, b =
10 After swapping values in main a = 20, b = 10

RESULT:
Thus the C Program to perform swap operation using pass by value and pass by reference
was executed and the output was obtained.
Ex .No:6.d C PROGRAM TO FIND THE SQUARE AND CUBE OF A NUMBER
USING A FUNCTION WITH RETURNVALUE
Date:
AIM:

To find square and cube of a given number using functions

ALGORITHM:

main program
Step 1: start
Step 2: read a as integer value
Step 3: call sub program as s = square(a)
Step 4: display s
Step 5: call sub program as c = cube(a) Step 6:
display c

Sub program: square(int a)


Step 1: return a*a
Sub program : cube(int a)
Step 1: return a*a*a
PROGRAM:

#include <stdio.h>

double cube(int);
double
square(int); int
main()
{ int num;
double
c,s;
printf("Enter any number: ");
scanf("%d", &num); c =
cube(num);

printf("\nCube of %d is %.2f", num, c); s=

square(num);

printf("\nSquare of %d is %.2f", num, s);

return 0;
}

double cube(int num)


{ return (num * num * num);
}

double square(int num)


{ return (num * num );
}

OUTPUT:
Enter any number:5
Cube of 5is 125
Square of 5 is 25

RESULT:
Thus the C Program to compute square and cube of a number using function was executed
and the output was obtained.
Ex .No:6.e C PROGRAM TO FIND THE SUM OF ‘N’ NATURAL NUMBER USING
FUNCTION
Date:
AIM:

To find the sum of N natural numbers using functions.

ALGORITHM:

main program
Step 1: start
Step 2: read range as integer value
Step 3: call sub program as result = sum(range)
Step 4: display result
Step 5 : Stop

Sub program: sum(int n)


Step 1: initialize add to 0(zero)
Step 2: Initialize i to 1Repeat Step 3 to Step 4until i reaches
n Step 3: add = add +1;
Step 4:
i=i+1 Step
5:return add
PROGRAM:
#include<stdio.h>

int sum(int n)
{ int i, add = 0;
for( i=1; i<=n; i++)
{ add +=
i; } return
add;
}

int main()
{ int range, result; printf("Upto which number you
want to find sum:
"); scanf("%d", &range);
result = sum(range);
printf("\n1+2+3+...+%d+%d = %d",range-1, range, result); }

OUTPUT:
Upto which number you want to find
sum:10 1+2+3+...+9+10= 55

RESULT:
Thus the C Program to compute sum of n natural numbers was executed and the output
was obtained.
Ex .No:6.f C PROGRAM TO FIND THE MEAN OF ‘N’ NATURAL NUMBERS
USING FUNCTION
Date:
AIM:

To write a C program to find the mean of N natural numbers using functions.

ALGORITHM:

main program
Step 1: start
Step 2: read range as integer value
Step 3: call sub program as result = sum(range)
Step 4: display result
Step 5: Compute mean as mean = result/range
Step 6: display mean
Step 7 : Stop

Sub program: sum(int n)


Step 1: initialize add to 0(zero)
Step 2: Initialize i to 1Repeat Step 3 to Step 4until i reaches
n Step 3: add = add +1;
Step 4:
i=i+1 Step
5:return add

PROGRAM:
#include<stdio.h

> int sum(int n)


{ int i, add = 0;
for( i=1; i<=n; i++)
{ add +=
i;
}
return add;
}

int main()
{ int range, result,mean; printf("Upto which number
you want to find sum:
"); scanf("%d", &range);
result = sum(range);
printf("\n1+2+3+...+%d+%d = %d",range-1, range, result);

mean=result/range;
printf("\nMean = %d",mean, result); }

OUTPUT:
Upto which number you want to find
sum:10 1+2+3+...+9+10= 55
Mean=5.5

RESULT:
Thus the C Program to compute mean of n natural numbers was executed and the output was
obtained.
Ex .No:7.a C PROGRAM TO FIND THE FACTORIAL OF GIVEN NUMBER
USING RECURSIVE FUNCTION
Date:

AIM:

To write a C program to find the factorial of a given number using recursive function.

ALGORITHM:

main program
Step 1: start Step
2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value Step 5:
stop
Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3 Step 3: return
n*fact(n-1) to main program
PROGRAM:

#include<stdio.> int

factorial(int n)
{ if(n!=0) return n*factorial(n-1); // general
case
else return 1; // base
case
}

int main()
{ int num, result; printf("Enter a
positive number: ");
scanf("%d",&num); result=
factorial(num); //function call
printf("\nResult = %d\n",result);
return 0;
}

OUTPUT:
Enter a positive number: 5
Result=120

RESULT:
Thus the C Program to compute factorial of a given number using recursive funcion was
executed and the output was obtained.
Ex .No:8.a
C PROGRAM TO DISPLAY ADDRESS AND VALUE OF VARIABLE
Date:
USING POINTER

AIM:

To write a C program to display address and value of a variable using pointer.

ALGORITHM:

Step 1: Start
Step 2 : Declare a variablecand a pointer variablepcof type integer. Step
3: Display value of c and address of c using vaiable c and reference
operator &. Step 4: Assign c to pc;
Step 5: Display value of c and address of c using pointer vaiable
pc. Step 6: Change value of c using pointer variable pc. Step 7:
Repeat step 3 and step 5 Step 8 : Stop.
PROGRAM:

#include <stdio.h> int


main()
{ int* pc, c;

c = 22; printf("Address of c: %p\


n", &c); printf("Value of c: %d\
n\n", c);

pc = &c; printf("Address of pointer pc:


%p\n", pc); printf("Content of pointer pc:
%d\n\n",
*pc);

c = 11; printf("Address of pointer pc: %p\


n", pc); printf("Content of pointer pc:
%d\n\n",
*pc);

*pc = 2; printf("Address of c:
%p\n", &c); printf("Value of c:
%d\n\n", c); return 0;
}

OUTPUT:
Address of c: FFF8
Value of c:22
Address of pointer pc:
FFF01 Content of pointer
pc:22 Address of pointer pc:
FFF01 Content of pointer
pc:11
Address of c: FFF8
Value of c:2

RESULT:

Thus the C program to display address and value of a variable was executed successfully and
obtained output.
Ex .No:8.b C PROGRAM TO PRINT STRING USING
POINTERS
Date:

AIM:

To write a C program to print string value using pointer.

ALGORITHM:

Step 1: Start
Step 2: Declare a character array and a character pointer
variable
Step 3: Read a string into character array..
Step 4: Assign pointer variable to character array.
Step 5: Repeat step 6 until array element ‘/0’ is reached
Step 6: Display string value by reading each element of the array using pointer
variable Step 7 : Stop
PROGRAM:

#include <stdio.h> int


main()
{ char str[100];
char
*ptr;

printf("Enter any string :: "); scanf("%s",str);

//assign address of str to ptr


ptr=str; printf("\nThe entered
string is :: ");

while(*ptr!='\0') printf("%c",*ptr+
+);

return 0;
}

OUTPUT:

Enter any string :: C_Programming


The entered string is :: C_Programming

RESULT:

Thus the C program to display string value using pointer was executed successfully and obtained
output.

Ex .No:8.c C PROGRAM TO ACCESS ARRAY ELEMENT USING POINTERS


Date:

AIM:

To write a C program to access array elements using pointer.

ALGORITHM:
Step 1: Start
Step 2: Declare an integer array ”a” and a integer pointer variable “p”
Step 3: Assign values to integer array. Step 4: Assign pointer variable
to integer
array. Step 5: Initialize i to 0
Step 5: Repeat step 6 until i reaches size of the integer array “a” Step
6: Display each element of the array using pointer variable “p”
Step 7 : Stop

PROGRAM:
#include<stdio.h
> void main()
{ int
a[5]={5,4,6,8,9};
int *p=&a[0];
int i; clrscr();
for(i=0;i<5;i++)
printf("%d\
t",*(p+i)); getch();
}

OUTPUT:

5 4 6 8 9

RESULT:
Thus the C program to display array elements using pointers executed successfully and obtained
output.

Ex .No:9.a
Date: C PROGRAM TO DISPLAY BOOK DETAILS USING STRUCTURE

AIM:

To write a C program to display book details using structure .

ALGORITHM:

Step 1: Start
Step 2: Declare structure "book" having data member as book_name, book_id, book_price, author.
Step 3: Declare book of size 3
Step 4: Read Book details – use loop construct that iterates thrice.
Step 5: Display Book details – use . operator to access structure members. – use loop another
loop construct that iterates thrice.
Step 6: Stop
PROGRAM:

#include<stdio.h>

struct book
{ char book_name[20];
int bookid; float
book_price; char
author[15];
};

int main()
{ struct book b[3];
int i;
//clrscr();
for(i=0;
i<3; i++)
{ printf("Enter details of book #%d\n",
i+1); printf("Enter book id: ");
scanf("%d", &b[i].bookid);
printf("Enter book name: ");
scanf("%s", b[i].book_name);
printf("Enter book author: ");
scanf("%s", b[i].author);
printf("Enter book price: ");
scanf("%f", &b[i].book_price);
}

for(i=0; i<3; i++)


{ printf("\nBook %d. ......... \n\n", i+1);
printf("Book Id: %d\n", b[i].bookid);
printf("Book Name: %s\n",
b[i].book_name); printf("Book
Author: %s\n", b[i].author);
printf("Book price: %f",
b[i].book_price);
} return
0;
}

OUTPUT:
Enter details of book #1
Enter book id: 101
Enter book name: LetUsC Enter
book author:
Kanetkar Enter book price:
350.20 Enter details of
book #2 Enter book id: 102
Enter book name: ANSI_C
Enter book author:
Balgurusamy Enter book price:
300.78
Enter details of book #3
Enter book id: 103
Enter book name: OperatingSystem
Enter book author: Godbole
Enter book price: 295.67
Book 1.............
Book Id: 101
Book Name: LetUsC
Book Author: Kanetkar
Book price: 350.200012

Book 2.............
Book Id: 102
Book Name: ANSI_C Book
Author:
Balgurusamy Book price:
300.779999

Book 3.............
Book Id: 103 Book
Name:
OperatingSystem Book
Author: Godbole
Book price: 295.670013

RESULT:
Thus the C program to display book details using structure executed successfully and obtained
output.

Ex .No:9.b C PROGRAM TO DISPLAY DATE USING STRUCTURES AND


POINTERS
Date:
AIM:

To write a C program to display the date using structures and pointers.

ALGORITHM:

Main program:
Step 1: Start
Step 2: Declare structure "date" having data member as day, month,
year Step 4: Read Date details. Step 5: Call subprogram as
display(&d) Step 6: Stop

Sub program : Display(struct date* d)


Step 1: Display Date details – use -> operator to access structure members.

PROGRAM:

#include<stdio.h>

void display(struct date*);


struct date { int day; char
month[10]; int year;
};

int main(){ struct date d; printf("Enter the


day , month and year : "); scanf("%d%s
%d", &d.day, d.month,
&d.year); display(&d);
return 0;
}

void display(struct date *d)


{
printf("\nDAY:%d" , d->day); printf("\nMONTH:%s",d->month);
printf("\nYEAR:%d",d->year);
}

OUTPUT:
Enter the day , month and year :
17
JUL
Y
2001
DAY: 17
MONTH :
JULY YEAR :
2001

RESULT:

Thus the C program to display date using structures and pointers executed successfully and
obtained output.

Ex .No:9.c
C PROGRAM TO DISPLAY JOB DETAILS USING UNION AND
Date: STRUCTURE
AIM:

To write a C program to display job details using structures and unions.

ALGORITHM:

Main program:
Step 1: Start
Step 2:Declare structure "sjob" having data member as name, salary, workerno
Step 3: Declare structure "sjob" having data member as name, salary, workerno
Step 4: Display size information of uJob and sJob, Step
5: Assign values to uJob members.
Step 6: Assign values to sJob members.
Step 7: Display salary details – use . operator to access union member and structure members
Step 8: Stop
PROGRAM:

#include <stdio.h> union


unionJob
{

char
name[32];
float salary;
int workerNo;
} uJob;

struct structJob
{ char
name[32];
float salary; int
workerNo;
} sJob;

int main()
{ printf("size of union = %d bytes",
sizeof(uJob));
strcpy(uJob.name,"Akash");
uJob.salary=11000.5; uJob.wokerNo=101;
printf("\nsize of structure = %d bytes",
sizeof(sJob));
strcpy(sJob.name,"Akash");
sJob.salary=11000.5;
sJob.wokerNo=101; printf("\nSalary in
Union =
%f",uJob.salary); prinf("\nSalary in Strucure
= %f",sJob.salary); return
0;
}
OUTPUT:

size of union = 32 bytes size


of structure = 38
bytes
Salary in union = 10944.098633
Salary in Structure =
11000.50000

RESULT:

Thus the C program to display job details using structure and union executed successfully
and obtained output.

Ex .No:10.a
C PROGRAM TO PERFORM WRITE OPERATION ON A FILE
Date:

AIM:

To write a C program to perform write operation on file.

ALGORITHM:

Step 1: Start
Step 2: Declare FILE pointer
Step 3: Open file program.txt in write mode
Step 4: Check if file pointer returns NULL
Step 5: If Step 4 returns NULL - display ERROR Else goto Step 6
Step 6: Read a number through console
Step 7: Write that number to file through file pointer
Step 8: Stop

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num; FILE *fptr; fptr =
fopen("program.txt","w");

if(fptr == NULL)
{ printf("Error!");
exit(1); }
printf("Enter num:
");
scanf("%d",&num);

fprintf(fptr,"%d",num);
fclose(fptr);

return 0;
}

OUTPUT:

Enter num:3456

Program.txt

3456
RESULT:

Thus the C program to write operation on file executed successfully and obtained output.
Ex .No:10.b
C PROGRAM TO PERFORM READ OPERATION ON A FILE
Date:
AIM:

To write a C program to perform read operation on file.

ALGORITHM:

Step 1: Start
Step 2: Declare FILE pointer
Step 3: Open file program.txt in read mode
Step 4: Check if file pointer returns NULL
Step 5: If Step 4 returns NULL - display ERROR Else goto Step 6
Step 6: Read a number from file as num
Step 7: Display num
Step 8: Stop

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{ int num;
FILE
*fptr;

if ((fptr = fopen("program.txt","r")) ==
NULL){ printf("Error! opening file");

// Program exits if the file pointer returns


NULL. exit(1);
}

fscanf(fptr,"%d", &num);

printf("Value of n=%d", num); fclose(fptr);

return 0;
}

OUTPUT:
RESULT:

Thus the C program to read operation on file executed successfully and obtained output.

Ex .No:10.c
Date: C PROGRAM TO PERFORM RANDOM ACCESS OVER A FILE

AIM:

To write a C program to perform random access on file using seek function.

ALGORITHM:

Step 1: Start
Step 2: Declare a file pointer
Step 3: Open file.txt in Write Append mode (w+)
Step 4: Write string to file
Step 5: Locate position 7 in the file using fseek function
Step 6:Stop

PROGRAM:
#include<stdio.h>

int main()
{
FILE *fp; fp =
fopen("jtp.txt","r");
if(!fp)
{
printf("Error: File cannot be opened\n");
return 0;
}
//Move 3 bytes forward, so if we print all the way through, we won't see the first 6
bytes.

fseek(fp, 3, 0);
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
//Here, we go through the entire file and print everything up until the end.
printf("%c",ch);
}

fclose(fp);
return 0;
}
OUTPUT:
is best

RESULT:
Thus the C program to random access on file executed successfully and obtained output.
Ex .No:10.d
C PROGRAM TO FIND AREA OF A CIRCLE USING MACRO
Date:

AIM:

To write a C program to compute area of a circle using preprocessor directive MACRO

ALGORITHM:

Step 1: Start
Step 2: Define a MACRO PI as 3.1415
Step 3: Declare variable radius and area
Step 4: Read radius
Step 5: Compute area as PI * radius * radius
Step 6: Display area Step
7: Stop

PROGRAM:

#include <stdio.h>
#define PI 3.1415

int main()
{ float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);

// Notice, the use of PI


area = PI*radius*radius;

printf("\nArea=%.2f",area)
; return 0;
}

OUTPUT:

Enter the radius:5


Area=157.075

RESULT:
Thus the C program to compute area of a circle using preprocessor directive MACRO was
executed successfully and obtained output.

You might also like