Lab manual c programming, Summaries of C programming

Noyckvvkfdmdvkdmvfkffmbcmmkgmddmckdmfmdmcmfkmfmdjeckjfkd

Typology: Summaries

2022/2023

Uploaded on 07/27/2023

siva-guna
siva-guna 🇮🇳

1 document

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program :
# include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
char ch;
printf(“Please enter any character:”);
ch=getchar();
if(isalpha(ch))
printf(“/n Alphabet”);
else if(isdigit(ch))
printf(“/n Number”);
else
Printf(“\n Special character”);
getch();
return(0);
}
OUTPUT:
Please enter any characters : 4Number
Please enter any characters : aAlphabet
Please enter any characters : “Special character
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Lab manual c programming and more Summaries C programming in PDF only on Docsity!

Program :

include

#include #include int main() { char ch; printf(“Please enter any character:”); ch=getchar(); if(isalpha(ch)) printf(“/n Alphabet”); else if(isdigit(ch)) printf(“/n Number”); else Printf(“\n Special character”); getch(); return(0); } OUTPUT: Please enter any characters : 4Number Please enter any characters : aAlphabet Please enter any characters : “Special character

Program : #include int main() { float fh,cl; int choice; printf("\n1: Convert temperature from Fahrenheit toCelsius."); printf("\n2: Convert temperature from Celsius toFahrenheit."); printf("\nEnter your choice (1, 2): "); scanf("%d",&choice); if (choice ==1) { printf("\nEnter temperature in Fahrenheit: "); scanf("%f",&fh); cl=(fh-32)/1.8; printf("Temperature in Celsius: %.2f",cl); } elseif(choice==2) { printf("\nEnter temperature in Celsius: "); scanf("%f",&cl); fh= (cl*1.8)+32; printf("Temperature in Fahrenheit: %.2f",fh); } else { printf("\nInvalid Choice !!!"); } return 0; Output : First Run: 1: Convert temperature from Fahrenheit to Celsius. 2: Convert temperature fromCelsius to Fahrenheit. Enter your choice (1, 2): 1 Enter temperature in Fahrenheit: 98.6Temperature in Celsius: 37. Second Run: 1: Convert temperature from Fahrenheit to Celsius. 2: Convert temperature from Celsius to Fahrenheit. Enter your choice (1, 2): 2 Enter temperature in Celsius: 37.0Temperature in Fahrenheit: 98.

Program: #include int main() { int a, b, c, largest; printf("Please Enter three different values\n"); scanf("%d %d %d", &a, &b, &c); largest =((a>b && a>c)?a: (b>c)?b:c); printf("\nLargest : %d", largest); return 0; } Output: Please Enter three different values 9 26 Largest: 56

Program : #include void main() { int num, rem; printf("Enter the number : "); scanf("%d", &num); rem = num % 2;if (rem! == 0) printf("The entered number %d is an even integer\n", num); else printf("The entered number %d is an odd integer\n", num); } Output : Enter the number: 15 The entered number 15 is odd integerEnter the number: 22 The entered number 22 is an even integer

Program : #include int main() { int number; printf(“Enter any integer number”); scanf(“%d”,&number); switch (number) { case 1: case 2: case 3: printf("One, Two, or Three.\n"); break; case 4: case 5: case 6: printf("Four, Five, or Six.\n"); break; default: printf("Greater than Six.\n"); } } Output : Enter any integer number 5Four, Five, or Six.

Program : #include int main() { int i; double number,sum=0.0;for(i=1;i<=5;i++) { printf(“Enter value of n:%d”,i);scanf(“%lf”,&number); //if the user enter a negative number, break the loopif(number<0.0) { break; } sum+=number; } printf(“sum=%2lf”,sum);getch(); return 0; } Output : Enter value of n :5 1 2 - 3 Sum = 3

(i) Looping statements – for loopProgram : #include int main() { inti, num; printf("Enter number to print table: "); scanf("%d", &num); for(i=1; i<=10; i++) { printf("%d * %d = %d\n", num, i, (num*i)); } return 0; } Output : Enter number to print table: 77 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5= 7 * 6= 7 * 7= 7 * 8= 7 * 9= 7 * 10=

(ii) Looping statements – while loopProgram : #include int main() { int num,digit,sum,temp; sum=0; printf("Enter a number:"); scanf("%d",&num); temp=num; while(temp!=0) { digit=temp%10; sum=sum+digit; temp=temp/10; } printf("Sum of digits of %d is %d",num,sum); return 0; } Output: Enter a number :654Sum of digits is=15 Enter a number :123Sum of digits is=

One dimensional ArrayProgram : #include void main() { inti, j, a, n, number[30]; printf("Enter the value of N \n"); scanf("%d", &n); printf("Enter the numbers \n"); for (i = 0; i< n; ++i) scanf("%d", &number[i]); for (i = 0; i< n; ++i) { for (j = i + 1; j < n; ++j) { if (number[i] > number[j]) { a = number[i]; number[i] = number[j]; number[j] = a; } } } printf("The numbers arranged in ascending order aregiven below \n"); for (i = 0; i< n; ++i) printf("%d\n", number[i]); } Output : Enter the value of N Enter the numbers 78 90 456 780 200 The numbers arranged in ascending order are givenbelow 3 78 90 200 456 780

Two dimensional arrayProgram: #include #include int main() { int a[10][10],b[10][10],c[10][10],m,n,r,s,i,j,k; clrscr(); printf("enter the number of rows and columns of I matrix :"); scanf("%d %d",&m,&n); printf("enter the number of rows and columns of II matrix"); scanf("%d%d”,&r,&s); if(n!=s) printf(“matrix multiplication is not possible”); else { printf("enter the first matrix element=\n"); for(i=0;i Program: #include int main() { int arr[2][3][3]; int tables,rows,cols; printf("enter the values in the array: \n"); for(tables=1;tables<=2;tables++) { for(rows=1;rows<=3;rows++) { for(cols=1;cols<=3;cols++) { printf(“the value at arr[%d][%d][%d]:”,tables,rows,cols); scanf (“%d”,&arr[tables][rows][cols]); }}} printf(“printing the values in array:\n”); for(i=1;i<=2;i++) { for(j=1;j<=3;j++) { for(k=1;k<=3;k++) { printf("%d ",arr[tables][rows][cols]); if(k==3) { printf("\n"); } } } printf("\n"); } return 0; }

OUTPUT:

enter the values in the array:the value at arr[1][1][1]: 23 the value at arr[1][1][2]: the value at arr[1][1][3]: 45 the value at arr[1][2][1]: 76 the value at arr[1][2][2]: 78 the value at arr[1][2][3]: 98 the value at arr[1][3][1]: 87 the value at arr[1][3][2]: 67 the value at arr[1][3][3]: 98 the value at arr[2][1][1]: 34 the value at arr[2][1][2]: 23 the value at arr[2][1][3]: 67 the value at arr[2][2][1]: 58 the value at arr[2][2][2]: 19 the value at arr[2][2][3]: the value at arr[2][3][1]: 39 the value at arr[2][3][2]: 82 the value at arr[2][3][3]: 44 printing the values in array: 23 34 45 76 78 98 87 67 98 34 23 67 58 19 84 39 82 44

Program : #include #includeint main() { int length; char s[20] = “We are Here”; char src[20]= “ Destination”;char dest[20]= “”; char src[20]= “ before”;char dest[20]= “after ”; char str1[]=”copy”; char str2[]=”Trophy”; char str[]=”CONVERT me To the Lower Case”; length=strlen(s); printf(“\Length of the string is = %d \n”, length); //strcpy printf(“\n source string is = %s”, src); printf(“\n destination string is = %s”, dest); strcpy(dest, src); printf (“\ntarget string after strcpy() = %s”, dest); //Strcat strcat(dest, src); printf(“\n Concatnated string is:”); puts(dest); //Strcmpint i,j,k; printf(“\nString comparision”); i=strcmp(str1, “copy”); j=strcmp(str1, str2); k=strcmp(str1, “f”); printf(“\n %d %d %d”,i,j,k); //strlwr printf(“%s\n”, strlwr(str)); } Output: Length of the string is = 11 Source string is = DestinationTarget string is = Target string after strcpy() = DestinationConcatinated string is: The output will be: after beforeString comparision 0 - 1 convert me to the lower case

Call by referenceProgram #include 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); // printing the value of a and b in mainswap(&a,&b); printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call byreference, a = 10, b = 20 } void swap (int *a, int *b) { int temp; temp = a; a=b; b=temp; printf("After swapping values in function a = %d, b =%d\n",a,b); // Formal parameters, a = 20, b = 10 } OUTPUT: Before swapping the values in main a =10 ,b= After swapping values in main a = 20,b= After swapping values in main a = 20, b = 10