20 C programs
Upcoming SlideShare
Loading in...5
×
 

20 C programs

on

  • 1,372 views

 

Statistics

Views

Total Views
1,372
Views on SlideShare
1,372
Embed Views
0

Actions

Likes
0
Downloads
31
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    20 C programs 20 C programs Document Transcript

    • INDEX1. Explain dos internal and external commands?2. Write a C program for swapping of two numbers?3. Write a C program to Find the roots of quadratic equation?4. Write a C program to find centigrade value C=(f-32)5/95. Write a C program to Find simple interest I=p*t*r/1006. Write a C program to find sum of ‘n’ numbers?7. Write a C program to find the biggest of two numbers?8. Write a C program to check that given number is even or odd?9. Write a C program to check the given no is +ve or –ve?10.Write a C program to find the biggest of 3 elements?11.Write a C program to perform Arithmetic operation using switch case?12.Write a C program to calculate Reverse of given numbers?13.Write a C program to find Sum of digits of a given numbers?14.Write a C program to Print 1-10 even numbers?15.Write a C program to Check prime number or not?16.Write a C program to find Factorial of a given number?17.Write a C program to find Sum of element of array?18.Write a C program to find the length of the string?19.Write a C program to print a multiplication table?
    • 1) Explain dos internal and external commands?*MS-DOS (Microsoft-Disk Operating System): Gary kildall of Digital Research developer developed the first popularoperating system for microcomputers (PC) in 1974. It was named CPM (ControlProgram for Microcomputers) and it created incompatibility (the softwaredeveloped for one computer would not run on another computer). IBM and Microsoft Company developed an operating system named asPC-DOS (Personal Computer-Disk Operating System). After that PC-DOS namewas changed as MS-DOS.*HOW TO OPEN COMMAND PROMPT: Click on START menu on the system toolbar, a pop-up menus are visible, in thatselect RUN menu, a dialog box will appear. In that we type COMMAND or CMDand click on ENTER key. It will display the command prompt.Program: Program is a set of various instructions that are using a computer language.*FILE: File is a collection of text or data that are logically related. A file is identifiedwith its name. Naming of a file normally has two parts-File Name and FileExtension. File name can be at most eight characters long. Characters can be eitherlowercase letters or uppercase letters or numbers or some special characters also.Some of the file extensions are .exe, .bat, .doc, .txt, etc. Text files: These are the files used to stored data. These are the files used to store inputs aswell as output data of the program. Batch files: These contain a set of instructions that are to be executed as a batch in asequential order. These files are usually used to perform a specific task.
    • Application program files: These are usually application specific used to perform specific task. Wordprocessors, spreadsheets are some of the application program files.*DIRECTORY: A directory is a logical grouping of files. A directory contains the name of thefiles, along with their extensions, the file size, and the date of creation.*STRUCTURE OF MS-DOS DISK: MS-DOS divides the disk into two parts are system area and Data area1. System Area: This area is used to keep track of any vital information about disk. Thissystem area uses very small portion (2 %) of the total disk.This system area is further divided into BOOT Area, FAT Area and ROOTDirectory. Boot area: It is also known as BOOT RECORD. This area stores the program, whichis used to do the task of beginning and loading the MS-DOS into the memory ofthe computer. This process is called booting process. Fat (File Allocation Table) area: This area is used to manage the big data portions of the disk and to recordthe status of each area of the disk. Root Directory: This is the file directory used to record the files that are stored on the disk.2. Data area: It is the bigger part of the disk, where the data files are stored on the disk. * Naming a file in MS-DOS: Every file stored on the disk is given a filename, which should be unique.The file can be of 8.3 characters i.e. name maximum of eight characters, + ’.’ +
    • maximum of three characters extension. If number of characters specified is more than 8.3, DOS will take only 8.3 characters and truncate the rest. * Characteristics of MS-DOS:• Every DOS commands have to be typed at the DOS prompt in either upper or lower case.• Each command is executed by pressing ENTER key.• Maximum length of a DOS command cannot exceed 127 characters.• Pressing CTRL+C or CTRL+Z to cancel the command under execution. *File Specification: Along with the filename, a drive specifies may be given while referring to a file. E.g.: C: SAMPLE. TXT Drive File Name Extension * Changing the drives: If we are in C drive. C:> If we want to change the drive D, just write drive name followed by :(Colon symbol). E.g.: C:> D: * COMMANDS IN MS-DOS:• Internal commands• External commands Internal commands: Internal commands are in-built commands of MS-DOS. MD: This command is used to create a new directory. Syntax: md directory-name. E.g.: c:> md gvp
    • CD: This command is used to change from one directory to another directory. Syntax: cd directory-name. E.g.: c:> cd gvpRD: This command is used to remove a current directory. Syntax: rd directory-name. E.g.: c:> rd gvpDATE: This command displays the current system date and we can set or modify thesystem date. Syntax: date E.g.: C:> dateTIME: This command displays the current system time and we can set or modify thesystem time. Syntax: time E.g.: C:> timeDIR: This command displays the list of file names and directory names of the current drive. Syntax: dir E.g.: C:> dirOptions:CLS: This command is used to clear the text on the display screen. Syntax: cls E.g.: C:>clsTYPE: This command displays contents of specified file. Syntax: type file-name E.g.: C:> type gvp.txtCOPY: This command is used to copies one or more files to specified file. Syntax: copy old-name new-name E.g.: C:> copy gvp.txt gvpdegree.txtCOPY CON: To create a file and insert the information into that file we used copy concommand. Syntax: copy con file-name E.g.: copy con gvp.txt
    • This command inserts the data on current file. If we want to stop the inserting informationpress CTRL+Z.RENAME / REN: This command is used to Change the name of specified file or directory. Syntax: ren old-name new-name E.g.: C:> ren gvp.txt gvpdegree.txtDEL: This command is used to delete the specified file. Syntax: del file-name E.g.: C:> del gvp.txtTREE: This command displays the sub-directories and files as tree structure of currentdirectory. Syntax: <path> tree E.g.: C:> gvpgvpdegree treeVER: This command displays the DOS version number that is being used.VOL: This command displays the disk volume label of the specified drive.External commands: The external commands are separate programs that reside on external devices (floppy, cd orhard disk). Loading it from external devices to main memory of the computer can use thesecommands.PRINT: It is used to print the current document. Syntax: print file-name E.g.: C:> print gvpdegree.txtFORMAT: It is used to format the current drive in the computer system. Syntax: format: drive-name E.g.: C:> format: CLABEL: It is used to change the label of current drive.SYS: It is used to transfer the operating system files from one drive to another drive.DISKCOPY: It is used to copy the disk and transfer the information into another disk.CHKDSK: It is used to scan the disk in the specified drive and checks it for
    • 2) Write a C program for swapping of two numbers? Algorithm: Program: #include <stdio.h> main() { int x, y, temp; printf("Enter the value of x and yn"); scanf("%d%d", &x, &y); printf("Before Swappingnx = %dny = %dn",x,y); temp = x; x = y; y = temp; printf("After Swappingnx = %dny = %dn",x,y); } Output: Enter the value of x and y x= 10 y=20 Before swapping x=10 y=20 After swapping x=20 y=10
    • 3) Write a C program to Find the roots of quadratic equation? Algorithm: Program: #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d,e,f,g; clrscr(); printf(“Enter the three values”); scanf(“%d%d%d”,&a,&b,&c); d=((b*b)-(4*a*c)); if(d==0) { printf(“Roots are real and equal”); f=-b/(2*a); printf(“x1=%dnx2=%d”,f,f); } else if(d>0) { printf(“Roots are real and distinct”); e=sqrt(d); f=(-b+e)/(2*a); g=(-b-e)/(2*a); printf(“x1=%dnx2=%d”,f,g); } else { printf(“Roots are imaginary”); d=-d; e=sqrt(d); f=-b/(2*a); g=e/(2*a); printf(“x1=%d+%dnx2%d-%d”,f,g,f,g); } getch(); }Output:Enter the three values:a=2 b=5 c=3d=25-24=1Roots are real and distinct:x1= -1y1= -1.5
    • 4) Write a C program to find centigrade value C=(f-32)5/9 Algorithm: Program: #include <stdio.h> main () { int fahrenheit; double celsius; printf("Enter the temperature in degrees Fahrenheit:nnnn"); scanf("%d", &fahrenheit); celsius = (5/9) * (fahrenheit-32); printf ("The converted temperature is %lfn", celsius); } Output: Enter the temperature in degrees Fahrenheit: 5 The converted temperature is= -15
    • 5) Write a C program to Find simple interest I=p*t*r/100 Algorithm: Program: #include<stdio.h> #include<conio.h> main() { float p,rate,time,si; printf("Enter principal amount : "); scanf("%f", &p); printf("Enter rate of interest : "); scanf("%f", &rate); printf("Enter time period in year : "); scanf("%f", &time); /*calculating simple interest*/ si=(p*time*rate)/100; printf("nSimple Interest = %2f",si); getch(); } Enter principal amount : 1000 Enter rate of interest : 2.5 Enter time period in year : 2 Simple Interest = 50
    • 6) Write a C program to find sum of ‘n’ numbers? Algorithm: Program: #include<stdio.h> #include<conio.h> main() { int s=0,i=1,n; clrscr(); printf("Enter a value "); scanf("%d",&n); while(i <= n) { s=s+i; i=i+1; } printf("Sum of first %d natural numbers is %d",n,s) getch(); } Output: Enter a value = 5 Sum of first 5 natural numbers is = 15
    • 7) Write a C program to find the biggest of two numbers?Algorithm:Program:#include <stdio.h>main() { int i,j,big; printf(“Enter any two numbers”); scanf("%d%d",&i,&j); if(i < j) { big = j; } else { big = i; } printf("biggest of two numbers(using else) is %d n",big);} Output: Enter any two numbers: i=15 j=29 biggest of two numbers(using else) is = 29
    • 8) Write a C program to check that given number is even or odd?Algorithm:Program: #include<stdio.h> main() { int n; printf("Enter an integern"); scanf("%d",&n); if ( n%2 == 0 ) printf("Evenn"); else printf("Oddn"); }Output:Enter an integer:5Odd
    • 9) Write a C program to check the given no is +ve or –ve?Algorithm:Program: #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("enter the no"); scanf("%d",&n); if(n>=0) printf("nnumber is +ve"); if(n<0) printf("n number is -ve"); getch(); }Output:enter the no : 36nnumber is +ve
    • 10) Write a C program to find the biggest of 3 elements?Algorithm:Program: #include<stdio.h> #include<conio.h> main() { int a,b,c; printf(“Enter any three numbers n”); scanf(“%d%d%d”,&a,&b,&c); if(a > b) { if(a > c) { big = a; } else { big = c; } } else { if(b > c) { big = b; } else { big = c; } } printf(“Biggest number of given three numbers = %d “,big); getch(); }Output: Enter any three numbers: a=23 b=34 c=22 Biggest number of given three numbers = 34
    • 11)Write a C program to perform Arithmetic operation using switch case?Algorithm:Program: #include<stdio.h> #include<conio.h> void main() { int n1, n2, ch ; clrscr() ; printf("Enter the first number : ") ; scanf("%d", &n1) ; printf("nEnter the second number : ") ; scanf("%d", &n2) ; printf("1 -> Addition n ") ; printf("2 -> Subtraction n ") ; printf("3-> Multiplication n ") ; printf("4 -> Division n ") ; printf("nnEnter your choice <1...4> : ") ; scanf("%d", &ch) ; switch(ch) { case 1 : printf("n%d + %d = %d", n1, n2, n1 + n2) ; break ; case 2 : printf("n%d - %d = %d", n1, n2, n1 - n2) ; break ; case 3 : printf("n%d * %d = %d", n1, n2, n1 * n2); break ; case 4 : printf("n%d / %d = %.2f", n1, n2, (float)n1 / n2); break ; default : printf("nInvalid choice"); break ; } getch(); }Output: Enter the first number : 5 Enter the second number : 6 1 -> Addition 2 -> Subtraction 3-> Multiplication 4 -> Division Enter your choice <1...4> : 1 11
    • 12) Write a C program to calculate Reverse of given numbers?Algorithm:Program: #include <stdio.h> main() { int n, reverse = 0; printf("Enter a number to reversen"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %dn", reverse); }Output: Enter a number to reverse : 235 Reverse of entered number is = 532
    • 13) Write a C program to find Sum of digits of a given numbers? Algorithm: // sum of the digits of num Step 0 : start Step 1 : declare main function Step 2 : int n, sum = 0, remainder; Step 3: input n value Step 4: loop until we have no digits left to process while(n != 0) { step 4.1: find out the rightmost digit remainder = n % 10; step 4.2: add the rightmost digit to our sum sum = sum + remainder; step 4.3: remove the rightmost digit from num n = n / 10; } Step 5: print sum value Step 6 : stop } Program: #include <stdio.h> main() { int n, sum = 0, remainder; printf("Enter an integern"); scanf("%d",&n); while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; } printf("Sum of digits of entered number = %dn",sum); }Output:Enter an integer : 123Sum of digits of entered number = 6
    • 14) Write a C program to Print 1-n even numbers?Algorithm:Program: main() { int i,num; printf(“Enter any number”); scanf(“%d”,&num); for(i=0;i<=num;i++) if(num%2==0) printf(“%d”,num); }Output:Enter any number: 1012345678910
    • 15) Write a C program to display prime numbers ?Algorithm:Program: #include<stdio.h> main() { int n, i = 3, count, c; printf("Enter the number of prime numbers requiredn"); scanf("%d",&n); if ( n >= 1 ) { printf("First %d prime numbers are :n",n); printf("2n"); } for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%dn",i); count++; } i++; } }Output:Enter the number of prime numbers required:10First 10 prime numbers are :2357
    • 16) Write a C program to find Factorial of a given number?Algorithm:Program: #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; } Sample output: Enter a number: 5 Factorial of 5 is: 120
    • 17) Write a C program to find Sum of element of array?Algorithm:Program: #include <stdio.h> #include<conio.h> main() { int a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17}; int sum=0; clrscr(); printf("nArray:n"); for(int i=0;i<10;i++) sum = sum + a[i]; printf("The sum is %dn", sum); getch(); }Output: The sum is = 53
    • 18) Write a C program to find the length of the string?Algorithm:Program: #include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a stringn"); gets(string); for (i=0; string[i] != 0; i++) { length++; } printf("The length of a string is the number of characters in itn"); printf("So, the length of %s =%dn", string, length); } /*---------------------------------------------------- Output Enter a string hello The length of a string is the number of characters in it So, the length of hello = 5
    • 19) Write a C program to print a multiplication table?Algorithm:Program: #include <stdio.h> int main() { int num, i = 1; printf(" Enter any Number:"); scanf("%d", &num); printf("Multiplication table of %d: ", num); while (i <= 10) { printf(" %d x %d = %d", num, i, num * i); i++; } return 0; } Output: Enter any Number:5 5x1=5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
    • 20) Write a program to display n natural numbers using for loop..(in C).....Algorithm:Program: #include<stdio.h> main() { int n,i; printf("n Enter the range......Please...t"); scanf("%d",&n); printf("n Range of Natural Numbers.....t"); for(i=1;i<=n;i++) { printf("t%d",i); } }Output: Enter the range......Please... = 10 Range of Natural Numbers..... 1 2 3 4 5 6 7 8 9 10