C Lab-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Program 1: Write a simple C program using conditional operators.

CONDITIONAL OPERATOR

Aim:

To write a sample c program using conditional operator.

Algorithm:

Step 1: Start the program.

Step 2: Declare a variable and get the value for it.

Step 3: Check the voting eligibility using conditional operator.

Step4: Display the result.

Step 5: Stop the program.

CODING:

#include<stdio.h>

#include<conio.h>
void main()
{
int age;
clrscr();
printf(“\n Enter your age:”);
scanf(“%d”,&age);
(age>=18)? Printf(“\n Eligible to vote”); : printf(“\n Not Eligible to vote”);
getch();
}
Program 2: Write a C program using branching structures (If, switch, goto).

BRANCHING STRUCTURES

Aim:

To write a c program using branching structures.

Algorithm:

Step 1: Start a program.

Step2: Initialize and declare the necessary variables.

Step 3: By using switch statement you can view the account details, deposit, and
withdraw.

Step 4: If else branching condition is used in withdraw case to check the condition.

Step 5: Then goto statement is used at the end of the program to transfer the control of the
program

Step 6: Display the result.

CODING:

#include<stdio.h>

#include<conio.h>
void main()
{
char ch,name[20];
int amount=1000,no,damt,wamt;
clrscr();
printf(“\n \t\t Bank Details”);
printf(“\n Enter the Customer Id:”);
scanf(“%d”,&no);
printf(“\n Enter the Customer Name:”);
scanf(“%s”,&name);
a:
printf(“\n a.View Account Details\n b. Deposit\n c. Withdraw\n d.Exit”);
printf(“\n Enter your choice:”);
scanf(“%s”,&ch);
switch(ch)
{
case ‘a’:
printf(“\n Customer Id : %d”, no);
printf(“\n Customer Name : %s “, name);
printf(“\n Current balance: %d”, amount);
break;
case ‘b’:
printf(“\n Customer Id : %d”, no);
printf(“\n Customer Name : %s “, name);
printf(“\n Current balance: %d”, amount);
printf(“\n Enter the amount to deposit:”);
scanf(“%d”,&damt);
amount=amount + damt;
printf(“\n Current balance after deposit :%d”, amount);
break;
case ‘c’:
printf(“\n Customer Id : %d”, no);
printf(“\n Customer Name : %s “, name);
printf(“\n Current balance: %d”, amount);
printf(“\n Enter the amount to be withdraw:”);
scanf(“%d”,&wamt);
if(amount<=500)
{
printf(“\n Balance is low”);
}
else
{
amount=amount-wamt;
printf(“Balance :%d”,amount);
}
break;
case ‘d’:
exit();
default:
printf(“\n Enter the correct choice”);
break;
}
goto a;
getch();
}
Program 3: Write a simple C program using looping structures (for, while, do-while).

LOOPING STRUCTURES

Aim:

To write a c program using looping structures.

Algorithm:

Step 1: Start the program.

Step 2: Declare the variable n and get the value for it.

Step 3: Using for loop, multiplication table is calculated for n.

Step 4: Display the multiplication table.

Step 5: Stop the program.

CODING:

#include<stdio.h>

#include<conio.h>
void main()
{
int i,n;
clrscr();
printf(“\n Enter a number:”);
scanf(“%d”,&n);
for(i=1;i<=10;i++)
{
printf(“%d * %d = %d \n”, i, n, i*n);
}
getch();
}
Program 4: Write a C program for matrix multiplication.

MATRIX MULTIPLICATION

Aim:

To write a c program for matrix multiplication.

Algorithm:

Step 1: Start the program.

Step 2: Declare three arrays a[10][10], b[10][10],c[10][10].

Step 3: Using nested for loop get the values for matrix a and b.

Step 4: Multiple the values of two matrix a and b and the result are stored in matrix c.

Step 5: Display the matrix c.

Step 6: Stop the program.

CODING:

#include<stdio.h>
#include<conio.h>
void main()
{

int a[10][10], b[10][10], c[10][10],I,j,k;


int sum=0;
clrcsr();
printf(“\n Enter first matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n Enter second matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=0;
for(k=0;k<=2;k++)
{
sum=sum+a[i][k] * b[k][j];
}
c[i][j]=sum;
}
}
printf(“\n Multiplication of two matrices:\n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”, c[i][j]);
}
printf(“\n”);
}
getch();
}
Program 5: Implement a Program to perform String Operations without library functions.

STRING MANIPULATION

Aim:

To write a c program for string manipulation.

Algorithm:

Step 1: Start the program.

Step 2: Declare two string variables s1 and s2.

Step 3: Read the values for string variables.

Step 4: using string function strcmp() and strlen() , compare strings and count length of
the string.

Step 5: Display the result.

Step 6: Stop the program.

CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[10],s2[10],n;
clrscr();
printf(‘\n Enter the first string:”);
scanf(“%s”,s1);
printf(‘\n Enter the second string:”);
scanf(“%s”,s2);
if(strcmp(s1,s2)==0)
{
printf(“\n Two strings are equal”);
}
else
{
printf(“\n Two strings are not equal”);
}
printf(“\n Length of first string :%d”,strlen(s1));
printf(“\n Length of second string :%d”,strlen(s2));
getch();
}
Program 6: Write a C program for Recursion.

RECURSION

Aim:

To write a c program for recursion.

Algorithm :

Step 1: Start the program.

Step 2: Declare the variables.

Step 3: Create a function fact(n).

Step 4: Get the value from the user to calculate the factorial.

Step 5: Display the result.

Step 6: Stop the program.

CODING:

#include<stdio.h>

#include<conio.h>

long int fact (long int);

void main()

long int num,f;

clrcsr();

printf(“\n Enter a number:”);

scanf(“%li”,&num);

f=fact(num);
printf(“\n Fcatorial is :%li”,f);

getch();

long int fact(long int n)

if(n==1)

return 1;

else

return(n*fact(n-1));

}
Program 7: Implement a C Program to print the given number in reverse order.

PRINTING THE NUMBERS IN REVERSE ORDER

Aim:

To write a c program to print the given number in reverse order .

Algorithm:

Step 1: Start the program.

Step 2: Declare the variables.

Step 3: Get the input value from the user.

Step 4: By using for loop, reverse the given numbers.

Step 5: Display the result.

Step 6: Stop the program.

CODING:

#include<stdio.h>

#include<conio.h>

void main()

int num,r,sum=0,t;

clrcsr();

printf(“\n Input a Number:”);

scanf(“%d”,&num);

for(t=num;num!=0;num=num/10)
{

r=num%10;

sum=sum*10+r;

printf(“\n The number in reverse order is : %d\n”,sum);

getch();

}
Program 8: Design a Structure as Employee which contains Name, Age, Designation and
Salary. Write a C program to access and print the N Employee Details.

EMPLOYE DETAILS USING STRUCTUTRE

Aim:

To write a c program to access and print N employee details

Algorithm :

Step 1: Start the program.

Step 2: Create a structure employee with name, age, designation, and salary as structure
member.

Step 3: Read the employee details.

Step 4: Display the result.

Step 5: Stop the program.

CODING:

#include<stdio.h>

#include<conio.h>

struct employee

int age,salary:

char name[25],desig[30];

}emp[100];

void main()

{
int i,n;

clrscr();

printf(“enter number of employess:\n”);

scanf(“%d”,&n);

printf(“entrer employee info as name,age,designation,salary\n”);

for(i=0:i<n;i++)

scanf(“%s%d%s%d”,&emp[i],name,&emp[i],age,&emp[i].design,&emp[i],salary);

printf(“\n emp_name\t emp_age\t\temp_design\t\temp_sal\n”);

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

printf(“%s\t\t%d\t\t%d\n”,emp[i].name,emp[i].age,emp[i]design,emp[i].salary);

getch();

}
9.Write a C program to implement Pointers

ARITHMETIC OPERATORS USING POINTERS

Aim:

To write a C program to implement pointer.

Algorithm:

Step-1: start the program

Step-2: Declare two pointer variable *p1 and *p2

Step-3: Read two values from user and assign the address of two variable to p1 and p2

Step-4: Using pointer variables perform the arithmetic operations

Step-5: Display the result

Step-6: Stop the program

CODING:

#include<stdio.h>

#include<conio.h>

void main()

int a,b,sub,add,mul,div,*p1,*p2;

clrscr();

printf(“\n arithmetic operations using pointers”);

printf(“enter two numbers:”);

scanf(“%d%d”,&a,&b);

p1=&a;
p2=&b;

add=*p1+*p2;

sub=*p1-*p2;

mul=*p1*p2;

div=*p1/*p2;

printf(“\n addition:%d”,add);

printf(“\n subtraction:%d”,sub);

printf(“\n multiplication:%d”,mul);

printf(“\n division :%d”,div);

getch();

}
10.Write a C program to copy the content of one file into another.

FILE COPYING

Aim:

To write a C program to copy the content of one file into another.

Algorithm:

Step-1: Start the program

Step-2: Create two text files text1.txt and text2.txt

Step-3: Create two file pointers fp1 and fp2

Step-4: open the file pointer fp1 in read mode and fp2 in write mode

Step-5: Copy the context of text1.txt to text2.txt

Step-6: Close the files

Step-7: Stop the program

CODING:

#include<stdio.h>

#include<conio.h>

void main()

file *fp1,*fp2;

char a;

printf(“\n copy the content of one file into another”);


fp1=fopen(“text1.txt”,”r”);

if(fp1==NULL)

puts(“\n cannot open this file”);

exit(1);

fp2=fopen(“text2.txt”,”w”);

if(fp2==null)

puts(“not able to open this file”);

fclose(fp1);

exit(1);

do

a=fgetc(fp10;

fputc(a,fp2);

while(a!=EOF);

printf(“\n\t successfully file copied”);

fclose alt();

getch();

You might also like