Docsity

Programming in C Lab Manual for B.Sc.(CS) 1st Year - Prof. Prasad Acharya, Cheat Sheet of C programming

Partial preview of the text

Download Programming in C Lab Manual for B.Sc.(CS) 1st Year - Prof. Prasad Acharya and more Cheat Sheet C programming in PDF only on Docsity!

GDC MEMORIAL COLLEGE

BAHAL (BHIWANI)-

Lab Manual

PROGRAMMING IN C (B.Sc.(cs) 1

st

year)

Department of Computer Science

B.SC.(C. SC.) SEM II

LIST OF PROGRAMS

Program 1: Temperature of a city in Fahrenheit degrees is input through keyboard. Write a

program to convert this temperature into Centigrade degrees.

Program 2:WAP to calculate Simple Interest and Compound Interest.

Program 3: WAP to find largest number among three numbers.

Program 4: WAP to count number of digits, reverse the digit and sum of digits of a given

number.

Program 5:WAP to find factorial of a number.

Program 6: WAP to check whether a given number is prime or not.

Program 7: WAP to check whether a given year is leap year or not.

Program 8: WAP to search for a given number in an array.

Program 9: WAP to find the smallest and largest element in an array.

Program 10: WAP to add and multiply two matrices.

Program 11: WAP to swap two numbers using call by value and call by reference.

Program 12: WAP to count number of words, characters and blanks in a string.

Program 13: WAP to illustrate the usage of structure in processing of student record.

Program 14: WAP to illustrate encryption/decryption of a string.

Program 15: WAP to illustrate pointer arithmetic.

printf("\n Enter the principal amount, rate and time: "); scanf("\n %d%f%d",&pa,&r,&t); si=part/100; si=s_i(pa,t,r); printf("\n Simple interest:%f",si); ci=c_i(pa,t,r); ci=ci-pa; printf("\n Compound interest:%f",ci); getch(); } float s_i(int p,int t,float r) { float temp; temp=(prt)/100; return temp; } float c_i(int p,int t,float r) { float temp; temp=p*pow((1+r/100),t); return temp; }

/Program #3: WAP to find largest number among three numbers./

#include<stdio.h>

#include<conio.h> void main() { int a,b,c,largest; clrscr(); printf("\n Enter three numbers:"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) largest=a; else largest=c; } else { if(b>c) largest=b; else largest=c; } printf("\n Largest among three numbers is :%d",largest); getch(); }

/* Program #4: WAP to count number of digits, reverse the digit and sum of digits of a

given number. */

return(count); } int rev_no(int n) { int rev=0,temp; while(n!=0) { temp=n%10; rev=rev*10+temp; n=n/10; } return(rev); } int s_o_d(int n) { int sum=0, temp; while(n!=0) { temp=n%10; sum=sum+temp; n=n/10; } return(sum); }

/* Program #5:WAP to find factorial of a number. */

#include<stdio.h> #include<conio.h> int factorial(int); void main() { int f,n; clrscr(); printf("\n Enter a number:"); scanf("%d",&n); f=factorial(n); printf("\n Factorial of given no. %d is %d",n,f); getch(); } int factorial (int m) { if(m==1) return 1; return(m*factorial(m-1)); }

/* Program #6: WAP to check whether a given number is prime or not. */

#include<stdio.h> #include<conio.h> #include<math.h> void main() { int div,n;

scanf("%d",& yr); if(yr%100==0) { if(yr%400==0) printf("Given year is leap year"); else printf("Given year is not a leap year "); } else { if(yr%4==0) printf("Given year is leap year "); } getch(); }

/* Program #8: WAP to search for a given number in an array.*/

#include<conio.h> #include<stdio.h> void main() { int a[30],no,i,n; clrscr(); printf("\n Enter the number of elements in the array:"); scanf("%d",&n); printf("\n Enter the array elements"); for(i=0;i<n;i++)

scanf("%d",&a[i]); printf("\n Enter the element to be searched"); scanf("%d",&no); for(i=0;i<n;i++) { if(a[i]==no) { printf("\n Element found at location %d",i+1); goto end; } else { printf("\n Element not found"); goto end; } } end: getch(); }

/* Program #9: WAP to find the smallest and largest element in an array. */

#include<stdio.h> #include<conio.h> void main() { int a[30],i,min,max,n; clrscr();

printf("\n Enter the order of first matrix:"); scanf("%d%d",&m,&n); printf("\n Enter the order of second matrix:"); scanf("%d%d",&p,&q); if(n!=p) { printf("\n Matrix multiplication is not possible"); goto end; } printf("\n Enter the elements of first matrix: "); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%f",&a[i][j]); } printf("\n Enter the elements of second matrix: "); for(i=0;i<p;i++) { for(j=0;j<q;j++) scanf("%f",&b[i][j]); } for(i=0;i<m;i++) { for(j=0;j<q;j++) { c[i][j]=0;

for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } printf("\n Resultant matrix is\n "); for(i=0;i<m;i++) { for(j=0;j<q;j++) printf("%f\t",c[i][j]); printf("\n"); } end: getch(); }

/* Program #11: WAP to swap two numbers using call by value and call by reference.*/

#include<stdio.h> #include<conio.h> void swap(int,int); void swap_ref(int,int); void main() { int a,b; clrscr(); printf("\n Enter the numbers:"); scanf("%d%d",&a,&b); printf("\n Swapping of numbers using call by values");

#include<string.h> void main() { char string[100]; int c,noc=0,noblanks=0,i,len=0,nowords=0; clrscr(); printf("\n Enter text :"); gets(string); puts(string); len=strlen(string); for(i=0;i<len;i++) noc++; for(i=0;i<len;i++) { if(string[i]=='\t'||string[i]==' '||string[i]=='\n') noblanks++; } for(i=0;i<len;i++) { if(string[i]==' ') { nowords++; } } noc=noc-noblanks;

printf("\nNo. of characters %d", noc); printf("\n No. of blanks: %d",noblanks); printf("\nNo. of words:%d",nowords+1); getch(); }

/* Program #13: WAP to illustrate the usage of structure in processing of student record.*/

#include<stdio.h> #include<conio.h> void main() { struct student { int roll_no; char name[30]; float marks; }; struct student s; clrscr(); printf("\n Enter the name, roll no., marks of student: "); scanf("%s%d%f",&s.name,&s.roll_no,&s.marks); printf("\n The details of student :"); printf("\n Name:%s",s.name); printf("\n Roll No.:%d",s.roll_no); printf("\n Marks:%f",s.marks); getch(); }

jump1: i++; } printf("The encrypted text is:"); puts(String); decryption(string); } void decryption(char*string) { int i=0; while(string[i]!='\0') { if(string[i]==' ') goto jump2; else if((string[i] >= 68 && string[i] <= 90) || \ (string[i] >= 100 && string[i] <= 122)) string[i]-=3; else string[i]+=23; jump2: i++; } printf("The decrypted text is:"); puts(string); }

/* Program #15: WAP to illustrate pointer arithmetic.*/

#include<stdio.h>

#include<conio.h> void main() { int i,x; float j,y; char k='a',*z; clrscr(); printf("\n Enter the values of i, j: "); scanf("%d%f",&i,&j); printf("\n Value of i=%d",i); printf("\n Value of j=%f",j); printf("\n Value of k=%c",k); x=&i; y=&j; z=&k; printf("\n Original address in x=%u",x); printf("\n Original address in y=%u",y); printf("\n Original address in z=%u",z); x++; y++; z++; printf(ā€œ\n After incrementing x, y, z:\nā€) printf("\n New address in x=%u",x); printf("\n New address in y=%u",y); printf("\n New address in z=%u",z); x=x-7;