C Class Assignments
C Class Assignments
Lecture No. 1
Assignment No. 1
Objective Write a program to display Hello world. Command to be used 1. printf Output:
Lecture No. 1
Assignment No.2
Objective Write a program to demonstrate the use of variable, data types and constants Command to be used 1. Variable Output:
#include<stdio.h> #include<conio.h> void main() { int i=10; float money=55.5; char ch=S; double dbl=2500000; printf(Integer value is:= %d, i ); printf(\nYour money is:= %f, money ); printf(\nYour Money is: %.2f , money); printf(\nCharacter value is:= %c, ch); printf(\nDouble value is:= %lf, dbl); getch(); }
Lecture No. 1
Assignment No.3
Objective Write a program to demonstrate the use of escape characters Command to be used 1. Escape characters Output:
#include<stdio.h> #include<conio.h> void main() { printf(First Line. \n New Line.); println(\nBackspaces\b); printf(Alarm \a); printf(\tTab); printf( \'Single\' Quote); printf( \Double\ Quote); printf( It is symbol of backslash \\); getch(); }
Lecture No. 2
Assignment No. 1
Objective Write a program to demonstrate use of arithmetic operators. Command to be used 1. Arithmetic operators Output
#include<stdio.h> #include<conio.h> void main() { int x=50, y= 60; printf(Addition of x and y is = %d, (x+y) ); printf(Subtraction of x and y is = %d, (x-y) ); printf(Division of y and x is = %d, (y/x) ); printf(Modulus of y and x is = %d, (y%x) ); getch(); }
Lecture No. 2
Assignment No. 2
Objective Write a program to demonstrate use of Logical Operator Commands to be used 1. Logical operators Output
#include<stdio.h> #include<conio.h> void main() { int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) printf(value1 is 1 AND value2 is 2); if((value1 == 1) || (value2 == 1)) println(value1 is 1 OR value2 is 1); if((value1 != 3) && (value2 != 3)) printf(value1 and value2 is not equal 3 ); getch(); }
Lecture No. 2
Assignment No. 3
Objective Write a program to demonstrate use of Relational Operators Commands to be used 1. Relational operators Output
#include<stdio.h> #include<conio.h> void main() { int value1 = 1; int value2 = 2; if(value1 == value2) printf(value1 == value2); if(value1 != value2) printf(value1 != value2); if(value1 > value2) printf(value1 > value2); if(value1 < value2) printf(value1 < value2); if(value1 <= value2) printf(value1 <= value2); getch(); }
Lecture No. 2
Assignment No. 4
Objective Write a program to demonstrate use of Pre/post increment/decrement operators Commands to be used 1. Expression operators Output
#include <stdio.h> int main(void) { int count = 0, loop; loop = ++count; // same as count = count + 1; loop = count printf(loop = %d, count = %d\n, loop, count); loop = count++; // same as loop = count; count = count + 1 printf(loop = %d, count = %d\n, loop, count); return 0; }
Lecture No. 3
Assignment No.1
Objectives Write a program to accept a number and find whether it is an odd number or even Commands to be used 1. if else Output
#include<stdio.h> #include<conio.h> //program to find odd and even number void main () { int a; clrscr(); printf(Enter a number\n); scanf(%d,&a); if(a%2==0) { printf(\n%d is a even number,a); } else { printf(\n%d is a odd number,a); } getch(); }
Lecture No. 3
Assignment No.2
Objective Write program to accept score in test and find the grade according to the below given conditions Score grade ---------------------------->= 90 A+ >=70 A >=60 B+ >=50 B >=35 C Commands to be used 1. nested if else Output:
// Program to calculate grade #include <stdio.h> #include <conio.h> void main() { int score; clrscr(); printf(Enter your score\n); scanf(%d,&score); if(score>=90) { printf(you got grade A+); } else if(score>=70) { printf(you got grade A); } else if(score>=60) { printf(you got grade B+); 10
} else if(score>=50) { printf(you got grade B); } else if(score>=35) { printf(you got grade C); } else { printf(you failed!); } getch(); }
11
Lecture No. 3
Assignment No.3
Objective Write a program to accept a month in number and display name of month Commands to be used 1. switch case Output
#include<conio.h> #include<stdio.h> void main() { int month; clrscr(); printf(Enter any month in number\n); scanf(%d,&month); switch(month) { case 1: printf(January); break; case 2: printf(February); break; case 3: printf(March); break; case 4: printf(April); break; case 5: printf(May); break; case 6: printf(June); break; case 7: printf(July); break; case 8: printf(August); break; case 9: printf(September); break; case 10: printf(October); break; case 11: printf(November); break; case 12: printf(December); break; default: printf(Invalid month.);break; } getch(); }
12
Lecture No. 4
Assignment No. 1
Objective Write a program to print number from 1 to 5 Commands to be used 1. for loop Output
13
#include <stdio.h> #include <conio.h> //program to print triangle void main() { int i, j; clrscr(); for(i = 1; i <= 10; i = i + 1) { for(j = 1; j <= i; j = j + 1) { printf(*); } printf(\n); } getch(); }
14
#include<stdio.h> #include<conio.h> /*sumation of n numbers using while loop*/ void main() { int no,sum=0; char choice='y'; clrscr(); while (choice=='y')//while choice is yes { printf(\nEnter the number to add\n); scanf(%d%,&no); sum=sum+no; printf(Do want to add more numbers?[y/n]); choice=getch(); } printf(\nSum of numbers is %d,no); getch(); }
15
#include<stdio.h> #include<conio.h> /*sumation of n numbers using while loop*/ void main() { int no,sum=0; char choice; clrscr(); do { printf(\nEnter the number to add\n); scanf(%d%,&no); sum=sum+no; printf(Do want to add more numbers?[y/n]); choice=getch(); }while (choice=='y');//while choice is yes printf(\nSum of numbers is %d,no); getch(); } 16
#include<stdio.h> #include<conio.h> void main() { int no,sum=0; char choice; clrscr(); top: printf(\nEnter the number to add\n); scanf(%d%,&no); sum=sum+no; printf(Do want to add more numbers?[y/n]); if(getch()=='y') { goto top; } printf(\nSum of numbers is %d,no); getch(); }
17
#include <stdio.h> #include <conio.h> /* program to find prime */ void main() { int number,i; char isPrime='y'; clrscr(); printf(Enter a number to check whether it is prime or not?\n); scanf(%d%,&number); for(i=2;i<number;i++) { if(number%i==0) { isPrime='n'; break; } } if(isPrime=='y') { printf(It is a prime number); } 18
19
Lecture No. 6
Assignment No.1
Objective Write a program to calculate the area of a circle, define PI as constant using preprocessor Command to be used 1. #define Output
#include<stdio.h> #include<conio.h> #undef PI #define PI 3.14 #define P printf #define S scanf void main() { int R; clrscr(); P(Enter a Radius of a Circle: ); S(%d,&R); P(Area of a Circle is: %f, PI*R*R); getch(); }
20
Lecture No. 6
Assignment No.2
Objective Write program to demonstrate the use of #if, #else and #endif Command to be used 1. #if 2. #else 3. #endif Output
#include<conio.h> #include<stdio.h> int main(void) { #if 1 printf(Yabba Dabba Do!\n); #else printf(Zip-Bang!\n); #endif getch(); return 0; }
21
Lecture No. 7
Assignment No.1
Objective Write a program to store the Hello in char array and display it. Command to be used 1. Array Output
#include<stdio.h> #include<conio.h> void main() { char word[20]; word[0] = 'H'; word[1] = 'e'; word[2] = 'l'; word[3] = 'l'; word[4] = 'o'; word[5] = 0; printf(The contents of word[] is -->%s\n, word ); getch(); }
22
Lecture No. 7
Assignment No.2
Objective Write a program to store the values in a 3, 4 matrix using two dimensional array and display it Command to be used 1. Array (two dimensional) Output
#include <stdio.h> #include <conio.h> /* program to display a matrix */ void main() { int matrix[4][3], i, j; matrix[0][0]=5; matrix[0][1]=4; matrix[0][2]=2; matrix[1][0]=15; matrix[1][1]=51; matrix[1][2]=53; matrix[2][0]=58; matrix[2][1]=51; matrix[2][2]=59; matrix[3][0]=25; matrix[3][1]=35; matrix[3][2]=14; clrscr(); 23
24
Lecture No. 8
Assignment No.1
Objective Write a program to sort an arrays using bubble sorting method Command to be used 1. Arrays 2. for loop Output
#include <stdlib.h> #include <stdio.h> #define NUM_ITEMS 20 void bubbleSort(int numbers[], int array_size); int numbers[NUM_ITEMS];
void main() 25
{ int i; clrscr(); //fill array with random integers for (i = 0; i < NUM_ITEMS; i++) numbers[i] = rand(); //perform bubble sort on array bubbleSort(numbers, NUM_ITEMS); printf(Done with sort.\n); for (i = 0; i < NUM_ITEMS; i++) printf(%i\n, numbers[i]); getch(); }
void bubbleSort(int numbers[], int array_size) { int i, j, temp; for (i = (array_size - 1); i >= 0; i--) { for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; } } } }
26
Lecture No. 8
Assignment No.2
Objective Write a program to sort an arrays using selection sorting method Command to be used 1. Arrays 2. for loop Output
#include <stdlib.h> #include <stdio.h> #define NUM_ITEMS 20 void selectionSort(int numbers[], int array_size); int numbers[NUM_ITEMS];
27
int main() { int i; //fill array with random integers for (i = 0; i < NUM_ITEMS; i++) { numbers[i] = rand(); } clrscr(); //perform selection sort on array selectionSort(numbers, NUM_ITEMS); printf(Done with sort.\n); for (i = 0; i < NUM_ITEMS; i++) { printf(%i\n, numbers[i]); } getch(); } void selectionSort(int numbers[], int array_size) { int i, j; int min, temp; for (i = 0; i < array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] < numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } }
28
Lecture No. 8
Assignment No.3
Objective Write a program to sort an arrays using selection quick method Command to be used 1. Arrays 2. for loop Output
#include <stdlib.h> #include <stdio.h> #define NUM_ITEMS 20 void quickSort(int numbers[], int array_size); void q_sort(int numbers[], int left, int right); int numbers[NUM_ITEMS];
29
int main() { int i; clrscr(); //fill array with random integers for (i = 0; i < NUM_ITEMS; i++) numbers[i] = rand(); //perform quick sort on array quickSort(numbers, NUM_ITEMS); printf(Done with sort.\n); for (i = 0; i < NUM_ITEMS; i++) printf(%i\n, numbers[i]); getch(); } void quickSort(int numbers[], int array_size) { q_sort(numbers, 0, array_size - 1); }
void q_sort(int numbers[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; if (left != right) { numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = l_hold; 30
right = r_hold; if (left < pivot) q_sort(numbers, left, pivot-1); if (right > pivot) q_sort(numbers, pivot+1, right); }
31
Lecture No. 9
Assignment No.1
Objective Write a program to add two 3X3 matrices. Command to be used 1. Two dimensional Array Output
/* ** Illustrates how to add two 3X3 matrices. */ #include <stdio.h> #include <conio.h> void add_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) { int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; clrscr(); 32
add_matrices(p, q, r); printf(\nMatrix 1:\n); print_matrix(p); printf(\nMatrix 2:\n); print_matrix(q); printf(\nResult:\n); print_matrix(r); getch(); } void add_matrices(int a[][3], int b[][3], int result[][3]) { int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { result[i][j] = a[i][j] + b[i][j]; } } } void print_matrix(int a[][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf(%d\t, a[i][j]); } printf(\n); } }
33
Lecture No. 9
Assignment No. 2
Objective Write a program to perform multiplication of two matrices of any size Command to be used 1. Arrays Output
#include<stdio.h> #include<conio.h> #define max 10 int main() { int a[max][max]; int a1[max][max]; int a2[max][max]; int i,j,n,m,k,l,e; clrscr(); printf(Enter the size of first matrix\n); scanf(%d%d,&n,&m); printf(\nEnter the elements of first matrix\n); for(i=0;i<n;i++) 34
for(j=0;j<m;j++) scanf(%d,&a[i][j]); printf(\nEnter the size of second matrix\n); scanf(%d%d,&k,&l); printf(\nEnter the elements of second matrix\n); for(i=0;i<k;i++) for(j=0;j<l;j++) scanf(%d,&a1[i][j]); printf( First Matrix you entered:\n); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf(%d ,a[i][j]); printf(\n); } printf( Second Matrix you entered:\n); for(i=0;i<k;i++) { for(j=0;j<l;j++) printf(%d ,a1[i][j]); printf(\n); } //Multiplication of these two matrix for(i=0;i<n;i++) { for(j=0;j<k;j++) { a2[i][j]=0; for(e=0;e<k;e++) { a2[i][j]=a2[i][j] + (a[i][e] * a1[e][j]); } } } printf( Multiplication of matrix is:\n); for(i=0;i<n;i++) { for(j=0;j<l;j++) printf(%d ,a2[i][j]); printf(\n); } getch(); }
35
Lecture No. 10
Assignment No. 1
Objective This session demonstrates about structure. Command to be used 1. Structure Output
#include<stdio.h> #include<conio.h> struct my_struct { int x,y; }; struct another_struct { char first_name[20], last_name[20]; }as1; void main() { struct my_struct ms; clrscr(); printf(Enter first number: ); scanf(%d,&ms.x); printf(\nEnter second number: ); scanf(%d,&ms.y); printf(\nAddition of two number is: %d, (ms.x + ms.y)); printf(\nEnter your First name: ); scanf(%s,as1.first_name); printf(\nEnter your Last name: ); 36
37
Lecture No. 10
Assignment No. 2
Objective Write a program to demonstrate the use of nested structures Command to be used 1. Nested Structures Output
#include<stdio.h> #include<conio.h> struct my_struct { int x,y; }; struct another_struct { struct my_struct ms1; char first_name[20], last_name; }as1; void main() { clrscr(); printf(Enter first number: ); scanf(%d,&as1.ms1.x); printf(\nEnter second number: ); scanf(%d,&as1.ms1.y); printf(\nAddition of two number is: %d, (as1.ms.x + as1.ms.y)); printf(Enter your First name: ); scanf(%s,as1.first_name); printf(\nEnter your Last name: ); scanf(%s,as1.last_name); printf(\nYour name is: %s %s,as1.first_name, as1.last_name); getch(); }
38
Lecture No. 11
Assignment No. 1
Objective Write a program to demonstrate the use of union Command to be used 1. Union Output
#include<stdio.h> #include<conio.h> void main() { union my_union { int i; char ch[2]; }; union my_union key; clrscr(); key.i=512; printf(\nKey.i=%d,key.i); printf(\nKey.ch[0]=%d,key.ch[0]); printf(\nKey.ch[1]=%d,key.ch[1]); getch(); }
39
Lecture No. 11
Assignment No. 2
Objective Write a program to demonstrate the use of Enumeration Command to be used 1. Enumeration Output
#include<conio.h> #include<stdio.h> void main() { /* Define a list of aliases */ enum months {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}; enum days {jan=31, feb=28, mar=31, apr=30, may=31, jun=30, jul=31, aug=31, sep=30, oct=31, nov=30, dec=31}; enum days day; enum months month; /* define 'month' variable of type 'months' */ clrscr(); //Assign integer value via an alias This will return a 9 printf(Your Month is: %d\n, month=Sep); printf(\nDays of February: %d, day=Feb); getch(); }
40
Lecture No. 12
Assignment No.1
Objective Write the program to display the length of the string, display it in upper case & lower case. Also display result by concatenating the string in lower case and upper case. Command to be used 1. String Functions Output
This program demonstrate how to use some predefine string function of C. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char up[20]=MUMBAI; char low[20]=mumbai; char *len=Borland International; clrscr(); printf(\nLower case to Upper case: %s, strupr(low)); printf(\nUpper case to Lower: %s, strlwr(up)); printf(\nAppends string: %s, strcat(up,low)); printf(\nLength of Mumbai: %d, strlen(len)); getch(); }
41
Lecture No. 12
Assignment No.2
Objective Write the program to accept two strings from user and check whether two strings are same or not, also display the difference between the strings Command to be used 1. String Functions Output
Program
#include<stdio.h> #include<conio.h> #include<string.h> #define p printf #define s scanf //string functions void main() { char s1[10],s2[10]; clrscr(); p("enter 1st string\n"); s("%s",&s1); p("enter 2nd string\n"); s("%s",&s2); if(strcmp(s1,s2)==0) //strncmp(s1,s2,3)->compares first 3 characters { p("\nboth strings are equal"); } else
42
//strncmp(s1,s2,3)
43
Lecture No. 13
Assignment No.1
Objective This session demonstrate the simple print system Date Command to be used 1. Date functions Output
#include <dos.h> #include <stdio.h> #include<conio.h> int main(void) { struct date d; clrscr(); getdate(&d); printf(The current year is: %d\n, d.da_year); printf(The current day is: %d\n, d.da_day); printf(The current month is: %d\n, d.da_mon); getch(); return 0; }
44
Lecture No. 13
Assignment No.2
Objective This session demonstrate the simple print system Time. Command to be used 1. Time functions Output
#include <time.h> #include <stdio.h> #include <dos.h> #include<conio.h> int main(void) { struct time t; clock_t start, end; time_t tm, first, second; clrscr(); gettime(&t); printf(The current time is: %2d:%02d:%02d.%02d\n, t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); time(&tm); printf(Today's date and time: %s\n, ctime(&tm)); start = clock(); delay(2000); end = clock(); printf(\n The time was: %f, (end - start) / CLK_TCK); first = time(NULL); /* Gets system time */ delay(2000); /* Waits 2 secs */ second = time(NULL); /* Gets system time again */ 45
46
Lecture No. 14
Assignment No.1
Objective Write a program to demonstrate how to use mathematics function like [acos(), asin(), atan(), atan2(), cos(), sin(), tan(), log(), log10(), pow(), sqrt(), ceil(), floor(), fmod(), abs(), labs()]. Command to be used 1. math functions Output
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int non_abs=-1230; long result; long x = -12345678L; double d1 = 5.0, d2 = 2.0; double number = 123.54; double down, up; double at1, at2; clrscr(); printf( Absolute value of %d = %d,non_abs, abs(non_abs)); 47
result= labs(x); printf(\n Number: %ld Absolute value of long number: %ld\n, x, result); printf(\n The remainder of (%lf / %lf) is %lf, d1, d2, fmod(d1,d2)); down = floor(number); up = ceil(number); printf(\n Number rounded down: %5.2lf, down); printf(\n Number rounded up: %5.2lf, up); printf(\n Square root of 16 = %lf, sqrt(9)); printf(\n Power of 9 = %lf, pow(9,2)); printf(\n The natural log of 800.6872 is: %lf, log(800.6872)); printf(\n The common log of 800.6872 is: %lf\n, log10(800.6872)); printf(\n The sin() of 0.5 is %lf, sin(0.5)); printf(\n The cosine of 0.5 is %lf, cos(0.5)); printf(\n The tan of 0.5 is %lf\n, tan(0.5)); printf(\n The arc sin of 0.5 is %lf, asin(0.5)); printf(\n The arc cosine of 0.5 is %lf, acos(0.5)); printf(\n The arc tangent of 0.5 is %lf, atan(0.5)); at1 = 90.0; at2 = 45.0; printf(\n The arc tangent ratio of %lf is %lf, (at2 / at1), atan2(at2, at1)); getch(); }
48
Lecture No. 15
Assignment No. 1
Objective This program demonstrates about local and global variable. Command to be used 1. global variables Output
#include<stdio.h> #include<conio.h> int x=10; void main() { int y=20; clrscr(); printf(Addition of x and y= %d, (x+y)); getch(); }
49
Lecture No. 15
Assignment No.2
Objective Write the program the addition of two numbers using function Command to be used 1. Functions Output
#include<stdio.h> #include<conio.h> int add(int a, int b) /* Create the function */ { return a+b; /* return to add */ } void main() { int R; clrscr(); printf(Addition of 5 and 10 is: %d,add(5,10)); /*Calling the function */ getch(); }
50
Lecture No. 16
Assignment No.1
Objective Write a program to display memory address of a variable Commands to be used 1. Pointer Output
#include<stdio.h> #include<conio.h> #define p printf #define s scanf //pointers void main() { int *p1; int a=10; p1=&a;//storing the address of a clrscr(); p(value of a is %d,a); p(\naddress of a is %d,&a); p(\nvalue of a is %d,*p1); p(\naddress of a is %d,p1); getch(); }
51
Lecture No. 16
Assignment No.2
Objective Write a program to swap the values of variables a & b using function with pointer as parameters Commands to be used 1. Pointer 2. functions (call by reference) Output
#include<stdio.h> #include<conio.h> void swapv(int *, int *); void main() { int a=10, b=20; clrscr(); swapv(&a, &b); printf(\na=%d b=%d, a, b); getch(); } void swapv(int *x, int *y) { int t; t=*x; *x=*y; *y=t; }
52
Lecture No. 16
Assignment No.3
Objective Write a program to allocate and de-allocate the memory to variables Commands to be used 1. Malloc 2. Alloc Output
#include <stdio.h> #include <string.h> #include <alloc.h> #include <process.h> #include<conio.h> void main() { char *str; clrscr(); /* allocate memory for string */ if ((str = (char *) malloc(10)) == NULL) { printf(Not enough memory to allocate buffer\n); exit(1); /* terminate program if out of memory */ } /* copy Hello into string */ strcpy(str, Hello); /* display string */ printf(String is %s\n, str); /* free memory */ free(str); getch(); }
53
Lecture No. 17
Assignment No. 1
Objective Write a program to demonstrate the use storage classes Commands to be used 1. 2. 3. 4. Output auto extern static register.
#include <stdio.h> #include <conio.h> /* Func1 is only visable to functions in this file. */ static void Func1(void); /* Func2 is visable to all functions. */ void Func2(void); int main(void) { char ch; clrscr(); printf(Press 'e' key....\n); 54
for(; ;) { ch=getche(); if (ch == 'e') { auto int t; for (t=0; t<'e'; t++) printf(%d , t); break; } } getch(); clrscr(); //call static functions Func1(); Func2(); getch(); return 0; } void Func1(void) { printf(Func1 called); } void Func2(void) { printf(Func2 called); }
55
Lecture No. 18
Assignment No.1
Objective Write a program to read the content of a file Commands to be used 1. 2. 3. 4. Output File while fopen() fclose()
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp;//file pointer char ch; fp=fopen(test.txt,r); if(fp==NULL) { printf(Error: file can not be read); getch(); exit(0); } printf(content of the file :\n\n); ch=fgetc(fp); while(ch!=EOF) { printf(%c,ch); ch=fgetc(fp); } fclose(fp); getch(); }
56
Lecture No. 18
Assignment No.2
Objective Write a program to write some content in a file Commands to be used 1. 2. 3. 4. Output File while fopen() fclose()
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp;//file pointer char ch; fp=fopen(samplefile.txt,a); if(fp==NULL) { printf(Error: file can not be read); getch(); exit(0); } clrscr(); // ch=fgetc(fp);//fscanf(fp,%c,&ch); scanf(%c,&ch); while(ch!='~') { fprintf(fp,%c,ch);//fputc(fp,ch); scanf(%c,&ch); // ch=fgetc(fp); } fclose(fp); printf(file created & saved); getch(); } 57
Lecture No. 19
Assignment No. 1
Objective Write a program which accepts one argument and displays it in upper case using string functions Commands to be used 1. Command line arguments 2. String function Output
#include <stdio.h> #include<conio.h> #include <string.h> void main(int argc, char *argv[]) { int a,b; char *c; if( argc != 2) { printf(Invalid no of arguments supplied\n use:\nsum [no1] [no2]\n); } else { printf(%s,strupr(argv[1])); } }
58
Lecture No. 19
Assignment No. 2
Objective Write a program which accepts file name and deletes it from command line Commands to be used 1. Command line arguments Output
void main(int argc, char *argv[]) { int a,b; char *c; if( argc != 2) { printf(Invalid no of arguments supplied\n use:\nsum [no1] [no2]\n); } else { remove(argv[1]); printf(file deleted); } }
59
Lecture No. 20
Assignment No. 1
Objective Write a program to draw a line using graphics Commands to be used 1. initgraph() 2. line() Output
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode; clrscr(); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ../bgi); /* draw a diagonal line */ line(0, 0, 100, 100); getch(); /* clean up */ closegraph(); return 0; }
60
Lecture No. 20
Assignment No. 2
Objective Write a program to draw a rectangle Commands to be used 1. initgraph() 2. rectangle() Output
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; clrscr(); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ../bgi); /* draw a diagonal rectangle */ rectangle (150, 200, 500, 100); getch(); /* clean up */ closegraph(); }
61
Lecture No. 20
Assignment No. 3
Objective Write a program to draw a polygon. Commands to be used 1. initgraph() 2. drawpoly() Output
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> void main() { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int poly[10]; clrscr(); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ../bgi); poly[0] = 20; poly[1] = 100; /* 1st vertext */
poly[3] = 200; poly[4] = 150; /* 3rd */ poly[5] = 20; poly[6] = 50; /* 4th */ poly[7] = 10; /* drawpoly doesn't automatically close the polygon, so we close it. */ poly[8] = poly[0]; poly[9] = poly[1]; /* draw the polygon */ drawpoly(5, poly); /* draw a diagonal rectangle */ drawpoly(5, poly); getch(); /* clean up */ closegraph(); }
63
Lecture No. 20
Assignment No. 4
Objective Write a program to set background color and forecolor Commands to be used 1. initgraph() 2. setcolor(int color) 3. setbkcolor(int color) Output
#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; clrscr(); /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ../bgi); setcolor(4); line(100, 100,500,100); setbkcolor(2); getch(); /* clean up */ closegraph(); }
64
Lecture No. 20
Assignment No. 5
Objective Write a program to set background color and forecolor Commands to be used 1. initgraph() 2. setcolor(int color) 3. setbkcolor(int color) Output For output run the program and listen the sound #include <dos.h> int main(void) { sound(7); delay(10000); nosound(); return 0; }
65