20 c programs

32
INDEX 1. 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/9 5. Write a C program to Find simple interest I=p*t*r/100 6. 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?

Upload: navjoth

Post on 06-May-2015

1.421 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 20 C programs

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/9

5. Write a C program to Find simple interest I=p*t*r/100

6. 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?

Page 2: 20 C programs

1) Explain dos internal and external commands?

*MS-DOS (Microsoft-Disk Operating System):

Gary kildall of Digital Research developer developed the first popular operating system for microcomputers (PC) in 1974. It was named CP\M (Control Program for Microcomputers) and it created incompatibility (the software developed for one computer would not run on another computer).

IBM and Microsoft Company developed an operating system named as PC-DOS (Personal Computer-Disk Operating System). After that PC-DOS name was changed as MS-DOS.

*HOW TO OPEN COMMAND PROMPT:

Click on START menu on the system toolbar, a pop-up menus are visible, in that select RUN menu, a dialog box will appear. In that we type COMMAND or CMD and 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 identified with its name. Naming of a file normally has two parts-File Name and File Extension.

File name can be at most eight characters long. Characters can be either lowercase 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 as well as output data of the program.

Batch files:

These contain a set of instructions that are to be executed as a batch in a sequential order. These files are usually used to perform a specific task.

Page 3: 20 C programs

Application program files:

These are usually application specific used to perform specific task. Word processors, spreadsheets are some of the application program files.

*DIRECTORY:

A directory is a logical grouping of files. A directory contains the name of the files, 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 area

1. System Area:

This area is used to keep track of any vital information about disk. This system area uses very small portion (2 %) of the total disk.

This system area is further divided into BOOT Area, FAT Area and ROOT Directory.

Boot area:

It is also known as BOOT RECORD. This area stores the program, which is used to do the task of beginning and loading the MS-DOS into the memory of the 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 record the 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, + ’ .’ +

Page 4: 20 C programs

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.

Page 5: 20 C programs

Syntax: cd directory-name. E.g.: c:\> cd gvp

RD: This command is used to remove a current directory.

Syntax: rd directory-name. E.g.: c:\> rd gvp

DATE: This command displays the current system date and we can set or modify the system date.

Syntax: date E.g.: C:\> date

TIME: This command displays the current system time and we can set or modify the system time.

Syntax: time E.g.: C:\> time

DIR: This command displays the list of file names and directory names of the current drive.

Syntax: dir E.g.: C:\> dir

Options:

CLS:

This command is used to clear the text on the display screen.

Syntax: cls E.g.: C:\>cls

TYPE: This command displays contents of specified file.

Syntax: type file-name E.g.: C:\> type gvp.txt

COPY: 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.txt

COPY CON: To create a file and insert the information into that file we used copy con command.

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 information press CTRL+Z.

Page 6: 20 C programs

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.txt

DEL: This command is used to delete the specified file.

Syntax: del file-name E.g.: C:\> del gvp.txt

TREE: This command displays the sub-directories and files as tree structure of current directory.

Syntax: <path> tree E.g.: C:\> gvp\gvpdegree tree

VER: 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 or hard disk). Loading it from external devices to main memory of the computer can use these commands.

PRINT: It is used to print the current document.

Syntax: print file-name E.g.: C:\> print gvpdegree.txt

FORMAT: It is used to format the current drive in the computer system.

Syntax: format: drive-name E.g.: C:\> format: C

LABEL: 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:

Page 7: 20 C programs

Program:#include <stdio.h> main(){ int x, y, temp;  printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y);  printf("Before Swapping\nx = %d\ny = %d\n",x,y);  temp = x; x = y; y = temp;  printf("After Swapping\nx = %d\ny = %d\n",x,y); }

Output:Enter the value of x and yx= 10y=20Before swapping x=10y=20After swapping x=20y=10

3) Write a C program to Find the roots of quadratic equation?Algorithm:

Page 8: 20 C programs

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=%d\nx2=%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=%d\nx2=%d”,f,g);

}else{

printf(“Roots are imaginary”);d=-d;e=sqrt(d);f=-b/(2*a);g=e/(2*a);printf(“x1=%d+%d\nx2%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

Page 9: 20 C programs

Algorithm:

Program:

#include <stdio.h>main (){

int fahrenheit;double celsius;

printf("Enter the temperature in degrees Fahrenheit:\n\n\n\n");scanf("%d", &fahrenheit);celsius = (5/9) * (fahrenheit-32);printf ("The converted temperature is %lf\n", 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:

Page 10: 20 C programs

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:

Page 11: 20 C programs

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:

Page 12: 20 C programs

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=15j=29biggest of two numbers(using else) is = 29

8) Write a C program to check that given number is even or odd?

Algorithm:

Page 13: 20 C programs

Program:

#include<stdio.h> main(){ int n;  printf("Enter an integer\n"); scanf("%d",&n);  if ( n%2 == 0 ) printf("Even\n"); else printf("Odd\n"); }

Output:

Enter an integer:5

Odd

9) Write a C program to check the given no is +ve or –ve?

Algorithm:

Page 14: 20 C programs

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?

Page 15: 20 C programs

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=23b=34c=22Biggest number of given three numbers = 34

11)Write a C program to perform Arithmetic operation using switch case?

Algorithm:

Page 16: 20 C programs

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("\n\nEnter 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> : 111

12) Write a C program to calculate Reverse of given numbers?

Page 17: 20 C programs

Algorithm:

Program:

#include <stdio.h> main(){ int n, reverse = 0;  printf("Enter a number to reverse\n"); scanf("%d",&n);  while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; }  printf("Reverse of entered number is = %d\n", 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?

Page 18: 20 C programs

Algorithm:

// sum of the digits of num

Step 0 : startStep 1 : declare main functionStep 2 : int n, sum = 0, remainder;Step 3: input n valueStep 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 integer\n"); scanf("%d",&n);  while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; }  printf("Sum of digits of entered number = %d\n",sum);

}Output:Enter an integer : 123Sum of digits of entered number = 6

14) Write a C program to Print 1-n even numbers?

Page 19: 20 C programs

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 ?

Page 20: 20 C programs

Algorithm:

Program:

#include<stdio.h>main(){ int n, i = 3, count, c;  printf("Enter the number of prime numbers required\n"); scanf("%d",&n);  if ( n >= 1 ) { printf("First %d prime numbers are :\n",n); printf("2\n"); }  for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c++ ) { if ( i%c == 0 ) break; } if ( c == i ) { printf("%d\n",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?

Page 21: 20 C programs

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: 5Factorial of 5 is: 120

17) Write a C program to find Sum of element of array?

Algorithm:

Page 22: 20 C programs

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 %d\n", sum); getch();}

Output:The sum is = 53

18) Write a C program to find the length of the string?

Page 23: 20 C programs

Algorithm:

Program:

#include <stdio.h>void main(){

char string[50];int i, length = 0;printf("Enter a string\n");gets(string);for (i=0; string[i] != '\0'; i++) { length++; }printf("The length of a string is the number of characters in it\n");printf("So, the length of %s =%d\n", string, length);

}

/*----------------------------------------------------OutputEnter a stringhelloThe length of a string is the number of characters in itSo, the length of hello = 5

19) Write a C program to print a multiplication table?

Page 24: 20 C programs

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:55 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 455 x 10 = 50

20) Write a program to display 'n' natural numbers using for loop..(in C).....

Page 25: 20 C programs

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.....

12345678910