0% found this document useful (0 votes)
61 views71 pages

C Programs

Simple programs
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)
61 views71 pages

C Programs

Simple programs
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/ 71

Ex.

no:1 FORMATTED I/O FUNCTION

Date:

AIM:

To write a c program to implements formatted i/o functions.

ALGORITHM:

Step-1:Start the program.

Step -2: Declare the variables "num"(integer),"ch"character, "str"(character array)and "f"float.

Step-3:clear the screen using the "clrscr()"function.

Step-4: Prompt the user to enter an integer and read the input the variable"num" using "scanf("%d",&
num)".

Step-5:Print the entered using "printf("\n entered interger is%d",num)".

Step-6: Prompt the uses to enter a float and read the input the variable"f"using "scanf("%f",&f)".

Step-7: Print the entered float using"printf("\n entered float is :%d",f)".

Step-8: Prompt the user to enter a character and read the input into the variable"ch" using
"scanf("%s",&ch)".

Step-9: Print the entered character using "printf("\n entered character is:%s",ch)".

Step-10: Wait for the user to press a key using "getch()".

Step -11:End the program.


CODING:

#include<stdio.h>

#inlcude<conio.h>

Void main()

int num;

float f;

char ch str[100];

clrscr();

printf(“Enter the integer:”);

scanf(“%d”,&num);

printf(“\nEntered integer:%d”,num);

printf(“\nEnter the float:”);

scanf(“%f”,&f);

printf(“\nEntered float:%f”,f);

printf(“\nEnter the character:”);

scanf(“%s”,&ch);

printf(“\nEntered character:%s”,ch);

getch();

OUTPUT:

Enter the integer:2

Entered integer:2

Enter the float:5.4

Entered float:5.4000
Enter the character:S

Entered character:S

RESULT:

Hence the expected output has been executed successfully.


Ex.no:2 UNFORMATTED I/O FUNCTIONS

Date:

AIM:

To write a c program implement unformatted I/O function.

ALGORITHM:

Step-1: Start the program.

Step-2: Declare the main function using the "void" keywords.

Step-3: Inside the main function declare an integer variable called "x".

Step-4: Use that printf function is display the message "please enter the character here".

Step-5: Use the getchar function to read a character from the uses under and store it in the variable"x".

Step-6: Use the putchar function to display the characters stored in the variable"x".

Step-7:End the program.


CODING:

#include<stdio.h>

#include<conio.h>

Void main()

int x;

clrscr();

printf(“\nPlease enter a character here:”);

x=getchar(x);

putchar(x);

getch();

OUTPUT:

Please enter a character here: H

RESULT:

Hence the expected output has been executed successfully.


Ex.no:3 ARITHMETIC OPOERATORS

Date:

AIM:

To write the c program to perform arithmetic operators.

ALGORITHM:

Step -1: Start the program

Step-2: Declare integer variable "a","b","sum","sub","mult","div" and"rem".

Step-3:Clear the screen using to enter the values of "a" and "b".

Step-4: Prompt the user to enter the values 'a' and 'b'.

Step-5:Read the values of 'a' and 'b' from the user.

Step-6: Calculate the sum of 'a' and 'b' and store it in the 'sum' variable.

Step-7:Calculate the subtraction of 'a' and 'b' and store it in the 'sub' variable.

Step-8: Calculate the multiplication of 'a' and 'b' and store it in the 'mult' variable.

Step-9: Calculate the division of 'a' and 'b' and store it in the 'div' variable.

Step-10: Calculate the reminder of 'a' and 'b' and store it in the 'rem' variable.

Step-11:Print the results of all arithmetic operations.

Step-12: End the program.


CODING:

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

int a,b,sum,sub,mult,div,rem;

clrscr();

printf(“Enter the values of a and b:\n”);

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

sum=a+b;

sub=a-b;

mult=a*b;

div=a/b;

rem=a%b;

printf(“\nThe addition of two number is %d\n”,sum);

printf(“\nThe subtraction of two number is %d\n”,sub);

printf(“\nThe multiplication of two number is %d\n”,mult);

printf(“\nThe division of two number is %d\n”,div);

printf(“\nThe remainder of two number is %d\n”,rem);

getc();

return 0;

OUTPUT:

Enter the values of a and b : 4 2


The addition of two numbers is 6

The subtraction of two number is 2

The multiplication of two number is 8

The division of two number is 2

The remainder of two number is 0

RESULT:

Hence the expected ouput has been executed successfully.


Ex.no:4 CONDITIONAL OPERATORS

Date:

AIM:

To write the c program to find the greatest number using conditional operators.

ALGORITHM:

Step-1: Start the program.

Step -2:Declare three variable "a", "b" and "s" as integer.

Step-3: Prompt the uses to enter the two number.

Step-4:Read and store the values of 'a' and 'b' from the user.

Step-5:Use the conditional operator to compare 'a' and 'b'.

Step-6:Assign the greatest value to 's'.

Step-7: Print the value of 's' as the greatest of the two number.

Step-8: End the program.


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int a,b,s;

clrscr();

printf(“Enter two numbers:”);

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

s=a>b?a:b;

printf(“\nThe greatest of two number is : %d”,s);

getch();

OUPUT:

Enter two numbers: 3 6

The greatest of two number is : 6

RESULT:

Hence the expected output has been executed successfully.


Ex.no:5 LOGICAL OPERATORS

Date:

AIM:

To write a c program to implement various logical operations.

ALGORITM:

Step-1: Start the program.

Step-2: Declare integer variable "a","b","c" and result.

Step-3: Assign the 5 to variables a and b and assign the value 10 to variable c.

Step -4: calculate the result of the expression (a=b)(c>b) and store it in the variable result.

Step-5: print the result using printf using format"(a=b)&&(c>b) equal to %d\n", result.

Step -6: calculate the result of expression (a=b)&&(c<b) and store it in variable result.

Step-7: print the value of result using printf using the format "(a=b)&&(c<b) equal to %d\n", result.

Step -8: the result of the expression (a=b)||(c<b) and store it in the variable result

Step -9: print the value of result using the format "(a=b)||(c<b) equal to %d\n", result.

Step-10: calculate the result of the expression (a!=b)||(c<b) and store it in the variable result.

Step -11: print a value of result using the formula"(a!=b)||(c<b) equal to %d\n", result.

Step-12: calculate the result of the expression !(a!=b) and store it in the variable result.

Step -13: print the value of result using the format !(a!=b) equal to %d\n", result.

Step -14: calculate the result of the expression !(a==b) and store if in the variable result

Step -15: print the value of result using format !(a==b) equal to %d\n", result.

Step -16: Return 0 to indicate successful execution of the program.

Step-17:End the program.


CODING:

#include<stdio.h>

#include<conio.h>

int main()

int a=5,b=6,c=10,result;

clrscr();

result=(a=b)&&(c>b);

printf(“(a=b)&&(c>b) equals to %d\n”,result);

result=(a=b)&&(c<b);

printf(“(a=b)&&(c<b) equals to %d\n”,result);

result=(a=b)||(c<b);

printf(“(a=b)||(c<b) equals to %d\n”,result);

result=(a!=b)||(c<b);

printf(“(a!=b)||(c<b) equals to %d\n”,result);

result=!(a!=b);

printf(“!(a!=b) equals to %d\n”,result);

result=!(a==b);

printf(“!(a==b) equals to %d\n”,result);

getch();

return 0;

OUTPUT:

(a=b)&&(c>b) equals to 1

(a=b)&&(c<b) equals to 0
(a=b)||(c<b) equals to 1

(a!=b)||(c<b) equals to 0

!(a!=b) equals to 1

!(a==b) equals to 0

RESULT:

Hence the expected output has been executed successfully.


Ex.no:6 ODD OR EVEN USING IF ELSE STATEMENT

Date:

AIM:

To write the c program to determine whether an integer is odd or even by using if..else statement.

ALGORITHM:

Step 1: start the program

Step 2: prompt the user to input an integer

Step 3: Use the % operator to calculate the remainder when the integer is divided by 2

Step 4: Check if the remainders is equal too

Step 5: If the remainder is 0 then the integer is even print a message starting that the number is even

Step 6: If the remainder is not 0 then the integer is odd. Print a message starting that the number is odd

Step 7: End the program.


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int num1,rem1;

clrscr();

printf(“Input an integer:”);

scanf(“%d”,&num1);

rem1=num1%2;

if(rem1==0)

printf(“\n%d is an even integer”,num1);

else

printf(“\n%d is an odd integer”,num2);

getch();

return 0;

OUTPUT:

Input an integer: 376

376 is an even integer

RESULT:

Hence the expected output has been executed successfully.


Ex.no:7 GREATEST NUMBER USING NESTED IF STATEMENT

Date:

AIM:

To write a c program to find the greatest number among three input using nested if condition.

ALGORITHM:

Step-1: Start the program

Step-2: Declare the variables to store the numbers

Step-3: Ask the user to input the values of the three numbers

Step-4: Read and assign the user's input to the variables

Step-5: Print the values of the three numbers

Step-6: Compare the numbers using if-else statements to determine the greatest number

Step-7: Print the result starting which number is the greatest among the three.

Step-8: End the program.


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int num1,num2,num3;

clrscr();

printf(“Input the values of three numbers:”);

scanf(“%d %d %d”,&num1,&num2,&num3);

printf(“\n1st Number=%d\t2nd Number=%d\t 3rd Number=%d”,num1,num2,num3);

if(num1>num2)

if(num1>num3)

Printf(“\nThe 1st Number is greatest among three.”);

else

Printf(“\nThe 3rd Number is greatest among three.”);

else if(num2>num3)

Printf(“\nThe 2nd Number is greatest among three.”);

}
else

{
printf(“\nThe 3rd Number is thr greatest among three.”);

getch();

OUTPUT:

Input the values of three numbers:8 6 4

1st Number=8 2nd Number=6 3rd Number=4

The 1stNumber is thr greatest among three.

RESULT:

Hence the expected output has been executed successfully.


Ex.no:8 IF ELSE LADDER

Date:

AIM:

To write a c program to calculatae the marks, percentage and division for student based dddddon their
marks in physics,chemistry and computer application using if else ladder statement.

ALGORITHM:

Step-1: start the program.

Step-2: declare variable.

Step-3: prompt the user for roll number.

Step-4: Read & store the roll number.

Step-5 : promt the user for the name.

Step-6 : Read and store the name.

Step-7: prompt the user for physics , chemistry and computer Application.

Step-8 : Read and store the physics, chemistry and computer Applications marks.

Step-9: calculate the total marks.

Step-10: calculate percentage.

Step-11 : Determine division based on percentage.

Step-12: print roll number,name, subject marks,total marks, percentage and division.

Step-13: End the program.


CODING:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int rl,phy,chem,ca,total;

float per;

char nm[20],div[10];

clrscr();

printf(“Input the roll number of the student:”);

scanf(“%d”,&rl);

printf(“\ninput the name of the student:”);

scanf(“%s”,nm);

printf(\nInput the marks of physics,chemistry and computer application:”);

scanf(“%d %d %d”,&phy,&chem,&ca);

total=phy+chem+ca;

per=total/3.0;

if(per>=60)

strcpy(div,”First”);

else if(per<60 && per>=48)

strcpy(div,”Second”);

else if(per<48 && per>=48)

strcpy(div,”Pass”);

else
strcpy(div,”Fail”);

printf(“\nROLL NO.: %d\nName of the student: %s\n”,rl,nm);

printf(“\nMarks in physics: %d\nMarks in chemistry: %d\nMarks in Computer Application: %d\


n”,phy,chem,ca);

printf(“\nTotal Marks = %d\nPercentage = %5.2f\nDivision = %s\n”,total,per,div);

getch();

OUTPUT:

Input the roll number of the student: 30

Input the name of the student: venkatesh

Input the marks of physics,chemistry and computer application: 80 90 95

ROLL NO.:30

Name of the student: venkatesh

Marks in physics: 80

Marks in chemistry: 90

Marks in Computer Application: 95

Total Marks= 265

Percentage= 88.33

Division = First

RESULT:

Hence the expected output has been executed successfully.


Ex.no:9 ELSE IF LADDER

Date:

AIM:

To write a c program to determine temperature using if else ladder statement.

ALGORITHM:

Step-1: start the program

Step-2: The variable 'tmp' is declared to score the temperature input

Step-3: The program displays the prompt "Input days temperature"

Step-4: The scanf() function is used to read the user's input and store it in the variable 'tmp'

Step-5:The program uses a series of 'if' and 'else if' statements to determine the temperature range and
prints out the corresponding message for each range.

-If the temperature is less than 0, the program prints "Freezing weather"

-If the temperature is 0 and 10 , the program prints "Very cold weather"

-If temperature is 10 and 20, the program prints"Cold weather "

-If the temperature is 20 and 30, the program prints"Normal in temp"

-If the temperature is 30 and 40 ,the program prints"It's hot"

-If temperature is greater than or equal to 40 ,it prints"It's very hot"

Step-6: If none of the conditions are met , the program does not print any message

Step-7: End of program


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int tmp;

clrscr();

printf(“Input day’s Temperature:”);

scanf(“%d”,&tmp);

if(tmp<0)

printf(“\nFreezing weather.”);

else if(tmp<10)

printf(“\nVery cold weather.”);

else if(tmp<20)

printf(“\nCold weather.”);

else if(tmp<30)

printf(“\nNormal in temperature.”);

else if(tmp<40)

printf(“\nIts hot.”)

else

printf(“\nIts very hot.”);

getch();

OUTPUT:

Input days temperature : 24


Normal in temperature.

RESULT:

Hence the expected output has been executed successfully.


Ex.no:10 SWITCH STATEMENT

Date:

AIM:

To write a c program to take grade as input and providing a corresponding grade as output using switch
case statement.

ALGORITHM:

Step-1: Declare two variable `notes' as character array enough space to store two note.

Step-2: Declare a character variable called `grd' to store the entered grade.

Step-3: Prompt the user to input using printf and scanf

Step-4: Convert the entered grade to upper case using the toupper() function.

Step-5: use a switch statement to check the value of `grd' and assign the corresponding next to the
"notes" Array.

Step-6: Display the chosen note using print.

Step-7:End of the program.


CODING:

#include<stdio.h>

#include<ctype.h>

#include<string.h>

#include<conio.h>

void main()

char notes[15];

char grd;

clrscr();

printf(“Input the grade:”);

scanf(“%c”,&grd);

grd=toupper(grd);

switch(grd)

case’E’:

strcpy(notes,”Excellent”);

break;

case’V’:

strcpy(notes,”Very good”);

break;

case’G’:

strcpy(notes,”Good”);

break;

case’A’:
strcpy(notes,”Average”);

break;

case’F’:

strcpy(notes,”Fail”);

break;

default:

strcpy(notes,”Invalid Grade Found.\n”);

break;

Printf(“\nYou have chosen: %s\n”,notes);

getch();

OUTPUT:

Input the grade: E

You have chosen: Excellent

RESULT:

Hence the expected output has been executed successfully.


Ex.no:11 SUM OF DIGITS

Date:

AIM:

To write a c program to find the sum of individual of a positive integer.

ALGORITHM:

Steps:1 start the program

Step:2 Read the number n

Step:3 Initialize sum=0

Step:4 while (n%10)

Step:5 d=n℅10

Step:6 sum=sum+d

Step:7 n=n/10

Step:8 print sum

Step:9 End the program


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int n,d,sum=0;

clrscr();

printf(“Enter any integer:”);

scanf(“%d”,&n);

while(n>0)

d=n%10;

sum=sum+d;

n=n/10;

Printf(“\nSum of individual digit is %d.”,sum);

getch();

OUTPUT:

Enter any integer: 44

Sum of individual digit is 8.

RESULT:

Hence the expected output has been executed successfully.


Ex.no:12 FIBONACCI SEQUENCE

Date:

AIM:

To write a c program to generate the Fibonacci sequence based on the n number of terms.

ALGORITHM:

Step-1: start with two variable ‘a’ and ‘b' with value 0 and 1 respectively.

Step-2: prompt the user to enter the number of terms, let's call it as n.

Step-3: print the value of 'a' and ' b' as the first two terms of sequence.

Step-4: use loop to generate the remaining terms:

-set a third value 'c' to sum of 'a' and ' b'.

-Print the value of 'c'

-Update the value of 'a' and ' b' by assigning ' b' to 'a' and 'c' to 'b'

-Respect the loop upto it reaches terms.

Step-5: end the program.


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int a=0,b=1,c,n,I;

clrscr();

printf(“Enter no. of terms:”);

scanf(“%d”,&n);

printf(“\nThe Fibonacci sequence is “);

printf(“%d %d”,a,b);

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

c=a+b;

printf(“%d”,c);

a=b;

b=c;

getch();

OUTPUT:

Enter no. of terms: 7

The Fibonacci Sequence is 0112358

RESULT:

Hence the expected output has been executed successfully.


Ex.no:13 ONE DIMENSIONAL ARRAY

Date:

AIM:

To write a c program to find largest element in an using one-dimensional array.

ALGORITHM:

Step-1:Start and Declare an array

Step-2: Intialize the array with values

Step-3:Access element using indices

Step-4:Loop through the array

Step-5:Modify array length or use dynamic data structure

Step-6:Perform operations an array

Step-7:Handle errors and exceptions.


CODING:

#include<stdio.h>

#include<conio.h>

void main()

int a[30],i,num,largest;

clrscr();

printf(“Enter no. of elements:”);

scanf(“%d”,&num);

largest=a[0];

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

if(a[i]>largest)

largest=a[i];

printf(“\nLargest Element: %d”,largest);

getch();

OUTPUT:

Enter no. of Element: 4

54

34

100
45

Largest Element: 100

RESULT:

Hence the expected output has been executed successfully.


Ex..no:14 TWO DIMENSIONAL ARRAY

Date:

AIM:

To write a c program for matrix addition using two dimensional array.

ALGORITHM:

Step-1: Start the program

Step-2: Declare variable: mat1,mat2,mat3 of type matrix

Step-3: Read values of mat1 and mat2 from user

Step-4: For each element i,j in mat1 and mat2

a) Set mat 3[i][j]=mat1[i][j]+ mat 2[i][j]

Step-5: print mat3(matrix 3)

Step-6: End of the program.


CODING:

#include<stdio.h>

#include<conio.h>

int main()

int i,j,mat1[10][10],mat2[10][10],mat3[10][10];

int row1,col1,row2,col2;

clrscr();

printf(“\nEnter the number of rows of Mat1:”);

scanf(“%d”,&row1);

printf(“\nEnter the number of cols of Mat1:”);

scanf(“%d”,&col1);

printf(“\nEnter the number of rows of Mat2:”);

scanf(“%d”,&row2);

printf(“\nEnter the number of cols of Mat3:”);

scanf(“%d”,&col2);

if(row1 != row2 || col1 != col2)

printf(“\nOrder of two matrices is not same .”);

return 0;

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

for(j=0;j<col1;j++)

{
printf(“\nEnter te Element a[%d][%d]:”,i,j);

scanf(“%d”,&mat1[i][j]);

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

for(j=0;j<col2;j++)

printf(“\nEnter the Element b[%d][%d]:”,i,j);

scanf(“%d”,&mat2[i][j]);

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

for(j=0;j<col1;j++)

mat3[i][j]=mat1[i][j]+mat2[i][j];

printf(“\nThe addition of two matrices is:\n”);

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

for(j=0;j<jcol1;j++)

printf(“%d\t”,mat3[i][j]);
}

Printf(“\n”);

getch();

return 0;

OUTPUT:

Enter the number of rows of Mat1: 2

Enter the number of cols of Mat1: 2

Enter the number of rows of Mat2: 2

Enter the number of cols of Mat2: 2

Enter the Element a[0][0]: 2

Enter the Element a[0][1]: 3

Enter the Element a[1][0]: 4

Enter the Element a[1][1]: 5

Enter the Element b[0][0]: 2

Enter the Element b[0][1]: 4

Enter the Element b[1][0]: 2

Enter the Element b[1][1]: 2

The Addition of two matrices is:

4 7

6 7

RESULT:

Hence the expected output has been executed successfully.


Ex.no:15 STRING MANIPULATION-1

Date:

AIM:

To write a c program using concatenate,compare and length of the string functions.

ALGORITHM:

Step 1: start the program.

Step-2: press 1-concatenate,2-compare,3-length of string if press 1 then goto step 3,if press step 2 then
goto step 4,if press 3 then goto step 5.

Step 3: Read the string 1 and string 2 and comparison function by using strcmp built in function is used.
If res=0 the print both string are not equal and then goto the step 4.

Step-4: Read string 1 and string 2 and find concatenation of two string using string handling function.
strcat() and display string and return back to main function.

Step-5: Read string 1 and call function to find the length of string by calling function length.

Step-6: If digit=1 then goto step 2 else goto step7.

Step-7:End the program


CODING:

CODING:
#include<stdio.h>

#include<conio.h>

Void concat (char str1 [ ], char str2 [ ]);

Void compare (char str1 [ ], char str2 [ ]);

Void length (char str1 [ ]);

Void main ( )

Clrscr ( );

Int n , digit;

Char str1 [10], str2 [10];

do

Printf(“ press 1- concatenate 2-compare 3-length of string”);

Printf(“ \n enter your choice:”);

Scanf(“%d”,&n);

Switch(n)

Case 1: printf(“ enter first string:”);

Scanf(“%s”,str1);

Printf(“enter second string:”);

Scanf(“%s”,str2);

Concat ( str1 , str2 );

Break;

Case 2: printf(“ enter first string:”);


Scanf(“%s”,str1);

printf(“enter second string:”);

scanf(“%s”,str2);

Compare ( str1 , str2 );

Break;

Case 3:printf(“enter string:”);

scanf(“%s”,str1);

Length (str1);

Break;

Default: printf(‘ wrong choice:”);

Break;

Printf(“\n Do you want to continue (1/0)?”);

Scanf(“%d”,& digit);

} while (digit==0);

Void concat (char str1 [ ], char str2 [ ])

Strcat ( str1 , str2 );

Printf(“concatenate string :%s”, str1);

Void compare (char str1 [ ], str2 [ ])

Int i;

i=strcmp ( str1 ,str2 );


if(i==0)

printf(“strings are equal \n”);

else

printf(“ strings are not equal \n”);

Void length ( char str1 [ ])

int len;

len= strlen (str1);

printf(“ the length of string:%d”, len );

OUTPUT:

press 1- concatenate 2- compare 3- length of string

enter your choice : 1

enter first string :Welcome

enter second string: all

concatenate string :Welcome all

Do you want to continue (1/0) ? 1

OUTPUT:

Press 1- concatenate 2- compare 3- length of string

Enter your choice: 2

Enter first string : aaa

Enter second string :hhh

Strings are not equal

Do you want to continue (1/0)? 1


OUTPUT:

Press 1- concatenate 2- compare 3- length of string

Enter your choice: 3

Enter string : klnasc

Length of string : 6

Do you want to continue (1/0)? 0

RESULT:

Hence the expected output has been executed successfully.


Ex.no:16 STRING MANIPULATION-2

Date:

AIM:

To write a c program using copy.reverse,lowercase,uppercase string function.

ALGORITHM:

Step-1:start the program.

Step-2:Include the necessary header files 'stdio.h','conio.h,'string.h'.

Step-3:Define the main() function.

Step 4:Declare the variables:'n','digit','str 1','str 2'.

Step-5:Use a do while loop to repeat the process unit the user decides to stop.

Step-6:Display the menu option,read the user's choice and perform the designed operations based on
the choice using switch statement.

Step-7:Prompt the user if they want to continue,If yes repeat the process,If not exit the program.

Step-8:End the program.


CODING:

#include <stdio.h>

#include <conio.h>

#include <string.h>

Void main( )

int n, digit;

char str1[10],str2[10];

do

printf("press \n 1-copy \n 2-reverse \n 3-lowercase \n 4- uppercase");

printf("enter your choice:");

scanf("%d",&n);

switch (n)

Case 1:printf("enter string:");

scanf("%s",str1);

strcpy(str2,str1);

printf("string 2:%s\n",str2);

Break;

Case 2:printf("enter string:");

scanf("%s",str1);

strcpy(str2,strrev(str1));

printf("string 2:%s\n",str 2);

Break;
Case 3:printf("enter string :");

scanf("%s",str1);

strcpy(str2,strlwr(str1));

printf("string 2:%s\n",str2);

Break;

Case 4:printf("enter string:");

scanf("%s",str1);

strcpy(str2,strupr(str1));

printf("string 2:%s\n",str2);

Break;

Default :

printf("wrong choice");

Break;

printf("Do you want to continue (1/0)?");

scanf("%d",&digit);

}while(digit==0);

getch( );

OUTPUT:

press

1-copy

2-reverse

3-lowercase

4-uppercase
Enter your choice:1

enter string:harsh

String 2:harsh

RESULT:

Hence the expected output has been executed successfully.


Ex.no:17 USING FUNCTION

Date:

AIM:

To write a c program for interchange two integer values.

ALGORITHM:

Step-1: start the program

Step-2: declaring the swap Function with two parameter 'a' & 'b'

Step-3: inside the swap function create a temporary variable 'a' & 'b'

Step-4: assign the value of 'b' in 'a'

Step-5: assign the value of 'temp' to 'b'

Step-6: end the program


CODING:

#include<stdio.h>

#include<conio.h>

void swap(int,int);

void main()

Int i,j;

clrscr();

printf(“Enter i and j values:”);

scanf(“%d %d”,&i,&j);

printf(“Before swapping: %d %d\n”,i,j);

swap(i,j);

getch();

Void swap(int a,int b)

int temp;

temp=a;

a=b;

b=temp;

printf(“\nAfter swapping: %d %d”,a,b);

OUTPUT:

Enter i and j values:7 4

Before swapping:7 4
After swapping:4 7

RESULT:

Hence the expected output has been executed successfully.


Ex.no:18 FIND FACTORIAL USING RECURSION

Date:

AIM:

To write a c program to find a factorial using recursion.

ALGORITHM:

Step-1:Start the program.

Step-2:Declare and initialize necessary variables.

Step-3: Print a message about the unavailability of factorial for negative numbers.

Step-4:Prompt the user to enter a number.

Step-5:Check if the entered number is negative. If so, display an error message

Step-6:Calculate the factorial of the non-negative number.

Step-7:Display the result.

Step-8:End the program.


CODING:

#include<stdio.h>

#include<conio.h>

long factorial(int);

int main()

int num;

long fact;

clrscr();

printf("enter a number to find factorial=\n");

scanf("%d",num);

if(num<0)

printf("factorial of negative no is not defined\n");

else

fact=factorial(num);

printf("%d!=%d\n",num,fact);

getch();

return 0;

long factorial(int num)

if(num==0)

return 1;

else
return (num*factorial(num-1));

OUTPUT:

Enter a number to find factorial= 5

5!=120

RESULT:

Hence the expected output has been executed successfully.


Ex.no:19 STRUCTURE

Date:

AIM:

To write a c program using structure variable.

ALGORITHM :

Step-1:start the program

Step-2:Declare and initialize an array of stucture to hold student information.

Step-3:Input the information for each student using a loop from 0 to 3 inside the loop set the roll
number for each student prompt the user to enter the name.read the name using scanf prompt the
user to enter the marks read the marks using scanf.

Step-4:Display the information for each student using a loop from 0 to less than 3 inside the loop print
the roll number ,name,and mark for each student

Step-5:End of the program.


CODING:

#include <stdio.h>

#include<conio.h>

struct student

char name[30];

int roll;

int marks;

}s[3];

void main()

int i;

printf(" information of students:");

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

printf("Enter roll number: ");

scanf("%d", &s[i].roll);

printf("Enter name: ");

scanf("%s", s[i].name);

printf("Enter marks: ");

scanf("%d", &s[i].marks);

printf("Displaying information:\n");

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

{
printf("Roll number: %d\n", s[i].roll);

printf("Name:", s[i].name);

printf("Marks: %d", s[i].marks);

getch();

OUTPUT:

Information of students

Enter roll number: 1

Enter name: Pinky

Enter marks: 90

Enter roll number: 2

Enter name: ben

Enter marks: 98

Enter roll number: 3

Enter name: rose

Enter marks: 87

Displaying information

Roll number: 1

Name: pinky

Marks: 90

Roll number: 2

Name:ben

Marks: 98

Roll number: 3
Name:rose

Marks: 87

RESULT:

Hence the expected output has been executed successfully.


Ex.no:20 UNION

Date:

AIM:

To write a c program using union variable.

ALGORITHM:

step-1: start the program

step-2: Declare a union called'pack with members 'a','b',and'c'

step-3: Create a variable 'p' of type 'union pack'

step-4: print the occupied size of the union using 'size of (p)'

step-5: Assign a value "A" to members 'a' of the union 'p' and print its value

Step-6: Assign a value '10' to member 'b' of the union 'p' and print its value

Step-7: Assign a value '12345.6790' to member 'c' of the union 'p' and prints its value

Step-8: Return 0 to incidates successful execution


CODING:

#include <stdio.h>

#include<conio.h>

union pack

char a;

int b;

double c;

};

int main()

union pack p;

clrscr();

printf("\n Occupied size of union pack: ", sizeof(p));

p.a='A';

printf("\nValue of a: %c", p.a);

p.b=10;

printf("\nValue of b: %d", p.b);

p.c=12345.6790;

printf("Value of c: %lf\n", p.c);

p.a='A';

p.b=10;

p.c=12345.6790;

printf("\n value of a:%c,b:%d,c:%f",p.a,p.b,p.c);

getch();
}

OUTPUT:

Occupied size of union pack:8

Value of a:A

Value of b:10

Value of c:12345.6790

Value of a:A ,b: -1887,c:12345.679000

RESULT:

Hence the expected output has been executed successfully.


Ex.no:21 POINTERS

Date:

AIM:

To write a c program to find biggest among three number using pointers.

ALGORITHM:

Step 1: Start the program.

step-2: Declare variable

a, b and c.

Step 3: Declare pointer 'ptra', 'ptr b', 'ptrc' Assign them the addresses of visible 'a', 'b', 'L' .
Step-4: Compare the values pointed by three Pointer to find biggest number, if 'ptra' is greater than
both 'ptrb' and 'ptrc'. then it is The biggest number. if 'ptrb' is greater than both 'ptra'and 'ptrc'.then it is
the biggest number.other wise 'ptrc' is the biggest number

step-5:print the result using printf statement

step-6: End the program.


CODING:

#include <stdio.h>

#include <conio.h>

int main()

int a, b, c;

int *ptra=&a,*ptrb=&b,*ptrc=&c;

clrscr();

printf("Enter three values: ");

scanf("%d %d %d", ptra, ptrb, ptrc);

if ((*ptra>*ptrb&&*ptra>*ptrc))

printf("biggest number is: %d", *ptra);

else if ((*ptrb>*ptra&&*ptrb>*ptrc))

printf("biggest number is: %d", *ptrb);

else

printf("biggest number is: %d", *ptrc);

getch();

OUTPUT:

Enter three values: 56

98

78

biggest number is: 98

RESULT:

Hence the expected output has been executed successfully.


Ex.no:22 ARRAY OF POINTERS

Date:

AIM:

To write a c program to find the address of the values.

ALGORITHM:

Step-1:Start the program.

Step-2:Declare three integer variable ' x ', 'y ' , ' z ' and assign them values of 10,20,30 respectively.

Step-3:Assign three address of 'x', 'y', 'z' the respectively elements of the arrays.

Step-4:Start a loop from 0 to 2 (inclusive) print the value of array [ i ], the value pointer by array [ i ]
and the address stored in array [ i ].

Step-5: End of the program.


CODING:

#include<stdio.h>

#include<conio.h>

main()

clrscr();

int*array[3];

int x=10,y=20,z=30;

int i;

array[0]=&x;

array[1]=&y;

array[2]=&z;

printf("the value of %d=%d address is %u\t\n",*(array[i]),array[i]);

getch();

return 0;

OUTPUT:

The value of 10=-18 address is 874

The value of 20=-20 address is 874

The value of 30=-22 address is 874

RESULT:

Hence the expected output has been executed successfully.


Ex.no:23 FILE EVEN NUMBER USING FILE HANDLING FUNCTION

Date:

AIM:

To create an integer file and displaying even number.

ALGORITHM:

Step-1:start the program.

Step-2:declare a file-FILE*F1.

Step-3:open a file for writing integer fopen(“new”,”w”);

Step-4:enter integer one by one repeat steps upto enter-1.

Step-5:write the integeriln the file F1-putw(number);

Step-6:close the file F1.

Step-7:open the file reading integer-fopen(“new”,”r”) getw(number) and print number.

Step-8:close the file F1.

Step-9:open the file-fopen(“new”,”r”) read integer are one by one-getw(number).

Step-10:check if it is even or not-if (number %2==0)true.

Step-11:if even print the number.

Step-12:repeat the step-10 and 11 upto end of file.

Step-13:end.
CODING:

#include<stdio.h>

#include<conio.h>

void main()

int I,number;

char c;

FILE*F1;

clrscr();

printf(“Enter number and -1 for end:”);

F1=fopen(“new”,”w”);

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

scanf(“%d”,&number);

If(number==-1)

break;

putw(number,F1);

fclose(F1);

printf(“\nnumbmers in the given file are:”);

F1=fopen(“new”,”r”);

while((f1number=getw(F1))!=EOF)

printf9(“%5d”,number);

fclose(F1);

printf(“\nEven number in the given file are:”);


F1=fopen(“new”,”r”);

while((number=getw(F1))!=EOF)

if(number%2==0)

printf(“%5d”,number);

fclose(F1);

getch();

OUTPUT:

Enter number and -1 for end: 11 22 33 44 55 66 77 88 99 -1

Number in the given file: 11 22 33 44 55 66 77 88 99

Even number in the given file are: 22 44 66 88.

RESULT:

Hence the expected output has been executed successfully.


Ex.no:24 STUDENTS DETAILS USING FILE

Date:

AIM:

To create and process student mark list its using file algorithms.

ALGORITHM:

Step-1: start the program

Step-2: Declare a file -FILE*F1 and variable name, rollno, m1,m2,m3,m4,m5, total

Step-3: open a file for writing student details &open ("Data", "w")

Step-4: enter how many student and store it

Step-5: enter student details one by one using for loops

Step-6: store student details in name, rollno, m1,m2, m3,m4,m5 using scanf

Step-7: write student details in that file using fprintf()

Step-8: open a file for reading student details fopen(" Data", "r") ;

Step-9: get student details one by one using for loop and mark

Step-10: print the student name, rollno, marks and total mark

Step-11: check the individual subject mark if it below 35 print the result is " FAIL"

Step-12: else print the result is "PASS"

Step-13: Repeat the step 10 to 13 upto n student &close the file F1

Step-14: End the program


CODING:

#include <stdio.h>

#include <conio.h>

Void main( )

int i,n,rollno,m1,m2,m3,m4,m5,total;

Char name[20];

FILE*F1;

clrscr( );

F1=fopen("data","w");

printf("enter no of students:");

scanf("%d",&n);

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

printf("\n enter %d Student details ",i);

printf("\n enter name and roll no :");

scanf("%s%d",name, rollno:");

printf(F1"%s%d",name, rollno);

printf("\n enter five subject mark :);

scanf("%d%d%d%d%d",&m1, &m2, &m3,&m4, &m5);

fprintf(F1"%d%d%d%d%d",m1,m2,m3,m4,m5);

fclose(F1);

printf("\n");

F1=fopen("Data"," ");
for(i=1;i<=n; i++)

fscanf(F1"%s%d",&name, &rollno);

fscanf(F1"%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);

Total=m1+m2+m3+m4+m5;

printf("\n name :%s\t rollno:%d",name, rollno);

printf("\n");

printf(\n subject name \t\t mark);

printf("\n");

printf("\n subject 1 \t\t %d",m1);

printf("\n subject 2 \t\t %d",m2);

printf("\n subject 3 \t\t %d",m3);

printf("\n subject 4 \t\t %d",m4);

printf("\n subject 5 \t\t %d",m5);

printf("\n");

printf("\n total mark \t\t%d",total);

if(m1<35||m2<35||m3<35||m4<35||m5<35)

fprintf(stdout, "\n result-fail");

else

fprintf(stdout"\n result-pass");

printf ("\n");

getch( );

fclose (F1);

}
OUTPUT:

enter no of students :1

enter 1 student details

enter name and rollno:kumaran 6006

enter five subject mark:82 85 90 94 96

Name: venkat ROLLNO:6030

Subject Name Mark

Subject 1 82

Subject 2 85

Subject 3 90

Subject 4 94

Subject 5 96

Total : 447

Result – pass.

You might also like