You are on page 1of 26

12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.

1 // Name : Dattatray Kashinath Awchar

2 // Roll No. : S-2100247

3 // Assignment no. : 2 of C programming

5 /* Q.1) Write a 'C' program to calculate the Area and circumference of a circle. */

6 #include <stdio.h>

8 void main()

9 {

10 float r, c, a;

11

12 printf("Enter radius = ");

13 scanf("%f", &r);

14

15 float pi = 3.14;

16

17 c = 2 * pi * r;

18 a = pi * r * r;

19

20 printf("\nArea of a circle = %f", a);

21 printf("\nCircumference of a circle = %f", c);

22 }

23

24 /* Output of Q.1 ::

25 Enter radius = 5.6

26

27 Area of a circle = 98.470398

28 Circumference of a circle = 35.167999

29 */

30

31 /***************************************************************************************/

32

33 /* Q.2) Write a 'C' program to calculate simple interest. */

34 #include <stdio.h>

35

36 int main()

37 {

38 float principle, time, rate, SI;

39

40 printf("Enter principle (amount) : ");

41 scanf("%f", &principle);

42

43 printf("Enter time(Years) : ");

44 scanf("%f", &time);

45

46 printf("Enter rate(%%) : ");

47 scanf("%f", &rate);

48

49 SI = (principle * time * rate) / 100;

50

51 printf("Simple Interest = %f", SI);

52

53 return 0;

54 }

55

56 /* Output of Q.2 ::

57 Enter principle (amount) : 720000

localhost:64631 1/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
58 Enter time(Years) : 2

59 Enter rate(%) : 12

60 Simple Interest = 172800.000000

61 */

62

63 /***************************************************************************************/

64

65 /* Q.3) Write a 'C' program to convert temperature from degree centigrade to Fahrenheit. */

66 #include <stdio.h>

67

68 int main()

69 {

70 float fahra, cel;

71

72 printf("Temperature in degree Centigrade : ");

73 scanf("%f", &cel);

74

75 fahra = (1.8 * cel) + 32.0;

76

77 printf("\nTemperature in Fahrenheit: %.2f F\n", fahra);

78

79 return 0;

80 }

81

82 /* Output of Q.3 ::

83 Temperature in degree Centigrade : 32

84

85 Temperature in Fahrenheit: 89.60 F

86 */

87

88 /***************************************************************************************/

89

90 /* Q.4) Write a 'C' program to calculate sum of 5 subjects and find percentage. */

91 #include <stdio.h>

92

93 int main()

94 {

95

96 float phy, chem, math, bio, eng;

97 float total, average, percentage;

98

99 printf("Enter marks in Physics : ");

100 scanf("%f", &phy);

101

102 printf("Enter marks in Chemistry : ");

103 scanf("%f", &chem);

104

105 printf("Enter marks in Mathematics : ");

106 scanf("%f", &math);

107

108 printf("Enter marks in Biology : ");

109 scanf("%f", &bio);

110

111 printf("Enter marks in English : ");

112 scanf("%f", &eng);

113

114 total = phy + chem + math + bio + eng;

localhost:64631 2/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

115 percentage = (total / 500.0) * 100;

116

117 printf("Total marks = %.2f\n", total);

118 printf("Percentage = %.2f", percentage);

119

120 return 0;

121 }

122

123 /* Output of Q.4 ::

124 Enter marks in Physics : 98

125 Enter marks in Chemistry : 96

126 Enter marks in Mathematics : 91

127 Enter marks in Biology : 82

128 Enter marks in English : 99

129 Total marks = 466.00

130 Percentage = 93.20

131 */

132

133 /***************************************************************************************/

134

135 /* Q.5) Write a 'C' program to swap two integers using temporary variable. */

136 #include <stdio.h>

137

138 int main()

139 {

140 int x, y, temp;

141

142 printf("Enter the value of x : ");

143 scanf("%d", &x);

144

145 printf("Enter the value of y : ");

146 scanf("%d", &y);

147

148 temp = x;

149 x = y;

150 y = temp;

151

152 printf("After swapping :\n x=%d\n y=%d", x, y);

153 return 0;

154 }

155

156 /* Output of Q.5 ::

157 Enter the value of x : 14

158 Enter the value of y : 18

159 After swapping :

160 x=18

161 y=14

162 */

163

164 /***************************************************************************************/

165

166 /* Q.6) Write a 'C' program to find minimum of three numbers. */

167 #include <stdio.h>

168

169 int main()

170 {

171 int x, y, z;

localhost:64631 3/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
172

173 printf("Enter Number X : ");

174 scanf("%d", &x);

175

176 printf("Enter Number Y : ");

177 scanf("%d", &y);

178

179 printf("Enter Number Z : ");

180 scanf("%d", &z);

181

182 if ((x < y) && (x < z))

183 printf("\n x is Smallest Number : %d", x);

184 else

185 {

186 if (y < z)

187 printf("\n y is Smallest Number : %d", y);

188 else

189 printf("\n z is Smallest Number : %d", z);

190 }

191 return (0);

192

193 }Biggest

194

195 /* Output of Q.6 ::

196 Enter Number X : 12

197 Enter Number Y : 45

198 Enter Number Z : 61

199

200 x is Smallest Number : 12

201 */

202

203 /***************************************************************************************/

204

205 /* Q.7) Write a 'C' program to find maximum of three numbers. */

206 #include <stdio.h>

207

208 int main()

209 {

210 int x, y, z;

211

212 printf("Enter Number X : ");

213 scanf("%d", &x);

214

215 printf("Enter Number Y : ");

216 scanf("%d", &y);

217

218 printf("Enter Number Z : ");

219 scanf("%d", &z);

220

221 if ((x > y) && (x > z))

222 printf("\n x is Biggest Number : %d", x);

223

224 else

225 {

226 if (y > z)

227 printf("\n y is Biggest Number : %d", y);

228 else

localhost:64631 4/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
229 printf("\n z is Biggest Number : %d", z);

230 }

231

232 return (0);

233 }

234

235 /* Output of Q.7 ::

236 Enter Number X : 25

237 Enter Number Y : 15

238 Enter Number Z : 20

239

240 x is Biggest Number : 25

241 */

242

243 /***************************************************************************************/

244

245 /* Q.8) Write a 'C' program to swap two integers without using temporary variable. */

246 #include <stdio.h>

247

248 int main()

249 {

250 int a, b;

251

252 printf("Enter Number a : ");

253 scanf("%d", &a);

254

255 printf("Enter Number b : ");

256 scanf("%d", &b);

257

258 a = a + b;

259 b = a - b;

260 a = a - b;

261

262 printf("\nAfter swap : a=%d , b=%d", a, b);

263

264 return 0;

265 }

266

267 /* Output of Q.8 ::

268 Enter Number a : 21

269 Enter Number b : 18

270

271 After swap : a=18 , b=21

272 */

273

274 /***************************************************************************************/

275

276 /* Q.9) Write a 'C' program to accept character & display its ASCII value and its next &

277 previous character. */

278 #include <stdio.h>

279

280 int main()

281 {

282 char datta;

283

284 printf("Enter one character : ");

285 scanf("%c", &datta);

localhost:64631 5/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
286

287 printf("\nYou entered character is : %c\n", datta);

288 printf("Previous character is : %c\n", datta - 1);

289 printf("Next character is : %c\n", datta + 1);

290 }

291

292 /* Output of Q.9 ::

293 Enter one character : g

294

295 You entered character is : g

296 Previous character is : f

297 Next character is : h

298 */

299

300 /***************************************************************************************/

301

302 /* Q.10) Write a 'C' program for a menu driven program which has following options:

303 1. Factorial of a number.

304 2. Odd or even

305 3. Exit */

306 #include <stdio.h>

307 #include <conio.h>

308

309 int main()

310 {

311 int c = 0, num, res, n;

312

313 while (c != 4)

314 {

315 printf("\n1. Factorial of a number\n2. Odd or even\n3. Exit\n");

316 printf("\nEnter your choice : ");

317 scanf("%d", &c);

318

319 switch (c)

320 {

321 case 1:

322 printf("Enter Number : ");

323 scanf("%d", &num);

324 n = num;

325 res = num;

326 while (num > 1)

327 {

328 res = res * (num - 1);

329 num = num - 1;

330 }

331 printf("\nFactorial of %d is %d. \n", n, res);

332 break;

333

334 case 2:

335 printf("Enter an integer : ");

336 scanf("%d", &num);

337 n = num;

338

339 if (num % 2 == 0)

340 printf("\n%d is Even Number.\n\n", n);

341 else

342 printf("\n%d is Odd Number.\n\n", n);

localhost:64631 6/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

343 break;

344

345 case 3:

346 printf("\nExit");

347 break;

348 }

349 }

350 }

351

352 /* Output of Q.10 ::

353 1. Factorial of a number

354 2. Odd or even

355 3. Exit

356

357 Enter your choice : 3

358

359 Exit

360 1. Factorial of a number

361 2. Odd or even

362 3. Exit

363

364 Enter your choice : 2

365 Enter an integer : 52

366

367 52 is Even Number.

368

369

370 1. Factorial of a number

371 2. Odd or even

372 3. Exit

373

374 Enter your choice :

375 */

376

377 /***************************************************************************************/

378

379 /* Q.11) Write a 'C' program to calculate the sum of first n even numbers. */

380 #include <stdio.h>

381

382 int main()

383 {

384 int i = 1, n, p = 0;

385

386 printf("Enter max number n : ");

387 scanf("%d", &n);

388

389 printf("\nEven numbers in between 1 to %d is :\n\n", n);

390

391 for (i = 1; i <= n; i++)

392 {

393 if (i % 2 == 0)

394 {

395 printf("%d ", i);

396 p = p + i;

397 }

398 }

399

localhost:64631 7/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

400 printf("\n\nSum of even numbers in between 1 to %d is = %d", n, p);

401 return 0;

402 }

403

404 /* Output of Q.11 ::

405 Enter max number n : 54

406

407 Even numbers in between 1 to 54 is :

408

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

410

411 42 44 46 48 50 52 54

412

413 Sum of even numbers in between 1 to 54 is = 756

414 */

415

416 /***************************************************************************************/

417

418 /* Q.12) Write a 'C' program to print the ASCII value of any given character */

419 #include <stdio.h>

420

421 int main()

422 {

423 char c;

424

425 printf("Enter a character : ");

426 scanf("%c", &c);

427

428 printf("ASCII value of %c = %d", c, c);

429

430 return 0;

431 }

432

433 /* Output of Q.12 ::

434 Enter a character : g

435 ASCII value of g = 103

436 */

437

438 /***************************************************************************************/

439

440 /* Q.13) Write a 'C' program, which accepts a character from the user and checks if it

441 is an alphabet, digit or symbol. If it is an alphabet, check if it is uppercase or

442 lowercase and then change the case. */

443 #include <stdio.h>

444

445 int main()

446 {

447 char a;

448

449 printf("Enter Character : ");

450 scanf("\n %c", &a);

451

452 if ((a >= 65 && a <= 90) || (a >= 97 && a <= 122))

453 {

454 printf("\nIt is an Alphabet");

455 if (isupper(a))

456 {

localhost:64631 8/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

457 a = tolower(a);

458 printf("%c", a);

459 }

460 else

461 {

462 a = toupper(a);

463 printf("%c", a);

464 }

465 }

466

467 if (a >= 48 && a <= 57)

468 {

469 printf("\n'%c' is Digit", a);

470 }

471

472 if ((a >= 58 && a <= 64) || (a >= 91 && a <= 96) ||

473 (a >= 33 && a <= 47) || (a >= 123 && a <= 126))

474 {

475 printf("\n'%c' is Punctuation Symbol.", a);

476 }

477

478 return 0;

479 }

480

481 /* Output of Q.13 ::

482 Enter Character : &

483

484 '&' is Punctuation Symbol.

485 */

486

487 /***************************************************************************************/

488

489 /* Q.14) Write a 'C' program to take input student age and check whether given student

490 can vote or not. */

491 #include<stdio.h>

492

493 int main()

494 {

495 int datta ;

496

497 printf("Enter your age : ");

498 scanf("%d",&datta);

499

500 if (datta>=18)

501 {

502 printf("You are eligibal for voting.");

503 }

504

505 else

506 {

507 printf("You are not eligibal for voting.\n");

508 }

509

510 return 0;

511 }

512

513 /* Output of Q.14;

localhost:64631 9/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

514 Enter your age : 20

515 You are eligibal for voting.

516 */

517

518 /***************************************************************************************/

519

520 /* Q.15) Write a menu-driven program using Switch case to calculate the following:

521 1. Area of circle

522 2. Area of square

523 3. Area of sphere */

524 #include <stdio.h>

525

526 int input();

527 void output(float);

528 int main()

529 {

530 float result;

531 int choice, num;

532

533 printf("Press 1 to calculate area of circle\n");

534 printf("Press 2 to calculate area of square\n");

535 printf("Press 3 to calculate area of sphere\n");

536 printf("Enter your choice : ");

537

538 choice = input();

539

540 switch (choice)

541 {

542 case 1:

543 {

544 printf("Enter radius : ");

545 num = input();

546 result = 3.14 * num * num;

547

548 printf("Area of circle = ");

549 output(result);

550

551 break;

552 }

553

554 case 2:

555 {

556 printf("Enter side of square : ");

557 num = input();

558 result = num * num;

559

560 printf("Area of square = ");

561 output(result);

562

563 break;

564 }

565

566 case 3:

567 {

568 printf("Enter radius : ");

569 num = input();

570 result = 4 * (3.14 * num * num);

localhost:64631 10/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

571

572 printf("Area of sphere = ");

573 output(result);

574

575 break;

576 }

577

578 default:

579 printf("wrong Input\n");

580 }

581

582 return 0;

583 }

584 int input()

585 {

586 int number;

587 scanf("%d", &number);

588

589 return (number);

590 }

591 void output(float number)

592 {

593 printf("%f", number);

594 }

595

596 /* Output of Q.15 ::

597 Press 1 to calculate area of circle

598 Press 2 to calculate area of square

599 Press 3 to calculate area of sphere

600 Enter your choice : 1

601 Enter radius : 2.1

602 Area of circle = 12.560000

603 */

604

605 /***************************************************************************************/

606

607 /* Q.16) Write a 'C' program that takes a number as input from user and checks whether

608 the number is even or odd using if-else statement. */

609 #include <stdio.h>

610

611 int main()

612 {

613 int num;

614

615 printf("Enter a number : ");

616 scanf("%d", &num);

617

618 if (num % 2 == 0)

619 printf("%d is even.", num);

620 else

621 printf("%d is odd.", num);

622

623 return 0;

624 }

625

626 /* Output of Q.16 ::

627 Enter a number : 21

localhost:64631 11/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
628 21 is odd.

629 */

630

631 /***************************************************************************************/

632

633 /* Q.17) Write a 'C' program to check whether the enter number is Armstrong or not. */

634 #include <stdio.h>

635

636 int main() {

637 int num, originalNumber, remainder, result = 0;

638 printf("Enter a three-digit number : ");

639 scanf("%d", &num);

640 originalNumber = num;

641

642 while (originalNumber != 0) {

643 remainder = originalNumber % 10;

644 result += remainder * remainder * remainder;

645 originalNumber /= 10;

646 }

647

648 if (result == num)

649 printf("%d is Armstrong number.", num);

650 else

651 printf("%d is not Armstrong number.", num);

652

653 return 0;

654 }

655

656 /* Output of Q.17 ::

657 Enter a three-digit number : 616

658 616 is not Armstrong number.

659 */

660

661 /***************************************************************************************/

662

663 /* Q.18) Write a 'C' program to check whether a given year is leap year or not using

664 if-else statement. */

665 #include <stdio.h>

666

667 int main()

668 {

669 int year;

670

671 printf("Enter a Year : ");

672 scanf("%d", &year);

673

674 if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))

675 printf("%d is a Leap Year.", year);

676 else

677 printf("%d is not a Leap Year.", year);

678

679 return 0;

680 }

681

682 /* Output of Q.18 ::

683 Enter a Year : 2022

684 2022 is not a Leap Year.

localhost:64631 12/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
685 */

686

687 /***************************************************************************************/

688

689 /* Q.19) Write a 'C' program to check whether the enter number is palindrome or not. */

690 #include <stdio.h>

691

692 int main()

693 {

694 int Number, Temp, Reminder, Reverse = 0;

695

696 printf("Enter a number : ");

697 scanf("%d", &Number);

698

699 Temp = Number;

700

701 while (Temp > 0)

702 {

703 Reminder = Temp % 10;

704 Reverse = Reverse * 10 + Reminder;

705 Temp = Temp / 10;

706 }

707

708 printf("Reverse of entered number = %d", Reverse);

709

710 if (Number == Reverse)

711 printf("\n%d is Palindrome Number.", Number);

712

713 else

714 printf("%d is not the Palindrome Number.", Number);

715

716 return 0;

717 }

718

719 /* Output of Q.19 ::

720 Enter a number : 202

721 Reverse of entered number = 202

722 202 is Palindrome Number.

723 */

724

725 /***************************************************************************************/

726

727 /* Q.20) ) Write a 'C' program to check whether a given year is leap year or not using

728 conditional operator. */

729 #include <stdio.h>

730

731 int main()

732 {

733 int year;

734

735 printf("Enter Year : ");

736 scanf("%d", &year);

737

738 (year%4==0 && year%100!=0) ? printf("%d is Leap Year", year) : (year%400 ==0 ) ?

739 printf("%d is Leap Year", year) : printf("%d is not Leap Year", year);

740

741 return 0;

localhost:64631 13/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
742 }

743

744 /* Output of Q.20 ::

745 Enter Year : 2024

746 2024 is Leap Year

747 */

748

749 /***************************************************************************************/

750

751 /* Q.21) Write a 'C' program to print the number pattern.

752 1

753 1 2

754 1 2 3

755 1 2 3 4

756 1 2 3 4 5 */

757 #include <stdio.h>

758 int main()

759 {

760 int i, j, rows;

761

762 printf("Enter Number of rows : ");

763 scanf("%d", &rows);

764

765 for (i = 1; i <= rows; ++i)

766 {

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

768 {

769 printf("%d ", j);

770 }

771 printf("\n");

772 }

773

774 return 0;

775 }

776

777 /* Output of Q.21 ::

778 Enter Number of rows : 5

779 1

780 1 2

781 1 2 3

782 1 2 3 4

783 1 2 3 4 5

784 */

785

786 /***************************************************************************************/

787

788 /* Q.22) Write a 'C' program that takes a number as input from user and checks whether

789 the number is even or odd using conditional operator. */

790 #include <stdio.h>

791

792 int main()

793 {

794 int num;

795

796 printf("Enter a Number : ");

797 scanf("%d", &num);

798

localhost:64631 14/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
799 (num % 2 == 0)

800 ? printf("%d is even number", num)

801 : printf("%d is odd number", num);

802

803 return 0;

804 }

805

806 /* Output of Q.22 ::

807 Enter a Number : 212

808 212 is even number

809 */

810

811 /***************************************************************************************/

812

813 /* Q.23) Write a 'C' program to check whether a given number is perfect number or not. */

814 #include <stdio.h>

815

816 int main()

817 {

818 int i, num, sum = 0;

819

820 printf("Enter Number : ");

821 scanf("%d", &num);

822

823 for (i=1; i<num; i++)

824 {

825 if (num%i == 0)

826 sum = sum + i;

827 }

828

829 if (sum == num)

830 printf(" %d is Perfect Number", num);

831 else

832 printf("%d is not Perfect Number", num);

833

834 return 0;

835 }

836

837 /* Output of Q.23 ::

838 Enter Number : 21

839 21 is not Perfect Number

840 */

841

842 /***************************************************************************************/

843

844 /* Q.24) Write a 'C' program to display multiplication table of a given number. */

845 #include <stdio.h>

846

847 int main()

848 {

849 int n, i;

850

851 printf("Enter Number : ");

852 scanf("%d", &n);

853

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

855 {

localhost:64631 15/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
856 printf("%d X %d = %d \n", n, i, n * i);

857 }

858

859 return 0;

860 }

861

862 /* Output of Q.24 ::

863 Enter Number : 11

864 11 X 1 = 11

865 11 X 2 = 22

866 11 X 3 = 33

867 11 X 4 = 44

868 11 X 5 = 55

869 11 X 6 = 66

870 11 X 7 = 77

871 11 X 8 = 88

872 11 X 9 = 99

873 11 X 10 = 110

874 */

875

876 /***************************************************************************************/

877

878 /* Q.25) Write a 'C' program to check whether a given character is VOWEL or CONSONANT

879 using switch-case. */

880 #include <stdio.h>

881

882 int main()

883 {

884 char p;

885

886 printf("Enter a character : ");

887 scanf("%c", &p);

888

889 if ((p >= 'A' && p <= 'Z') || (p >= 'a' && p <= 'z'))

890 {

891 switch (p)

892 {

893 case 'A':

894 case 'E':

895 case 'I':

896 case 'O':

897 case 'U':

898 case 'a':

899 case 'e':

900 case 'i':

901 case 'o':

902 case 'u':

903 printf("%c is a VOWEL.\n", p);

904 break;

905 default:

906 printf("%c is a CONSONANT.\n", p);

907 }

908 }

909 return 0;

910 }

911

912 /* Output of Q.25 ::

localhost:64631 16/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
913 Enter a character : u

914 u is a VOWEL.

915 */

916

917 /***************************************************************************************/

918

919 /* Q.26) Write a 'C' program to check whether inputted character is digit or alphabet. */

920 #include <stdio.h>

921

922 int main()

923 {

924 char d;

925

926 printf("Enter character : ");

927 scanf("%c", &d);

928

929 if (d >= '0' && d <= '9')

930 {

931 printf("%c is digit.", d);

932 }

933

934 else

935 {

936 printf("%c is alphabet.", d);

937 }

938

939 return 0;

940 }

941

942 /* Output of Q.26;

943 Enter character : a

944 a is alphabet.

945 */

946

947 /***************************************************************************************/

948

949 /* Q.27) Write 'C' Program to check whether given character is vowel or consonant. */

950 #include <stdio.h>

951

952 int main()

953 {

954 char i;

955

956 printf("Enter a character : ");

957 scanf("%c", &i);

958

959 if ((i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z'))

960 {

961 switch (i)

962 {

963 case 'A':

964 case 'E':

965 case 'I':

966 case 'O':

967 case 'U':

968 case 'a':

969 case 'e':

localhost:64631 17/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
970 case 'i':

971 case 'o':

972 case 'u':

973 printf("%c is a vowel\n", i);

974 break;

975 default:

976 printf("%c is a consonant\n", i);

977 }

978 }

979 return 0;

980 }

981

982 /* Output of Q.27;

983 Enter a character : l

984 l is a consonant

985 */

986

987 /***************************************************************************************/

988

989 /* Q.28) Write 'C' Program to find factorial of a given number. */

990 #include <stdio.h>

991

992 int main()

993 {

994 int i, num;

995 long facto = 1;

996

997 printf("Enter Number : ");

998 scanf("%d", &num);

999

1000 for (i = 1; i <= num; i++)

1001 {

1002 facto = facto * i;

1003 }

1004

1005 printf("Factorial of %d is = %d", num, facto);

1006

1007 return 0;

1008 }

1009

1010 /* Output of Q.28;

1011 Enter Number : 5

1012 Factorial of 5 is = 120

1013 */

1014

1015 /***************************************************************************************/

1016

1017 /* Q.29) Write 'C' Program to print the ASCII value of any given character. */

1018 #include <stdio.h>

1019

1020 int main()

1021 {

1022 char c;

1023

1024 printf("Enter a character : ");

1025 scanf("%c", &c);

1026

localhost:64631 18/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
1027 printf("ASCII value of %c = %d", c, c);

1028

1029 return 0;

1030 }

1031 /* Output of Q.29 ::

1032 Enter a character : t

1033 ASCII value of t = 116

1034 */

1035

1036 /***************************************************************************************/

1037

1038 /* Q.30) Write a 'C' program to accept one number and to calculate sum of digits. */

1039 #include <stdio.h>

1040

1041 int main()

1042 {

1043 int n, sum = 0, m;

1044

1045 printf("Enter Number : ");

1046 scanf("%d", &n);

1047

1048 while (n > 0)

1049 {

1050 m = n % 10;

1051 sum = sum + m;

1052 n = n / 10;

1053 }

1054

1055 printf("Sum of digits = %d", sum);

1056

1057 return 0;

1058 }

1059

1060 /* Output of Q.30;

1061 Enter Number : 123

1062 Sum of digits = 6

1063 */

1064

1065 /***************************************************************************************/

1066

1067 /* Q.31) Write a 'C' Program to calculate the x to the power y without using standard

1068 function. */

1069 #include <stdio.h>

1070 #include <conio.h>

1071

1072 void main()

1073 {

1074 int i, x, y, z;

1075

1076 z = 1;

1077

1078 printf("Enter the value of x : ");

1079 scanf("%d", &x);

1080

1081 printf("Enter the value of y : ");

1082 scanf("%d", &y);

1083

localhost:64631 19/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
1084 for (i=1; i<=y; i++)

1085 {

1086 z = z * x;

1087 }

1088

1089 printf("%d is %d to the power %d.", z, x, y);

1090 getch();

1091 }

1092

1093 /* Output of Q.31;

1094 Enter the value of x : 4

1095 Enter the value of y : 2

1096 16 is 4 to the power 2.

1097 */

1098

1099 /***************************************************************************************/

1100

1101 /* Q.32) Write a 'C' Program to convert a given character into uppercase & vice versa. */

1102 #include <stdio.h>

1103 #include <string.h>

1104

1105 int main()

1106 {

1107 char str[20];

1108 int i;

1109

1110 printf("Enter string(characters) : ");

1111 gets(str);

1112

1113 printf("inputted String is : %s ", str);

1114

1115 for (i = 0; i <= strlen(str); i++)

1116 {

1117 if (str[i] >= 97 && str[i] <= 122)

1118 str[i] = str[i] - 32;

1119

1120 else

1121 ;

1122 }

1123

1124 printf("\nConverted to Uppercase : %s ", str);

1125

1126 return 0;

1127 }

1128

1129 /* Output of Q.32 ::

1130 Enter string(characters) : Datta Awchar

1131 inputted String is : Datta Awchar

1132 Converted to Uppercase : DATTA AWCHAR

1133 */

1134

1135 /***************************************************************************************/

1136

1137 /* Q.33) Write a 'C' Program to print the following pattern.

1138 * * * * *

1139 * * * *

1140 * * *

localhost:64631 20/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
1141 * *

1142 * */

1143 #include <stdio.h>5

1144

1145 int main()

1146 {

1147 int i, j, rows;

1148

1149 printf("Enter Number of rows : ");

1150 scanf("%d", &rows);

1151

1152 for (i=rows; i>=1; --i)

1153 {

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

1155 {

1156 printf("* ");

1157 }

1158 printf("\n");

1159 }

1160

1161 return 0;

1162 }

1163

1164 /* Output of Q.33 ::

1165 Enter Number of rows : 5

1166 * * * * *

1167 * * * *

1168 * * *

1169 * *

1170 *

1171 */

1172

1173 /***************************************************************************************/

1174

1175 /* Q.34) Write a 'C' Program to display reverse of a given number. */

1176 #include <stdio.h>

1177

1178 int main()

1179 {

1180 int n, reverse = 0, rem;

1181

1182 printf("Enter Number : ");

1183 scanf("%d", &n);

1184

1185 while (n != 0)

1186 {

1187 rem = n % 10;

1188 reverse = reverse * 10 + rem;

1189 n /= 10;

1190 }

1191 printf("Reversed Number = %d", reverse);

1192

1193 return 0;

1194 }

1195

1196 /* Output of Q.34 ::

1197 Enter Number : 12

localhost:64631 21/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c
1198 Reversed Number = 21

1199 */

1200

1201 /***************************************************************************************/

1202

1203 /* Q.35) Write a 'C' Program to check whether a given number is prime number or not. */

1204 #include <stdio.h>

1205

1206 int main()

1207 {

1208 int i, l, flag = 0;

1209

1210 printf("Enter Number : ");

1211 scanf("%d", &l);

1212

1213 for (i = 2; i <= l / 2; ++i)

1214 {

1215 if (l % i == 0)

1216 {

1217 flag = 1;

1218 break;

1219 }

1220 }

1221

1222 if (flag == 0)

1223 printf("%d is a prime number.", l);

1224 else

1225 printf("%d is not a prime number.", l);

1226

1227 return 0;

1228 }

1229

1230 /* Output of Q.35;

1231 Enter Number : 29

1232 29 is a prime number.

1233 */

1234

1235 /***************************************************************************************/

1236

1237 /* Q.36) Write a 'C' Program to find power of a number (x^n). */

1238 #include <stdio.h>

1239 #include <math.h>

1240

1241 int main()

1242 {

1243 int x,n;

1244 int result;

1245

1246 printf("Enter Base : ");

1247 scanf("%d",&x);

1248

1249 printf("Enter Power : ");

1250 scanf("%d",&n);

1251

1252 result =pow((double)x,n);

1253

1254 printf("%d to the power of %d is= %d", x,n, result);

localhost:64631 22/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

1255 return 0;

1256 }

1257

1258 /* Output of Q.36 ::

1259 Enter Base : 6

1260 Enter Power : 3

1261 6 to the power of 3 is= 216

1262 */

1263

1264 /***************************************************************************************/

1265

1266 /* Q.37) Write a 'C' Program to accept characters from the user and count total

1267 alphabet and digit till the user enters '$'. */

1268 #include <stdio.h>

1269 #include <string.h>

1270

1271 int main()

1272 {

1273 char s[1000];

1274 int i, alphabets = 0, digits = 0, specialcharacters = 0;

1275

1276 printf("Enter the string : ");

1277 gets(s);

1278

1279 for (i = 0; s[i]; i++)

1280 {

1281 if ((s[i] >= 65 && s[i] <= 90) || (s[i] >= 97 && s[i] <= 122))

1282 alphabets++;

1283

1284 else if (s[i] >= 48 && s[i] <= 57)

1285 digits++;

1286

1287 else

1288 specialcharacters++;

1289 }

1290

1291 printf("Alphabets = %d\n", alphabets);

1292 printf("Digits = %d\n", digits);

1293 printf("Special characters = %d", specialcharacters);

1294

1295 return 0;

1296 }

1297

1298 /* Output of Q.37 ::

1299 Enter the string : Datta9(*())Awchar3892%#!$92-3

1300 Alphabets = 11

1301 Digits = 8

1302 Special characters = 10

1303 */

1304

1305 /***************************************************************************************/

1306

1307 /* Q.38) Write a 'C' Program to accept a number and check if is divisible by 5 and 7. */

1308 #include <stdio.h>

1309

1310 int main()

1311 {

localhost:64631 23/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

1312 int num;

1313

1314 printf("Enter Number : ");

1315 scanf("%d", &num);

1316

1317 if (num % 5 == 0)

1318 {

1319 printf("Number is divisible by 5");

1320 }

1321

1322 else if (num % 7 == 0)

1323 {

1324 printf("Number is divisible by 7");

1325 }

1326

1327 else

1328 {

1329 printf("Number is not divisible by 5 and 7") ;

1330 }

1331

1332 return 0;

1333 }

1334

1335 /* Output of Q.38 ::

1336 Enter Number : 21

1337 Number is divisible by 7

1338 */

1339

1340 /***************************************************************************************/

1341

1342 /* Q.39) Write a 'C' Program to check whether a given character is a VOWEL or CONSONANT

1343 using switch-case statement. */

1344 #include <stdio.h>

1345

1346 int main()

1347 {

1348 char p;

1349

1350 printf("Enter a character : ");

1351 scanf("%c", &p);

1352

1353 if ((p >= 'A' && p <= 'Z') || (p >= 'a' && p <= 'z'))

1354 {

1355 switch (p)

1356 {

1357 case 'A':

1358 case 'E':

1359 case 'I':

1360 case 'O':

1361 case 'U':

1362 case 'a':

1363 case 'e':

1364 case 'i':

1365 case 'o':

1366 case 'u':

1367 printf("%c is a VOWEL.\n", p);

1368 break;

localhost:64631 24/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

1369 default:

1370 printf("%c is a CONSONANT.\n", p);

1371 }

1372 }

1373 return 0;

1374 }

1375

1376 /* Output of Q.39 ::

1377 Enter a character : o

1378 o is a VOWEL.

1379 */

1380

1381 /***************************************************************************************/

1382

1383 /* Q.40) Write a menu driven program, accept two numbers and perform following options:

1384 1. GCD of a number

1385 2. LCM of a number */

1386 #include <stdio.h>

1387

1388 void main()

1389 {

1390 int num1, num2, GCD, LCM, remainder, numerator, denominator;

1391

1392 printf("Enter first number : ");

1393 scanf("%d", &num1);

1394

1395 printf("Enter second number : ");

1396 scanf("%d", &num2);

1397

1398 if (num1 > num2)

1399 {

1400 numerator = num1;

1401 denominator = num2;

1402 }

1403

1404 else

1405 {

1406 numerator = num2;

1407 denominator = num1;

1408 }

1409 remainder = numerator % denominator;

1410

1411 while (remainder != 0)

1412 {

1413 numerator = denominator;

1414 denominator = remainder;

1415 remainder = numerator % denominator;

1416 }

1417

1418 GCD = denominator;

1419 LCM = num1 * num2 / GCD;

1420

1421 printf("GCD of %d and %d = %d\n", num1, num2, GCD);

1422 printf("LCM of %d and %d = %d\n", num1, num2, LCM);

1423 }

1424

1425 /* Output of Q.40 ::

localhost:64631 25/26
12/3/21, 8:45 PM c:\Users\admin\Assinment of C\Assignment.c

1426 Enter first number : 12

1427 Enter second number : 14

1428 GCD of 12 and 14 = 2

1429 LCM of 12 and 14 = 84

1430 */

localhost:64631 26/26

You might also like