Вы находитесь на странице: 1из 26

C PROGRAMS SLIP 1

Q1)
syntax and execute the following DOS commands i) dir/p iii) del v) cls ii) cd iv)date

dir/p: lists the contents of the specified directory SYNTAX: DIR[drive:][path][filename][.ext][/option] Option specifies one or more option to be used like: /p : page wise listing. /o : ordered listing. /A : Attribute wise listing. /W : Wide format(width wise ) listing.

(Q2)
//Write a program to find whether number is odd or even. #include <iostream.h> #include <conio.h> void main () { int a; clrscr (); printf ("Enter Any Number: "); scanf(%d,&a); if (a%2==0) { Printf("Number is Even"); } else { Printf("\n Number is Odd); } getch (); }

(Q3)
Maximum and minimum number in array
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,n,max=0,min=0; int a[100]; clrscr(); printf("Enter the limit:\n"); scanf("%d",&n); printf("Enter the Value:\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } max=min=a[0]; for(i=0;i<n;i++) { if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } printf("\nmaximum=%d\n\nminimum=%d",max,min); getch(); }

(Q4) Menu Driven program working of library..


#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void add_book(); void disp_book(); void book_auth(); void count_book(); struct lib { int acc_no; char book_title[20]; char author[20]; int cost; }b[100]; int count; void main() { int ch; while(1) { clrscr(); { printf("\n 1:enter the book info\n"); printf("\n 2:display book info\n"); printf("\n 3:book author\n"); printf("\n 4:count book\n"); printf("\n 5:exit\n"); printf("\n enter the choice \n"); scanf("%d",&ch); switch(ch) { case 1:add_book();getch(); break; case 2:disp_book();getch(); break; case 3:book_auth();getch(); break; case 4:count_book();getch();

break; case 5:exit(0); } } } } void add_book() { if(count==9) { printf("\n no more space\n"); return; } printf("\n enter the detail of book \n"); printf("\n enter accession number of book ="); scanf("%d",&b[count].acc_no); printf("\n enter the book title="); scanf("%s",b[count].book_title); printf("\n enter the name of author="); scanf("%s",b[count].author); printf("\n enter the cost of book="); scanf("%d",&b[count].cost); count++; } void disp_book() { int i; printf("\n detail of %d booksin library",count); for(i=0;i<count;i++) { printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost); } } void book_auth() { int i,cnt=0; char name[20]; printf("\n enter the name of author="); scanf("%s",name); for(i=0;i<count;i++) { if(strcmp(name,b[i].author)==0) { cnt++; printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost); } } if(cnt==0) printf("\n no such book \n");

} void count_book() { printf("\n total no of book in library =%d",count); }

SLIP 2

Q1 :
syntax and execute the following DOS commands i) copy iii) time v) ren copy: used to copy or append file to another file. Syntax:copy[drive:][path] filename[.ext][+drive:][path] filename[.ext] copy con: copies the content typed at the console to file a.txt. Input has to be determined by ctrl +Z. time: Used to display and change the system time. Syntax: TIME[hh:mm:ss:xx] ver: Displays the current DOS version. Syntax : ver iv)ver ii) copy con

Q2 Pattern Aa Aa Bb Aa Bb Cc Aa Bb Cc Dd..
#include<stdio.h> #include<conio.h> void main() { int i,j,n; char c,c1; clrscr(); printf("Enter the number of lines to be printed: "); scanf("%d",&n); printf("\n\n"); for(i=0;i<n;i++) { c='A' && c1=a; for(j=0;j<=i;j++) { printf("%c",c++); } printf("\n"); } getch(); }

Q4. Write a C program to accept customer details such as: Account_no, Name, Balance in account,. Assume Maximum 20 customers in the bank. Write a function to print the account no and name of each customer with balance below Rs.100.

#include<stdio.h> #include<conio.h> void main() { struct bank { int acc_no; char name[20]; int bal; }b[5]; int i; clrscr(); for(i=0;i<5;i++) { printf(" \n\n enter the acc_no,name,balance\n\n"); printf("\nenter the account no\n"); scanf("%d",&b[i].acc_no); printf("\n enter the name\n"); scanf("%s",b[i].name); printf("\n enter the balance\n"); scanf("%d",&b[i].bal); } for(i=0;i<5;i++) { if(b[i].bal<100) { printf("\n---------------------------------------------\n"); printf("\n\n The acc_no,name,balance below RS:-100\n\n"); printf("\n the account no:-%d\n",b[i].acc_no); printf("\n the name:-%s\n",b[i].name); printf("\n the balance:-%d\n",b[i].bal); } } getch(); }

SLIP 3 Q2: to print the pattern

#include<stdio.h> #include<conio.h> void main() { int i,j,n; clrscr(); printf("Enter the number of lines to be printed:"); scanf("%d",&n); for(i=0;i<n;i++) { for(j=1;j<=n-i;j++) { printf("%d ",j); } printf("\n"); } getch(); }
Q3:Write a C Program to accept 5 names from user and store these names into an array. Sort these array elements in alphabetical order.
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,d; char ch[20][30],tmp[30]; clrscr(); printf("\n enter the no of string\n"); scanf("%d",&d); printf("\n entert %d string",d); for(i=0;i<=d;i++) { gets(ch[i]); printf("\n\n\n"); printf("\nenter string are\n");

} for(i=0;i<=d;i++) { printf("\n%s",ch[i]); } for(i=0;i<=d;i++) { for(j=i+1;j<=d;j++) { if(strcmp(ch[i],ch[j])>0) { strcpy(tmp,ch[i]); strcpy(ch[i],ch[j]); strcpy(ch[j],tmp); } } } printf("\n\n\n"); printf("\n sorted string are:"); for(i=0;i<=d;i++) printf("\n%s",ch[i]); getch(); } Q4:To create a text file and print it in reverse order. #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char ch; int i,pos; clrscr(); fp=fopen("input.txt","r+"); fseek(fp,1,SEEK_END); pos=ftell(fp); i=0; while(i<=pos) { ch=fgetc(fp); printf("%c",ch); fseek(fp,-i,SEEK_END); i++; } getch(); }

SLIP 4: Q2: Sum of digits of a number


#include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); while(num){ r=num%10; num=num/10; sum=sum+r; } printf("Sum of digits of number: return 0; }

%d",sum);

Q3 : Write a C Program to swap the values of two variables by using call by reference
#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the value of a & b:\n"); scanf("%d%d",&a,&b); printf("A=%d\n\nB=%d",a,b); swap(&a,&b); printf("\nA=%d\nB=%d\n",a,b); getch(); } swap(int*x,int*y) { *x=*x+*y; *y=*x-*y; *x=*x-*y; return; }

Q4: Write a C program to write a macro definition for the following


i) ii) iii) To test whether a character is a lower case letter or not. To check whether a character is alphabet or not. To obtain the largest of two numbers.

#include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> #define LOWER(ch) (ch>=97&&ch<=122 ? printf("\nlower"):printf("\n not lower")) #define ALPHA(ch) (isalpha (ch) ? printf("\n alphabet"):printf("\n not alphabet")) #define LARGE(a,b) (a>b ? printf("\n a is greater"):printf("\n b is greater")) void main() { char ch; int a,b; clrscr(); printf("\n enter the character="); scanf("%c",&ch); printf("\n enter the two number ="); scanf("%d%d",&a,&b); LOWER (ch); ALPHA (ch); LARGE (a,b); getch(); }

Slip 5 Q2: Write a C program to calculate Factorial of a given number.


#include<stdio.h> int main(){ int i=1,f=1,num; printf("Enter a number: "); scanf("%d",&num); while(i<=num){ f=f*i; i++; } printf("Factorial of %d is: %d",num,f); return 0; }

Q3: Write a C Program to accept a string from user and generate following pattern
(e.g. input is string abcd)

#include void main() { int i,j,k,n,l; clrscr(); printf("\nEnter the no lines to be printed: "); scanf("%d",&n); printf("\n\n"); for(i=0;i0;j--) if you want spaces // printf(" "); for(k=n-i;k<=n;k++) { printf("%c ",97-n+k); } for(l=1;l<=i;l++) {

printf("%c ",97-l); } printf("\n"); } }


Q4) Write a C Program to copy the contents of one file into another file. #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch,fname1[20],fname2[20]; printf("\n enter sourse file name"); gets(fname1); printf("\n enter sourse file name"); gets(fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"w"); if(fp1==NULL||fp2==NULL) { printf("unable to open"); exit(0); } do { ch=fgetc(fp1); fputc(ch,fp2); } while(ch!=EOF); fcloseall(); getch(); }

SLIP 6
Q2: Write a C program to calculate sum of first and last digit of a given number
#include<stdio.h> int main() { int num; int sum=0,i,lastDigit; printf("Enter any positive number more than one digit : "); scanf("%d",&num); sum = sum + num % 10; while(num != 0){ lastDigit = num; num = num /10; } sum = sum + lastDigit; printf("\nSum of first and last digit of given numer is :%d",sum); }
Q.3) Write a C Program to accept a string from user, delete all vowels from that string and display the result.
#include<stdio.h> #include<conio.h> void main() { int i=0,flag=0; char s[50]; clrscr(); printf("\n enter the string:-"); gets(s); printf(" the string is =%s",s); printf("\n"); while(s[i]!=NULL) { if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A' || s[i]=='E' || s[i]=='I'|| s[i]=='O' || s[i]=='U') { flag=1; } else

{ printf("%c",s[i]); } i++; } getch(); }

Slip 7
Q.3) Write a C program to accept a sentence from user and reverse its each word #include<stdio.h> #include<string.h> main() { char str[50],revstr[50]; int i=0,j=0; printf("Enter the string to be reversed : "); scanf("%s",str); for(i=strlen(str)-1;i>=0;i--) { revstr[j]=str[i]; j++; } revstr[j]='\0'; printf("Input String : %s",str); printf("\nOutput String : %s",revstr); getch(); }

SLIP 8
Q.2) Write a C program to generate following pattern for n lines: * * * * * * * * * *

#include<stdio.h> main() { int n, c, k = 2, j; printf("Enter number of rows\n"); scanf("%d",&n); for ( j = 1 ; j <= n ; j++ ) { for ( c = 1 ; c <= 2*n-k ; c++) printf(" "); k = k + 2; for ( c = 1 ; c <= j ; c++) printf("* "); printf("\n"); } getch(); return 0; }

Q.3)

Write a C Program to calculate sum of elements of a mXn matrix.

#include<stdio.h> #include<conio.h> void main() { int a[10][10],r,c; int sum=0,i=0,j=0; clrscr(); printf("\n enter the row and coloum\n"); scanf("%d%d",&r,&c); printf("\n enter the matrix elements\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&a[i][j]); } } printf("\n the matrix elements are\n\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("\t%d",a[i][j]); } printf("\n"); } printf("\n sum of elements\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { sum=sum+a[i][j]; } } printf("\n the elements sum=%d",sum); getch(); }

Q.4)

Write a C Program to append the contents of one file at the end of another file

#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch,fname1[20],fname2[20]; printf("\n enter sourse file name"); gets(fname1); printf("\n enter sourse file name"); gets(fname2); fp1=fopen(fname1,"r"); fp2=fopen(fname2,"a"); if(fp1==NULL||fp2==NULL) { printf("unable to open"); exit(0); } do { ch=fgetc(fp1); fputc(ch,fp2); } while(ch!=EOF); fcloseall(); }

SLIP 9 Q2: Write a C program to calculate sum of Fibonacci series up to a given terms

#include<stdio.h>
int main(){ int i,range; long int arr[40]; printf("Enter the number range: "); scanf("%d",&range); arr[0]=0; arr[1]=1; for(i=2;i<range;i++){ arr[i] = arr[i-1] + arr[i-2]; } printf("Fibonacci series is: "); for(i=0;i<range;i++) printf("%ld ",arr[i]); return 0; }

Q3:
Write a C Program to accept n numbers from user, store these numbers into an array and count the number of occurrence of each number

#include<stdio.h> #include<conio.h> void main() { int a[10],i,j,k; int count=1,num[10],pos=0; clrscr(); printf("Enter the Array Element "); for(i=0;i<10;i++) { scanf("%d",&a[i]) ; }//close the for loop for(i=0;i<10;i++) { count=1,pos++; for(j=0;j<10;j++) { if(a[i]==a[j]) { for(k=j;k<10;j++) a[k]=a[k+1]

} count++; } num[pos] = count; } for(i=0;i<pos;i++) printf("Repeted Number of IN Arrary %d",num[i]); }

Q4: Write a C Program to copy one file to another file while doing so replace all lower case character
to their equivalent upper case character
#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char *fn1,*fn2,ch; clrscr(); printf("\n enter the source file"); gets(fn1); printf("\n enter the destination file"); gets(fn2); fp1=fopen(fn1,"r"); fp2=fopen(fn2,"w"); if(fp1==NULL||fp2==NULL) { printf("\n unable to open file"); exit(0); } while(!feof(fp1)) { ch=fgetc(fp1); if(ch>='a'&& ch<='z') ch=ch-32; fputc(ch,fp2); } printf("\n file successfully copied"); fcloseall(); getch(); }

SLIP 10
Q.3) Write a C Program to display the transpose of a given 3X3 matrix.

#include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf("enter the elements\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("matrix\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",a[i][j]); } printf("\n"); } printf("-------------------------\n"); printf("transpose\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",a[j][i]); } printf("\n"); } getch(); }

Q.4)

Write a menu driven program in C which performs the following operations on

strings. Write separate function for each option Check if one string is substring of another string Count number of occurrences of a character in the string. Exit.

#include<stdio.h> #include<conio.h> #include<string.h> void main() { void substring(); void occurrence(); while(1) { int ch; clrscr(); printf("\n 1:check substring\n"); printf("\n 2:occurrence \n"); printf("\n 3:EXIT"); printf("\n\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1:substring();getch(); break; case 2:occurrence();getch(); break; case 3:exit(0); break; } } } void substring() { int s; char ch[50],sub[10]; printf("\n enter the string : "); scanf("%s",ch); printf("\n enter the substring: "); scanf("%s",sub); s=substr(ch,sub); if(s==-1) printf("\n substring not found"); else printf("\n substring is found \n"); printf("\n starting location of substring :%d",s); } int substr(char ch[50],char sub[10]) { int b,e,l,i=0,j=0; while(ch[i]!='\0')

{ if(ch[i]==sub[j]) { b=i; while(sub[j]!='\0') { e=ch[i]-sub[j]; i++; j++; } } i++; } if(e==0) return(b+1); else return(-1); } void occurrence() { char *s1,*s2,ch; int cnt; clrscr(); printf("\n enter the string \n"); scanf("%s",s1); printf("\n enter the character ="); scanf("%s",s2); cnt=0; while(*s1!='\0') { if(*s1==*s2) { cnt++; } s1++; } printf("\n %s occour %d times ",s2,cnt); }

SLIP 11
Q.2) Write a C program to accept character & display its ASCII value and its next & previous character. #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char ch; clrscr(); printf("enter the charter"); scanf("%c",&ch); printf("charter=%c\n",ch); printf("ascii value=%d\n",ch); printf("next charter=%c\n",ch+1); printf("pre charter=%c\n",ch-1); getch(); } Q.3). Write a C Program to accept n numbers and store all prime numbers in an array and display this array
#include<stdio.h> #include<conio.h> void main() { int a[50],i=0,j=0; int n,f=0; clrscr(); printf("\n enter the limit ="); scanf("%d",&n); printf("\n enter the elements \n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { f=0; if(a[i]<=0) break; else { for(j=2;j<=a[i]/2;j++)

{ if(a[i]%j==0) { f=1;break; } } if(f==0) printf("prime number=%d",a[i]); } } getch(); }

Вам также может понравиться