IT

ALL IT Technology Information

C++ Tutorial

 

 C++ Tutorial


 


C LANGUAGE WORKBOOK

Table of Content

Page No. 3

3

4

5

6

7 8

9 10 11

12 13 14 15 16 17 18

19 20 21 22

23 24 25 26 27

Sr. No Contents

1 WRITE A PROGRAM TO PRINT HELLO WORLD.

2 WRITE A PROGRAM TO PRINT YOUR ADDRESS IN PARTICULAR FORMAT.

3 WRITE A PROGRAM To PRINT 1ST CHARACTER OF YOUR NAME.

4 WRITE A PROGRAM FOR ENTER ANY NO AND DISPLAY.

5 WRITE A PROGRAM TO PRINT ADDITION OF TWO NUMBER.

6 WRITE A PROGRAM TO DISPLAY TWO NUMBER.

7 WRITE A PROGRAM TO PRINT MULTIPLICATION OF TWO NOS.

8 WAP TO SUBTRACT TWO NOS.

9 WAP TO DIVISION OF TWO NOS. 10 WAP TO CALCULATE

ADDITION,SUBTRACTION,MULTIPLICATION AND DIVISION IN SINGLE PROGRAM.

11 WAP TO CONVERT FERANHIT TO CENTIGRATE. 12 WAP TO CALCULATE SIMPLE INTEREST.

13 WAP TO FIND AREA OF TRIANGLE. 14 WAP TO FIND AREA OF RECTANGLE.

15 WAP TO CALCULATE AREA OF CIRCLE.

16 WAP TO CHECK MAXIMUM NO IN TO TWO NOS. 17 WAP TO CHECK GIVEN NO IS POSSITIVE OR

NEGETIVE.

18 WAP TO CHECK U R ELLIGIBLE FOR VOTE OR NOT. 19 WAP TO CHECK GIVEN NO IS ODD OR EVEN.

20 WAP TO CHECK MALE OR FEMALE.

21 WAP TO CHECK GIVEN NO IS POSSITIVE OR NEGETIVE OR ZERO.

22 WAP TO FIND MAXIMUM NO IN TO THREE NOS. 23 WAP TO CALCULATE TOTAL AVERAGE AND GRADE. 24 MENU DRIVEN PROGRAM.

25 WAP OF KBC.

26 WAP TO CHECK GIVEN CHARACTER IS VOWELS OR NOT.

27 WRITE A MENU DRIVEN PROGRAM TO CALCULATE MATHEMETICAL OPERATION.

28

C LANGUAGEWORKBOOK

Page 2 of 64

28 WAP TO PRINT ANY MESSAGE 5 TIMES. 30 29 WAP TO PRINT 1 TO 10 NOS. 31 30 WAP TO PRINT THE EVEN NOS BETWEEN 1 TO 40. 32 31 WAP TO PRINT THE ODD NOS BETWEEN 1 TO 40. 33 32 WAP TP PRINT ANY MESSAGE 5 TIMES USING DO 34

WHILE.

33 WAP TO PRINT EVEN NOS BETWEEN 1 TO 40. 35 34 WAP TO PRINT ODD NOS BETWEEN 1 TO 40 USING 36

DO WHILE LOOP.

35 ENTER 3 SUBJECTS MARK AND CALCULATE 37 TOTAL,AVERAGE AND GRADE. IF USER PRESS 'Y'

THEN CONTINUE OTHERWISE END OF PROGRAM.

36 WAP TO PRINT "*" 5 TIMES. 39 37 WAP TO PRINT 1 TO 10 NOS HORIZONTAL AND 40

VERTICAL.

38 WAP TO DISPLAY PATTERN. 41 39 WAP TO PRINT SPECTRRUM 5 TIMES. 42 40 WAP TO PRINT 5 TO 1. 43 41 WAP TO PRINT 1 TO 10 NOS AND IT'S SUM. 44 42 ENTER ANY NO AND DISPLAY IT'S TABLE. 45 43 WAP TO DISPLAY PATTERN. 46 44 WAP TO DISPLAY PATTERN. 47 45 WAP TO DISPLAY PATTERN. 48 46 WAP TO DISPLAY PATTERN. 49 47 WAP TO DISPLAY PATTERN. 50 48 ENTER ANY 10 NOS AND DISPLAY. 51 49 ENTER 10 NOS AND COUNTPOSSITIVE,NEGETIVE 52

AND ZERO.

50 ENTER ANY STRING AND DIPLAY IT ARRAY WISE. 53 51 ENTER ANY STRING AND FIND IT'S LENGTH. 54 52 ENTER STRING ANDCHARACTER AND CHECK HOW 55

MANY TIME CHARACTER IS REPETED.

53 DISPLAY 3*3 MATRIX. 56 54 SUM OF TWO MATRIX AND DISPLAY SUM IN 57

MATRIX.

55 TRANPOSE OF MATRIX. 59 56 FIND STRING LENGTH,COPY STRING,COMPARE 60

STRING,CONCATE STRING.

57 USE ALL STRING FUNCTIONS USING SWITCH CASE. 61

58 USE ALL STRING FUNCTIONS USING IF. 63

INTRODUCATION OF C

----------------------------------------------------------------------------------------------------------------------------- ----------------------

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to

develop the UNIX operating system at Bell Labs.

C was invented to write an operating system called UNIX.

The UNIX OS was totally written in C.

WHY C ?

--------------------------------------------------------------------------------------------------------------------------------------------------

Operating Systems

Language Compilers

Assemblers

Text Editors

Print Spoolers

Network Drivers

Modern Programs

Databases

Language Interpreters

Utilities

o A C program can vary from 3 lines to millions of lines and it should be written into one or more text

files with extension ".c"; for example, hello.c.

C PROGRAM BASICALLY CONSISTS OF THE FOLLOWING PARTS

--------------------------------------------------------------------------------------------------------------------------------------------------

Preprocessor Commands

Functions

Variables

Statements & Expressions

Comments

C Keyword:

--------------------------------------------------------------------------------------------------------------------------------------------------

auto else long switch

break enum register typedef

case extern return union

char float short unsigned

const for signed void

continue goto sizeof volatile

default if static while

do int struct _Packed

C LANGUAGEWORKBOOK

C LANGUAGEWORKBOOK

Page 3 of 64

EX:1 WRITE A PROGRAM TO PRINT HELLO WORLD. ANS:1

CODING

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

printf("HELLO WORLD");

getch();

}

OUTPUT:

HELLO WORLD

EX:2 WRITE A PROGRAM TO PRINT YOUR ADDRESS IN PARTICULAR FORMAT.

ANS:2

CODING

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

printf("\n SHIVAM APT");

printf("\n S B I ");

printf("\n SURAT : ");

getch();

}

OUTPUT:

SHIVAM APT

SBI

SURAT

C LANGUAGEWORKBOOK

Page 4 of 64

EX:3 WRITE A PROGRAM TO PRINT 1ST CHARACTER OF YOUR NAME. ANS:3

CODING

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

{ clrscr();

printf("\n ********"); printf("\n * *"); printf("\n ********"); printf("\n * *"); printf("\n * *");

getch(); }

OUTPUT:

******** * * ******** * * * *

C LANGUAGEWORKBOOK

Page 5 of 64

EX:4 WRITE A PROGRAM FOR ENTER ANY NO AND DISPLAY.

ANS:4 CODING

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

{

int a; clrscr();

printf("\n enter value of a:"); scanf("%d",&a);

printf("\n value of a is=%d",a); getch();

} OUTPUT:

enter value of a:6 value of a is=6

C LANGUAGEWORKBOOK

Page 6 of 64

EX:5 WRITE A PROGRAM TO PRINT ADDITION OF TWO NUMBER. ANS:5

CODING

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

{

int a,b,add; clrscr();

printf("\n enter value of a:"); scanf("%d",&a);

printf("\n enter value of b:"); scanf("%d",&b);

add=a+b;

printf("\n additon of two nos=%d",add); getch();

} OUTPUT:

enter value of a:10

enter value of b:15 additon of two nos=25

C LANGUAGE   WORKBOOK

Page 7 of 64


 

EX:6 WAP TO DISPLAY ANY TWO NOS. ANS:6

CODING

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

{

int a,b; clrscr();

printf("\n enter first no:"); scanf("%d",&a);

printf("\n enter second no:"); scanf("%d",&b);

printf("\n first number=%d",a); printf("\n second number=%d",b); getch();

} OUTPUT:

enter first no:20

enter second no:10 first number=20 second number=10

C LANGUAGEWORKBOOK

Page 8 of 64

EX:7 WRITE A PROGRAM TO PRINT MULTIPLICATION OF TWO NOS. ANS:7

CODING

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

{

int a,b,mul; clrscr();

printf("\n enter value of a:"); scanf("%d",&a);

printf("\n enter value of b:"); scanf("%d",&b);

mul=a*b;

printf("\n mul of two no is =%d",mul); getch();

}

OUTPUT:

enter value of a 10 enter value of b 5 mul of two no is =50

C LANGUAGEWORKBOOK

Page 9 of 64

EX:8 WAP TO SUBTRACT TWO NOS. ANS:8

CODING

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

{

int no,no1,sub; clrscr();

printf("\nEnter value of no:"); scanf("%d",&no); printf("\nenter value of no1:"); scanf("%d",&no1);

sub=no-no1;

printf("\n subtraction of value is=%d",sub); getch();

} OUTPUT:

Enter value of no:15

enter value of no1:10 subtraction of value is=10

C LANGUAGEWORKBOOK

Page 10 of 64

EX:9 WAP TO DIVISION OF TWO NOS. ANS:9

CODING

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

{

int a,b; float div; clrscr();

printf("\n enter value of a:"); scanf("%d",&a);

printf("\n enter value of b:"); scanf("%d",&b);

div=a/b;

printf("\n division=%f",div); getch();

}

OUTPUT:

enter value of a:15 enter value of b:5 division=3

C LANGUAGEWORKBOOK

Page 11 of 64

EX:10 WAP TO CALCULATE ADDITION,SUBTRACTION,MULTIPLICATION AND DIVISION IN SINGLE PROGRAM.

ANS:10 CODING

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

{

int no1,no2,add,sub,mul,div; clrscr();

printf("\n enter value of no1:"); scanf("%d",&no1);

printf("\n enter value of no2:"); scanf("%d",&no2); add=no1+no2;

sub=no1-no2; mul=no1*no2; div=no1+no2;

printf("\n addition=%d",add); printf("\n substraction=%d",sub); printf("\n multiplication=%d",mul); printf("\n division=%d",div); getch();

} OUTPUT:

enter value of no1:20 enter value of no2:10 addition=30 substraction=10 multiplication=200 division=10

C LANGUAGEWORKBOOK

Page 12 of 64

EX:11 WAP TO CONVERT FERANHIT TO CENTIGRATE. ANS:11

CODING

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

{

int c,f; clrscr();

printf("\nenter temprature in feranhit="); scanf("%d",&f);

c=(f-32)/2;

printf("\ncenyigrate value=%d",c); getch();

} OUTPUT:

enter temprature in feranhit=44 cenyigrate value=6

C LANGUAGEWORKBOOK

Page 13 of 64

EX:12 WAP TO CALCULATE SIMPLE INTEREST. ANS:12

CODING

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

{

int p,r,n; float i; clrscr();

printf("\nenter principle amount:"); scanf("%d",&p);

printf("\nenter rate:"); scanf("%d",&r); printf("\nenter year:"); scanf("%d",&n); i=(p*r*n)/100;

printf("\nsimple interest=%f",i); getch();

} OUTPUT:

enter principle amount:5000 enter rate:7

enter year:2

simple interest=44.000000

C LANGUAGEWORKBOOK

Page 14 of 64

EX:13 WAP TO FIND AREA OF TRIANGLE. ANS:13

CODING

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

{

int b,h,area; clrscr();

printf("\nenter base:"); scanf("%d",&b);

printf("\nenter vertical height:"); scanf("%d",&h);

area=(1*b*h)/2;

printf("\n area of triangle=%d",area); getch();

}

OUTPUT: enter base:5

enter vertical height:6 area of triangle=15

C LANGUAGEWORKBOOK

Page 15 of 64

EX:14 WAP TO FIND AREA OF RECTANGLE. ANS:14

CODING

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

{

int w,h,area; clrscr();

printf("enter width:"); scanf("%d",&w); printf("\n enter height:"); scanf("%d",&h); area=w*h;

printf("\n area of rectangle=%d",area); getch();

} OUTPUT:

enter width:6 enter height:5

area of rectangle=30

C LANGUAGEWORKBOOK

Page 16 of 64

EX:15 WAP TO CALCULATE AREA OF CIRCLE. ANS:15

CODING

#include<stdio.h> #include<conio.h> #define pi 3.14 void main()

{

int r;

float area; clrscr();

printf("\n Enter radious:"); scanf("%d",&r); area=pi*r*r;

printf("\n area=%f",area); getch();

}

OUTPUT:

Enter radious:44 area=6079.040039

C LANGUAGEWORKBOOK

Page 17 of 64

EX:16 WAP TO CHECK MAXIMUM NO IN TO TWO NOS. ANS:16

CODING

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

{

int a,b; clrscr();

printf("enter value of a:"); scanf("%d",&a);

printf("\n enter value of b:"); scanf("%d",&b);

if(a>b)

printf("\n a is max"); else

printf("\n b is maximum"); getch();

}

OUTPUT:

enter value of a:10 enter value of b:11 b is maximum

C LANGUAGEWORKBOOK

Page 18 of 64

EX:17 WAP TO CHECK GIVEN NO IS POSSITIVE OR NEGETIVE. ANS:17

CODING

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

{

int no; clrscr();

printf("enter any no:"); scanf("%d",&no); if(no>=0)

printf("\ngiven no is possitive"); else

printf("\ngiven no is negetive"); getch();

}

OUTPUT: enter any no:7

given no is possitive

C LANGUAGEWORKBOOK

Page 19 of 64

EX:18 WAP TO CHECK U R ELLIGIBLE FOR VOTE OR NOT. ANS:18

CODING

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

{

int age; clrscr();

printf("\n enter your age:"); scanf("%d",&age);

if(age>18)

printf("\n you are elligible for vote"); else

printf("\n you are not elligible for vote"); getch();

} OUTPUT:

enter your age:45

you are elligible for vote

C LANGUAGEWORKBOOK

Page 20 of 64

EX:19 WAP TO CHECK GIVEN NO IS ODD OR EVEN. ANS:19

CODING

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

{

int no; clrscr();

printf("\n enter any no:"); scanf("%d",&no); if(no%2==0)

printf("\n no is even"); else

printf("\n no is odd"); getch();

} OUTPUT:

enter any no:9

no is odd

C LANGUAGEWORKBOOK

Page 21 of 64

EX:20 WAP TO CHECK MALE OR FEMALE. ANS:20

CODING

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

{

char ch; clrscr();

printf("\n enter any character:"); scanf("%c",&ch);

if(ch=='f') {

printf("\n you are female"); }

else {

printf("\n you are male"); }

getch(); }

OUTPUT:

enter any character:f you are female

C LANGUAGEWORKBOOK

Page 22 of 64

EX:21 WAP TO CHECK GIVEN NO IS POSSITIVE OR NEGETIVE OR ZERO. ANS:21

CODING

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

{

int no; clrscr();

printf("\n enter any no:"); scanf("%d",&no); if(no==0)

printf("\n given no is zero"); else

if(no>0)

printf("\n given no is possitive"); else

printf("\n given no is negetive"); getch();

} OUTPUT:

enter any no:0 given no is zero

C LANGUAGEWORKBOOK

Page 23 of 64

EX:22 WAP TO FIND MAXIMUM NO IN TO THREE NOS.

ANS:22 CODING

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

{

int a,b,c; clrscr();

printf("\n enter first no:"); scanf("%d",&a);

printf("\n enter second no:"); scanf("%d",&b);

printf("\n enter third no:"); scanf("%d",&c);

if(a>b && a>c) printf("\n a is max");

else

if(b>a && b>c)

printf("\n b is maximum"); else

printf("\n c is maximum"); getch();

}

OUTPUT:

enter first no:75 enter second no:87 enter third no:44

b is maximum

C LANGUAGEWORKBOOK

Page 24 of 64

EX:23 WAP TO CALCULATE TOTAL AVERAGE AND GRADE. ANS:23

CODING

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

{

int mat,sci,eng,tot; float avg;

clrscr();

printf("\n enter marks of maths: "); scanf("%d",&mat);

printf("\n enter marks of science:"); scanf("%d",&sci);

printf("\n enter marks of english:"); scanf("%d",&eng); tot=mat+sci+eng;

printf("\n total=%d",tot); avg=tot/3;

printf("\n average=%f",avg); if(avg>=80)

printf("\n grade=A+"); else

if(avg>=70) printf("\ngrade=A");

else if(avg>=60) printf("\n grade=B+");

else if(avg>=50) printf("\n grade=B");

else if(avg>=40) printf("\ngrade=C");

getch(); }

OUTPUT:

enter marks of maths:85 enter marks of science:90 enter marks of english:75 total=250 average=83.333333 grade=A+

C LANGUAGEWORKBOOK

Page 25 of 64

EX:24 MENU DRIVEN PROGRAM. ANS:24

CODING

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

{

char ch; clrscr();

printf("\n press m for GOOD MORNING"); printf("\n press a for GOOD AFTERNOON"); printf("\n press e for GOOD EVENING"); printf("\n press n for GOOD NIGHT"); printf("\n enter your choice:-"); scanf("%c",&ch);

switch(ch) {

case 'm':

printf("\nGOOD MORNING"); break;

case 'a':

printf("\nGOOD AFTERNOON"); break;

case 'e':

printf("\nGOOD EVENING"); break;

case 'n':

printf("\nGOOD NIGHT"); break;

default:

printf("\nOOPS! NOT DECLAREd"); }

getch();

} OUTPUT:

press m for GOOD MORNING press a for GOOD AFTERNOON press e for GOOD EVENING press n for GOOD NIGHT

enter your choice:m GOOD MORNING

C LANGUAGEWORKBOOK

Page 26 of 64

EX:25 WAP OF KBC. ANS:25

CODING

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

{

int no; clrscr();

printf("\n WHAT IS A FULL FORM OF C.P.U"); printf("\n 1. central processing unit"); printf("\n 2. control processing unit"); printf("\n 3. cellular processing unit"); printf("\n enter your choice:"); scanf("%d",&no);

switch(no) {

case 1:

printf("\n write answer"); break;

case 2:

printf("\n wrong answer"); break;

case 3:

printf("\n wrong answer"); break;

default:

printf("\n wrong amswer"); }

getch(); }

OUTPUT:

WHAT IS A FULL FORM OF C.P.U 1. central processing unit 2. control processing unit 3. cellular processing unit

enter your choice:2 wrong answer

C LANGUAGEWORKBOOK

Page 27 of 64

EX:26 WAP TO CHECK GIVEN CHARACTER IS VOWELS OR NOT. ANS:26

CODING

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

{

char ch; clrscr();

printf("\n enter any character:-"); scanf("%c",&ch);

switch(ch) {

case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U':

printf("\n This character is vowels"); break;

default:

printf("\n This character is not vowels"); }

getch(); }

OUTPUT:

enter any character:-C

This character is not vowels

C LANGUAGEWORKBOOK

Page 28 of 64

EX:27 WRITE A MENU DRIVEN PROGRAM TO CALCULATE MATHEMETICAL OPERATION.

ANS:27 CODING

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

{

int a,b,c,ch; clrscr();

printf("\n1. addition"); printf("\n2. subtraction"); printf("\n3. multiplication"); printf("\n4. division"); printf("\nenter value of a:"); scanf("%d",&a); printf("\nenter value of b:"); scanf("%d",&b); printf("\nenter your choice:"); scanf("%d",&ch);

switch(ch) {

case 1: c=a+b;

printf("\naddition=%d",c); break;

case 2: c=a-b;

printf("\nsubtraction=%d",c); break;

case 3: c=a*b;

printf("\nmultiplication=%d",c); break;

case 4: c=a/b;

printf("\ndivision=%d",c); break;

default:

printf("\n no not found"); }

getch(); }

C LANGUAGEWORKBOOK

Page 29 of 64

OUTPUT:

1. Addition

2. Subtraction 3. Multiplication 4. Division

enter value of a:50 enter value of b:20 enter your choice:3 multiplication=1000

C LANGUAGEWORKBOOK

Page 30 of 64

EX:28 WAP TO PRINT ANY MESSAGE 5 TIMES. ANS:28

CODING

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

{

int i=1; clrscr(); while(i<=5)

{ printf("spectrrum"); printf("\n");

i++; }

getch(); }

OUTPUT: Spectrrum Spectrrum Spectrrum Spectrrum Spectrrum

C LANGUAGEWORKBOOK

Page 31 of 64

EX:29 WAP TO PRINT 1 TO 10 NOS. ANS:29

CODING

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

{

int i=1; clrscr(); while(i<=10)

{ printf("%d",i); printf("\n"); i++;

} getch();

}

OUTPUT: 1

2 3 4 5 6 7 8 9 10

C LANGUAGEWORKBOOK

Page 32 of 64

EX:30 WAP TO PRINT THE EVEN NOS BETWEEN 1 TO 40 ANS:30

CODING

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

{

int i=2; clrscr(); while(i<=40)

{ printf("%d",i); printf("\n"); i+=2;

} getch();

} OUTPUT: 2

4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

C LANGUAGEWORKBOOK

Page 33 of 64

EX:31 WAP TO PRINT THE ODD NOS BETWEEN 1 TO 40. ANS:31

CODING

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

{

int i=1; clrscr(); while(i<=40)

{ printf("%d",i); printf("\n"); i+=2;

} getch();

} OUTPUT: 1

3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39

C LANGUAGEWORKBOOK

Page 34 of 64

EX:32 WAP TP PRINT ANY MESSAGE 5 TIMES USING DO WHILE. ANS:32

CODING

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

{

int i=1; clrscr(); do

{ printf("spectrrum"); printf("\n");

i++; }

while(i<=5); getch();

} OUTPUT:

Spectrrum Spectrrum Spectrrum Spectrrum Spectrrum

C LANGUAGEWORKBOOK

Page 35 of 64

EX:33 WAP TO PRINT EVEN NOS BETWEEN 1 TO 40. ANS:33

CODING

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

{

int i=2; clrscr(); do

{ printf("%d",i); printf("\n"); i+=2;

} while(i<=40);

} OUTPUT: 2

4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

C LANGUAGEWORKBOOK

Page 36 of 64

EX:34 WAP TO PRINT ODD NOS BETWEEN 1 TO 40 USING DO WHILE LOOP.

ANS:34 CODING

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

{

int i=1; clrscr();

do {

printf("%d",i); printf("\n"); i+=2;

} while(i<=40);

getch(); }

OUTPUT: 1

3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39

C LANGUAGEWORKBOOK

Page 37 of 64

EX:35 ENTER 3 SUBJECTS MARK AND CALCULATE TOTAL,AVERAGE AND GRADE. IF USER PRESS 'Y' THEN CONTINUE OTHERWISE END OF PROGRAM. ANS:35

CODING

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

{

int m1,m2,m3,tot; char grade,yn; float per;

clrscr(); do

{

printf("\n enter first subject marks:"); scanf("%d",&m1);

printf("\n enter second subject marks:"); scanf("%d",&m2);

printf("\n enter third subject marks:"); scanf("%d",&m3);

tot=m1+m2+m3; per=tot/3; if(per>=70)

grade='A'; else

if(per>=60) grade='B';

else if(per>=50)

grade='C'; else

if(per>=35) grade='D';

else grade='E';

printf("\n total marks=%d",tot); printf("\n percentage=%f",per); printf("\n grade=%c",grade);

printf("\n do you want to continue?(y/n):”); yn=getch();

}

while(yn=='y' || yn=='Y');

C LANGUAGEWORKBOOK

Page 38 of 64

printf("\n end of the program"); getch();

}

OUTPUT:

enter first subject marks:80 enter second subject marks:80 enter third subject marks:80 total marks=240 percentage=80.000000 grade=A

do you want to continue?(y/n):N

C LANGUAGEWORKBOOK

Page 39 of 64

EX:36 WAP TO PRINT "*" 5 TIMES. ANS:36

CODING

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

{

int i; clrscr();

for(i=0;i<=4;i++) {

printf(" * "); }

getch(); }

OUTPUT: * * * * *

C LANGUAGEWORKBOOK

Page 40 of 64

EX:37 WAP TO PRINT 1 TO 10 NOS HORIZONTAL AND VERTICAL. ANS:37

CODING

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

{

int i; clrscr();

for(i=1;i<=10;i++) {

printf("\n %d",i); }

Printf(“\n”); for(i=1;i<=10;i++)

{

printf(" %d ",i); }

getch(); }

OUTPUT: 1

2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

C LANGUAGEWORKBOOK

Page 41 of 64

EX:38 WAP TO DISPLAY PATTERN. ANS:38

CODING

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

{

int i,j; clrscr();

for(i=0;i<=4;i++) {

for(j=0;j<=4;j++) {

printf("* "); } printf("\n");

} getch();

} OUTPUT: * * * * * * * * * * * * * * * * * * * * * * * * *

C LANGUAGEWORKBOOK

Page 42 of 64

EX:39 WAP TO PRINT SPECTRRUM 5 TIMES. ANS:39

CODING

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

{

int i; clrscr();

for(i=1;i<=5;i++) {

printf("spectrrum"); printf("\n");

} getch();

} OUTPUT:

Spectrrum Spectrrum Spectrrum Spectrrum Spectrrum

C LANGUAGEWORKBOOK

Page 43 of 64

EX:40 WAP TO PRINT 5 TO 1. ANS:40

CODING

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

{

int i; clrscr();

for(i=5;i>=1;i--) {

printf("\n%d",i); }

getch(); }

OUTPUT: 5

4 3 2 1

C LANGUAGEWORKBOOK

Page 44 of 64

EX:41 WAP TO PRINT 1 TO 10 NOS AND IT'S SUM. ANS:41

CODING

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

{

int i,sum=0; clrscr(); for(i=1;i<=10;i++)

{ printf("\n%d",i); sum=sum+i;

} printf("\n");

printf("sum=%d",sum); getch();

} OUTPUT: 1

2 3 4 5 6 7 8 9 10

sum=55

C LANGUAGEWORKBOOK

Page 45 of 64

EX:42 ENTER ANY NO AND DISPLAY IT'S TABLE. ANS:42

CODING

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

{

int no,i; clrscr();

printf("enter any no:"); scanf("%d",&no);

for(i=1;i<=10;i++) {

printf("\n%d * %d = %d",no,i,no*i); }

getch(); }

OUTPUT:

enter any no:7 7 * 1 = 7

7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70

C LANGUAGEWORKBOOK

Page 46 of 64

EX:43 WAP TO DISPLAY PATTERN. ANS:43

CODING

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

{

int i,j; clrscr();

for(i=1;i<=5;i++) {

for(j=1;j<=i;j++) {

printf(" 1 "); }

printf("\n"); }

getch(); } OUTPUT: 1

1 1

1 1 1 1 1 1 1

1 1 1 1 1

C LANGUAGEWORKBOOK

Page 47 of 64

EX:44 WAP TO DISPLAY PATTERN. ANS:44

CODING

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

{

int i,j; clrscr();

for(i=1;i<=5;i++) {

for(j=1;j<=i;j++) {

printf(" * "); }

printf("\n"); }

getch(); } OUTPUT: *

* * * * *

* * * * * * * * *

C LANGUAGEWORKBOOK

Page 48 of 64

EX:45 WAP TO DISPLAY PATTERN. ANS:45

CODING

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

{

int i,j; clrscr();

for(i=1;i<=5;i++) {

for(j=1;j<=i;j++) {

printf(" %d ",j); }

printf("\n"); }

getch(); } OUTPUT: 1

1 2

1 2 3 1 2 3 4

1 2 3 4 5

C LANGUAGEWORKBOOK

Page 49 of 64

EX:46 WAP TO DISPLAY PATTERN. ANS:46

CODING

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

{

int i,j; clrscr();

for(i=1;i<=5;i++) {

for(j=5;j>=i;j--) {

printf(" * "); }

printf("\n"); }

getch(); } OUTPUT:

* * * * * * * * * * * *

* * *

C LANGUAGEWORKBOOK

Page 50 of 64

EX:47 WAP TO DISPLAY PATTERN. ANS:47

CODING

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

{

int i,j; clrscr();

for(i=5;i>=1;i--) {

for(j=1;j<=i;j++) {

printf(" %d ",j); }

printf("\n"); }

getch(); }

OUTPUT: 1 2 3 4 5 1 2 3 4

1 2 3 1 2

1

C LANGUAGEWORKBOOK

Page 51 of 64

EX:48 ENTER ANY 10 NOS AND DISPLAY. ANS:48

CODING

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

{

int i,no[10]; clrscr();

for(i=1;i<=10;i++) {

printf("\n Enter no:"); scanf("%d",&no[i]);

} for(i=1;i<=10;i++)

{ printf("\n%d",no[i]);

} getch();

} OUTPUT:

Enter no:96 Enter no:8 Enter no:45 Enter no:78 Enter no:74 Enter no:85 Enter no:63 Enter no:52 Enter no:41 Enter no:45 96

8 45 78 74 85 63 52 41 45

C LANGUAGEWORKBOOK

Page 52 of 64

EX:49 ENTER 10 NOS AND COUNTPOSSITIVE,NEGETIVE AND ZERO. ANS:49

CODING

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

{

int no[10],i,pc=0,nc=0,zc=0; clrscr();

for(i=1;i<10;i++) {

printf("\n enter value:"); scanf("%d",&no[i]); if(no[i]==0)

zc++; else if(no[i]>0)

pc++; else

nc++; }

printf("\ntotal zero=%d",zc); printf("\ntotal possitive=%d",pc); printf("\ntotal negetive=%d",nc); getch();

} OUTPUT:

enter value:0 enter value:3 enter value:-9 enter value:-9 enter value:8 enter value:0 enter value:0 enter value:78 enter value:-96 enter value:74 total zero=3 total possitive=4 total negetive=3

C LANGUAGEWORKBOOK

Page 53 of 64

EX:50 ENTER ANY STRING AND DIPLAY IT ARRAY WISE. ANS:50

CODING

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

{

char str[10]; int i; clrscr();

printf("enter any string:"); scanf("%s",str); for(i=0;str[i]!='\0';i++)

{

printf("\n str[%d]=%c",i,str[i]); }

getch(); }

OUTPUT:

enter any string:HELLO str[o]=H

str[1]=E str[2]=L str[3]=L str[4]=O

C LANGUAGEWORKBOOK

Page 54 of 64

EX:51 ENTER ANY STRING AND FIND IT'S LENGTH. ANS:51

CODING

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

{

char str[10]; int l=0,i; clrscr();

printf("enter any string:"); scanf("%s",str);

for(i=0;str[i]!='\0';i++) {

l=l+1; }

printf("\n length=%d",l); getch();

}

OUTPUT:

enter any string:SPECTRRU length=9

C LANGUAGEWORKBOOK

Page 55 of 64

EX:52 ENTER STRING ANDCHARACTER AND CHECK HOW MANY TIME CHARACTER IS REPETED.

ANS:52 CODING

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

{

char str[20],ch; int i,count=0; clrscr();

printf("\n enter any string:"); scanf("%s",str);

printf("\n enter any character"); scanf("\n %c",&ch); for(i=0;str[i]!='\0';i++)

{

if(str[i]==ch) {

count++; }

}

printf("\n %c is repeted=%d times",ch,count); getch();

} OUTPUT:

enter any string:SPECTRRUM enter any character:R

R is repeted 2 times

C LANGUAGEWORKBOOK

Page 56 of 64

EX:53 DISPLAY 3*3 MATRIX. ANS:53

CODING

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

{

int i,j,no[3][3]; clrscr(); for(i=0;i<=2;i++)

{ for(j=0;j<=2;j++)

{

printf("enter no"); scanf("%d",&no[i][j]); }

} for(i=0;i<=2;i++)

{ for(j=0;j<=2;j++)

{

printf(" %d ",no[i][j]); }

printf("\n"); }

getch(); }

OUTPUT: enter no 1 enter no 2 enter no 3 enter no 4 enter no 5 enter no 6 enter no 7 enter no 8 enter no 9 1 2 3

4 5 6 7 8 9

C LANGUAGEWORKBOOK

Page 57 of 64

EX:54 SUM OF TWO MATRIX AND DISPLAY SUM IN MATRIX. ANS:54

CODING

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

{

int a[3][3],b[3][3],c[3][3],i,j; clrscr();

for(i=0;i<=2;i++) {

for(j=0;j<=2;j++) {

printf("\n enter value of first matrix"); scanf("%d",&a[i][j]);

} }

for(i=0;i<=2;i++) {

for(j=0;j<=2;j++) {

printf("\n enter value of second matrix"); scanf("%d",&b[i][j]);

} }

for(i=0;i<=2;i++) {

for(j=0;j<=2;j++) {

c[i][j]=a[i][j]+b[i][j]; printf(" %d ",c[i][j]);

} printf("\n");

} }

OUTPUT:

enter value of first matrix 1

enter value of first matrix 2 enter value of first matrix 3 enter value of first matrix 4 enter value of first matrix 5

C LANGUAGEWORKBOOK

Page 58 of 64

enter value of first matrix 6 enter value of first matrix 7 enter value of first matrix 8 enter value of first matrix 9 enter value of second matrix 9 enter value of second matrix 8 enter value of second matrix 7 enter value of second matrix 6 enter value of second matrix 5 enter value of second matrix 4 enter value of second matrix 3 enter value of second matrix 2 enter value of second matrix 1 9 9 9

9 9 9 9 9 9

C LANGUAGEWORKBOOK

Page 59 of 64

EX:55 TRANPOSE OF MATRIX. ANS:55

CODING

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

{

int a[3][3],b[3][3],i,j; clrscr(); for(i=0;i<=2;i++)

{ for(j=0;j<=2;j++)

{

printf("enter value of first matrix:"); scanf("%d",&a[i][j]);

} }

printf("\n after tranpose "); printf("\n"); for(i=0;i<=2;i++)

{ for(j=0;j<=2;j++)

{ b[i][j]=a[j][i];

printf(" %d ",b[i][j]); }

printf("\n"); } }

OUTPUT:

enter value of first matrix:1

enter value of first matrix:2 enter value of first matrix:3 enter value of first matrix:4 enter value of first matrix:5 enter value of first matrix:6 enter value of first matrix:7 enter value of first matrix:8 enter value of first matrix:9 1 4 7

2 5 8 3 6 9

C LANGUAGEWORKBOOK

Page 60 of 64

EX:56 FIND STRING LENGTH,COPY STRING,COMPARE STRING,CONCATE STRING.

ANS:56 CODING

#include<stdio.h> #include<conio.h> #include<string.h> void main()

{

char str1[10],str2[10],str3[10]; int len1,len2,diff;

clrscr();

printf("enter first string:"); scanf("%s",str1); printf("enter second string:"); scanf("%s",str2); len1=strlen(str1); len2=strlen(str2);

printf("\n LENGTH OF FIRST STRING IS=%d",len1); printf("\n LENGTH OF SECOND STRInG IS=%d",len2); strcpy(str3,str1);

printf("\n COPIED STRING IS=%s",str3); diff=strcmp(str1,str2);

if(diff==0)

printf("\n both string is equal");

else printf("\n both string is different"); strcat(str1,str2);

printf("\n concate string is=%s",str1); getch(); }

OUTPUT:

enter first string:SPECTRRUM

enter second string:INSTITUTE LENGTH OF FIRST STRING IS=9 LENGTH OF SECOND STRInG IS=9 COPIED STRING IS= SPECTRRUM both string is different

concate string is= SPECTRRUMINSTITUTE

C LANGUAGEWORKBOOK

Page 61 of 64

EX:57 USE ALL STRING FUNCTIONS USING SWITCH CASE. ANS:57

CODING

#include<stdio.h> #include<conio.h> #include<string.h> void main()

{

char str1[10],str2[10],str3[10]; int len1,len2,diff,ch;

clrscr();

printf("\n ****************************************"); printf("\n 1. count the lenth");

printf("\n 2. copy string"); printf("\n 3. compare both string");

printf("\n 4. conconate both string"); printf("\n 5. exit");

printf("\n ****************************************"); printf("\nenter first string:");

scanf("%s",str1); printf("\nenter second string:"); scanf("%s",str2);

printf("\n enter your choice"); scanf("%d",&ch);

switch(ch) {

case 1: {

len1=strlen(str1); len2=strlen(str2);

printf("\n LENGTH OF FIRST STRING IS=%d",len1); printf("\n LENGTH OF SECOND STRInG IS=%d",len2); break;

}

case 2: {

strcpy(str3,str1);

printf("\n COPIED STRING IS=%s",str3); break;

}

case 3: {

C LANGUAGEWORKBOOK

Page 62 of 64

diff=strcmp(str1,str2); if(diff==0)

{

printf("\n both string is equal"); }

else {

printf("\n both string is different"); }

break; }

case 4: {

strcat(str1,str2);

printf("\n concate string is=%s",str1); break;

} default: {

printf("\n no not found"); break;

} }

getch(); }

OUTPUT: ****************************************

1. count the lenth 2. copy string

3. compare both string 4. conconate both string 5. exit

****************************************

enter first string:SPECTRRUM enter second string:INSTITUTE LENGTH OF FIRST STRING IS=9 LENGTH OF SECOND STRInG IS=9

C LANGUAGEWORKBOOK

Page 63 of 64

EX:58 USE ALL STRING FUNCTIONS USING SWITCH CASE. ANS:58

CODING

#include<stdio.h> #include<conio.h> #include<string.h> void main()

{

char str1[10],str2[10],str3[10]; int len1,len2,diff,ch;

clrscr();

printf("\n ****************************************"); printf("\n 1. count the lenth");

printf("\n 2. copy string"); printf("\n 3. compare both string");

printf("\n 4. conconate both string"); printf("\n 5. exit");

printf("\n ****************************************"); printf("\nenter first string:");

scanf("%s",str1); printf("\nenter second string:"); scanf("%s",str2);

printf("\n enter your choice"); scanf("%d",&ch);

if(ch==1) {

len1=strlen(str1); len2=strlen(str2);

printf("\n LENGTH OF FIRST STRING IS=%d",len1); printf("\n LENGTH OF SECOND STRInG IS=%d",len2);

} else

if(ch==2) {

strcpy(str3,str1);

printf("\n COPIED STRING IS=%s",str3); }

else if(ch==3) {

diff=strcmp(str1,str2); if(diff==0)

{

C LANGUAGEWORKBOOK

Page 64 of 64

printf("\n both string is equal"); }

else {

printf("\n both string is different"); }

} else {

strcat(str1,str2);

printf("\n concate string is=%s",str1); }

getch(); }

OUTPUT: ****************************************

1. count the lenth 2. copy string

3. compare both string 4. conconate both string 5. exit

****************************************

enter first string:SPECTRRUM enter second string:INSTITUTE enter your choice:4

concate string is=SPECTRRUMINSTITUTE

 

 

0 Comments: