-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1035 lines (909 loc) · 44.6 KB
/
Copy pathindex.html
File metadata and controls
1035 lines (909 loc) · 44.6 KB
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C Loop Programs - Muhammad Arslan (Roll No. 18)</title>
<meta property="og:image" content="https://safarpath.github.io/c-loop-assig/image.jpg">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.code-block {
font-family: 'Fira Code', 'Courier New', monospace;
background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
color: #e2e8f0;
border-radius: 0.75rem;
padding: 1.25rem;
overflow-x: auto;
position: relative;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.code-block pre {
margin: 0;
line-height: 1.5;
}
.code-block .code-actions {
position: absolute;
top: 0.75rem;
right: 0.75rem;
display: flex;
gap: 0.5rem;
}
.code-block .code-btn {
background-color: rgba(51, 65, 85, 0.8);
color: white;
border: none;
border-radius: 0.375rem;
padding: 0.375rem 0.75rem;
cursor: pointer;
font-size: 0.75rem;
transition: all 0.2s ease;
backdrop-filter: blur(4px);
}
.code-block .code-btn:hover {
background-color: rgba(71, 85, 105, 0.9);
transform: translateY(-1px);
}
.tab-button {
transition: all 0.3s ease;
border-radius: 0.5rem;
font-weight: 500;
}
.tab-button.active {
background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
color: white;
box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.3);
}
.program-card {
transition: transform 0.3s ease, box-shadow 0.3s ease;
border-radius: 0.75rem;
overflow: hidden;
}
.program-card:hover {
transform: translateY(-5px);
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
.output-container {
background-color: #0f172a;
color: #e2e8f0;
border-radius: 0.75rem;
padding: 1.25rem;
margin-top: 1rem;
min-height: 120px;
font-family: 'Fira Code', 'Courier New', monospace;
white-space: pre-wrap;
overflow-y: auto;
max-height: 300px;
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}
.input-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
margin-top: 1rem;
}
.input-group label {
font-weight: 500;
color: #374151;
}
.input-group input {
padding: 0.5rem 0.75rem;
border: 1px solid #d1d5db;
border-radius: 0.375rem;
font-family: 'Fira Code', 'Courier New', monospace;
}
.input-group button {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
color: white;
border: none;
border-radius: 0.375rem;
padding: 0.5rem 1rem;
cursor: pointer;
font-weight: 500;
transition: all 0.2s ease;
}
.input-group button:hover {
transform: translateY(-1px);
box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.3);
}
.loading-spinner {
display: none;
width: 20px;
height: 20px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: #fff;
animation: spin 1s ease-in-out infinite;
margin-right: 0.5rem;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.mobile-menu {
display: none;
}
@media (max-width: 768px) {
.mobile-menu {
display: flex;
overflow-x: auto;
padding-bottom: 0.5rem;
}
.desktop-tabs {
display: none;
}
}
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<!-- Mobile Menu -->
<div class="mobile-menu bg-white shadow-sm sticky top-0 z-10">
<select id="mobile-menu-select" class="w-full p-3 text-lg font-medium border-none focus:outline-none">
<option value="while">While Loop Programs</option>
<option value="dowhile">Do-While Loop Programs</option>
</select>
</div>
<header class="bg-gradient-to-r from-blue-600 to-indigo-700 text-white py-6 shadow-lg">
<div class="container mx-auto px-4">
<h1 class="text-3xl md:text-4xl font-bold text-center">C Programming Loop Examples</h1>
<div class="flex flex-col md:flex-row justify-center items-center mt-4 space-y-3 md:space-y-0 md:space-x-6">
<div class="bg-blue-700/80 backdrop-blur-sm px-4 py-2 rounded-lg">
<p class="text-lg"><i class="fas fa-user mr-2"></i>Muhammad Arslan</p>
</div>
<div class="bg-blue-700/80 backdrop-blur-sm px-4 py-2 rounded-lg">
<p class="text-lg"><i class="fas fa-id-card mr-2"></i>Roll No. 18</p>
</div>
</div>
</div>
</header>
<main class="container mx-auto px-4 py-8">
<!-- About Section -->
<div class="bg-white rounded-xl shadow-lg p-6 mb-8">
<h2 class="text-2xl font-bold text-blue-700 mb-4">About This Project</h2>
<p class="text-gray-700 mb-4">
This interactive showcase demonstrates 5 programs using <code class="bg-gray-200 px-2 py-1 rounded">while</code> loops and 5 programs using <code class="bg-gray-200 px-2 py-1 rounded">do-while</code> loops in C programming language.
Each program includes the complete source code, a brief explanation, and an interactive runner to test the code.
</p>
<p class="text-gray-700">
While everyone else is submitting PDFs, I wanted to create something unique and advanced using HTML, CSS, Tailwind CSS, and JavaScript to present my assignment in an interactive format.
</p>
</div>
<!-- Program Sections -->
<div class="flex flex-col lg:flex-row gap-8">
<!-- While Loop Programs -->
<div id="while-section" class="lg:w-1/2">
<div class="program-card bg-white rounded-xl shadow-lg overflow-hidden">
<div class="bg-gradient-to-r from-blue-600 to-indigo-600 text-white p-4">
<h2 class="text-2xl font-bold"><i class="fas fa-redo-alt mr-2"></i>While Loop Programs</h2>
</div>
<div class="p-4">
<!-- Desktop Tabs -->
<div class="desktop-tabs flex space-x-2 mb-4 overflow-x-auto">
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium active" data-tab="while-1">Sum of Numbers</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="while-2">Reverse Number</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="while-3">Count Digits</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="while-4">Fibonacci</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="while-5">Palindrome</button>
</div>
<!-- While Program 1 -->
<div id="while-1" class="tab-content">
<h3 class="text-xl font-bold text-blue-700 mb-2">1. Sum of Natural Numbers</h3>
<p class="text-gray-700 mb-4">This program calculates the sum of the first N natural numbers using a while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('while-1-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="while-1-code" data-inputs='["n"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="while-1-code">#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
i++;
}
printf("Sum of first %d natural numbers = %d\n", n, sum);
return 0;
}</pre>
</div>
<div class="input-group">
<label for="while-1-input">Enter a positive integer:</label>
<input type="number" id="while-1-input" placeholder="e.g., 10" min="1">
<button onclick="runCode('while-1-code', ['while-1-input'])">Execute Program</button>
</div>
<div class="output-container" id="while-1-output">
Output will appear here...
</div>
</div>
<!-- While Program 2 -->
<div id="while-2" class="tab-content hidden">
<h3 class="text-xl font-bold text-blue-700 mb-2">2. Reverse a Number</h3>
<p class="text-gray-700 mb-4">This program reverses a given number using a while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('while-2-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="while-2-code" data-inputs='["num"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="while-2-code">#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed number = %d\n", reversed);
return 0;
}</pre>
</div>
<div class="input-group">
<label for="while-2-input">Enter an integer:</label>
<input type="number" id="while-2-input" placeholder="e.g., 12345">
<button onclick="runCode('while-2-code', ['while-2-input'])">Execute Program</button>
</div>
<div class="output-container" id="while-2-output">
Output will appear here...
</div>
</div>
<!-- While Program 3 -->
<div id="while-3" class="tab-content hidden">
<h3 class="text-xl font-bold text-blue-700 mb-2">3. Count Digits in a Number</h3>
<p class="text-gray-700 mb-4">This program counts the number of digits in a given integer using a while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('while-3-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="while-3-code" data-inputs='["num"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="while-3-code">#include <stdio.h>
int main() {
long long num;
int count = 0;
printf("Enter a number: ");
scanf("%lld", &num);
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Count digits
while (num != 0) {
num /= 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}</pre>
</div>
<div class="input-group">
<label for="while-3-input">Enter a number:</label>
<input type="number" id="while-3-input" placeholder="e.g., 123456789">
<button onclick="runCode('while-3-code', ['while-3-input'])">Execute Program</button>
</div>
<div class="output-container" id="while-3-output">
Output will appear here...
</div>
</div>
<!-- While Program 4 -->
<div id="while-4" class="tab-content hidden">
<h3 class="text-xl font-bold text-blue-700 mb-2">4. Fibonacci Series</h3>
<p class="text-gray-700 mb-4">This program prints the Fibonacci series up to N terms using a while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('while-4-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="while-4-code" data-inputs='["n"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="while-4-code">#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm, i = 1;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
while (i <= n) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
i++;
}
printf("\n");
return 0;
}</pre>
</div>
<div class="input-group">
<label for="while-4-input">Enter the number of terms:</label>
<input type="number" id="while-4-input" placeholder="e.g., 10" min="1">
<button onclick="runCode('while-4-code', ['while-4-input'])">Execute Program</button>
</div>
<div class="output-container" id="while-4-output">
Output will appear here...
</div>
</div>
<!-- While Program 5 -->
<div id="while-5" class="tab-content hidden">
<h3 class="text-xl font-bold text-blue-700 mb-2">5. Check for Palindrome Number</h3>
<p class="text-gray-700 mb-4">This program checks if a given number is a palindrome using a while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('while-5-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="while-5-code" data-inputs='["num"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="while-5-code">#include <stdio.h>
int main() {
int num, original, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
// Reverse the number
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
// Check if palindrome
if (original == reversed) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}</pre>
</div>
<div class="input-group">
<label for="while-5-input">Enter an integer:</label>
<input type="number" id="while-5-input" placeholder="e.g., 12321">
<button onclick="runCode('while-5-code', ['while-5-input'])">Execute Program</button>
</div>
<div class="output-container" id="while-5-output">
Output will appear here...
</div>
</div>
</div>
</div>
</div>
<!-- Do-While Loop Programs -->
<div id="dowhile-section" class="lg:w-1/2">
<div class="program-card bg-white rounded-xl shadow-lg overflow-hidden">
<div class="bg-gradient-to-r from-green-600 to-emerald-600 text-white p-4">
<h2 class="text-2xl font-bold"><i class="fas fa-sync-alt mr-2"></i>Do-While Loop Programs</h2>
</div>
<div class="p-4">
<!-- Desktop Tabs -->
<div class="desktop-tabs flex space-x-2 mb-4 overflow-x-auto">
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium active" data-tab="dowhile-1">Calculator</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="dowhile-2">Guessing Game</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="dowhile-3">Factorial</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="dowhile-4">Sum of Digits</button>
<button class="tab-button bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-lg font-medium" data-tab="dowhile-5">Multiplication Table</button>
</div>
<!-- Do-While Program 1 -->
<div id="dowhile-1" class="tab-content">
<h3 class="text-xl font-bold text-green-700 mb-2">1. Menu-Driven Calculator</h3>
<p class="text-gray-700 mb-4">This program implements a simple calculator with a menu using a do-while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('dowhile-1-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="dowhile-1-code"><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="dowhile-1-code">#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
do {
printf("\nCalculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice >= 1 && choice <= 4) {
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
}
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case 2:
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case 3:
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero!\n");
}
break;
case 5:
printf("Exiting calculator. Goodbye!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}</pre>
</div>
<div class="input-group">
<p class="text-gray-700">This program simulates a calculator. Run it and follow the prompts in the output area.</p>
<button onclick="runCode('dowhile-1-code')">Execute Program</button>
</div>
<div class="output-container" id="dowhile-1-output">
Output will appear here...
</div>
</div>
<!-- Do-While Program 2 -->
<div id="dowhile-2" class="tab-content hidden">
<h3 class="text-xl font-bold text-green-700 mb-2">2. Number Guessing Game</h3>
<p class="text-gray-700 mb-4">This program implements a simple number guessing game using a do-while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('dowhile-2-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="dowhile-2-code"><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="dowhile-2-code">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
// Initialize random seed
srand(time(0));
// Generate random number between 1 and 100
number = rand() % 100 + 1;
printf("Welcome to the Number Guessing Game!\n");
printf("I'm thinking of a number between 1 and 100.\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("Too high! Try again.\n");
} else if (guess < number) {
printf("Too low! Try again.\n");
} else {
printf("Congratulations! You guessed the number in %d attempts.\n", attempts);
}
} while (guess != number);
return 0;
}</pre>
</div>
<div class="input-group">
<p class="text-gray-700">This program is a number guessing game. Run it and follow the prompts in the output area.</p>
<button onclick="runCode('dowhile-2-code')">Execute Program</button>
</div>
<div class="output-container" id="dowhile-2-output">
Output will appear here...
</div>
</div>
<!-- Do-While Program 3 -->
<div id="dowhile-3" class="tab-content hidden">
<h3 class="text-xl font-bold text-green-700 mb-2">3. Factorial Calculation</h3>
<p class="text-gray-700 mb-4">This program calculates the factorial of a number using a do-while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('dowhile-3-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="dowhile-3-code" data-inputs='["n"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="dowhile-3-code">#include <stdio.h>
int main() {
int n, i = 1;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
// Check for negative numbers
if (n < 0) {
printf("Error: Factorial of a negative number doesn't exist.\n");
} else {
// Calculate factorial
do {
factorial *= i;
i++;
} while (i <= n);
printf("Factorial of %d = %llu\n", n, factorial);
}
return 0;
}</pre>
</div>
<div class="input-group">
<label for="dowhile-3-input">Enter a positive integer:</label>
<input type="number" id="dowhile-3-input" placeholder="e.g., 10" min="0">
<button onclick="runCode('dowhile-3-code', ['dowhile-3-input'])">Execute Program</button>
</div>
<div class="output-container" id="dowhile-3-output">
Output will appear here...
</div>
</div>
<!-- Do-While Program 4 -->
<div id="dowhile-4" class="tab-content hidden">
<h3 class="text-xl font-bold text-green-700 mb-2">4. Sum of Digits</h3>
<p class="text-gray-700 mb-4">This program calculates the sum of digits of a number using a do-while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('dowhile-4-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="dowhile-4-code" data-inputs='["num"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="dowhile-4-code">#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
// Handle negative numbers
if (num < 0) {
num = -num;
}
// Calculate sum of digits
do {
digit = num % 10;
sum += digit;
num /= 10;
} while (num != 0);
printf("Sum of digits = %d\n", sum);
return 0;
}</pre>
</div>
<div class="input-group">
<label for="dowhile-4-input">Enter a number:</label>
<input type="number" id="dowhile-4-input" placeholder="e.g., 12345">
<button onclick="runCode('dowhile-4-code', ['dowhile-4-input'])">Execute Program</button>
</div>
<div class="output-container" id="dowhile-4-output">
Output will appear here...
</div>
</div>
<!-- Do-While Program 5 -->
<div id="dowhile-5" class="tab-content hidden">
<h3 class="text-xl font-bold text-green-700 mb-2">5. Multiplication Table</h3>
<p class="text-gray-700 mb-4">This program prints the multiplication table of a number using a do-while loop.</p>
<div class="code-block mb-4">
<div class="code-actions">
<button class="code-btn" onclick="copyCode('dowhile-5-code')"><i class="fas fa-copy mr-1"></i>Copy</button>
<button class="code-btn run-btn" data-code="dowhile-5-code" data-inputs='["num"]'><i class="fas fa-play mr-1"></i>Run</button>
</div>
<pre id="dowhile-5-code">#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
do {
printf("%d × %d = %d\n", num, i, num * i);
i++;
} while (i <= 10);
return 0;
}</pre>
</div>
<div class="input-group">
<label for="dowhile-5-input">Enter a number:</label>
<input type="number" id="dowhile-5-input" placeholder="e.g., 5">
<button onclick="runCode('dowhile-5-code', ['dowhile-5-input'])">Execute Program</button>
</div>
<div class="output-container" id="dowhile-5-output">
Output will appear here...
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Additional Information -->
<div class="bg-white rounded-xl shadow-lg p-6 mt-8">
<h2 class="text-2xl font-bold text-purple-700 mb-4">Key Differences Between While and Do-While Loops</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div class="bg-blue-50 p-4 rounded-lg">
<h3 class="text-xl font-bold text-blue-700 mb-2">While Loop</h3>
<ul class="list-disc pl-5 text-gray-700 space-y-2">
<li>Condition is checked before the loop body executes</li>
<li>If the condition is false initially, the loop body never executes</li>
<li>Syntax: <code class="bg-gray-200 px-2 py-1 rounded">while (condition) { ... }</code></li>
<li>Use when you need to check the condition before executing the loop body</li>
</ul>
</div>
<div class="bg-green-50 p-4 rounded-lg">
<h3 class="text-xl font-bold text-green-700 mb-2">Do-While Loop</h3>
<ul class="list-disc pl-5 text-gray-700 space-y-2">
<li>Loop body executes at least once before condition is checked</li>
<li>Condition is checked after the loop body executes</li>
<li>Syntax: <code class="bg-gray-200 px-2 py-1 rounded">do { ... } while (condition);</code></li>
<li>Use when you need the loop body to execute at least once</li>
</ul>
</div>
</div>
</div>
</main>
<footer class="bg-gray-800 text-white py-6 mt-8">
<div class="container mx-auto px-4 text-center">
<p class="text-lg">Assignment by <span class="font-bold">Muhammad Arslan</span> (Roll No. 18)</p>
<p class="mt-2">C Programming - Loop Structures</p>
</div>
</footer>
<script>
// Advanced JavaScript functionality
document.addEventListener('DOMContentLoaded', function() {
// Tab switching functionality
document.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', () => {
const tabGroup = button.closest('.p-4');
const tabId = button.getAttribute('data-tab');
// Remove active class from all buttons in this group
tabGroup.querySelectorAll('.tab-button').forEach(btn => {
btn.classList.remove('active');
});
// Add active class to clicked button
button.classList.add('active');
// Hide all tab contents in this group
tabGroup.querySelectorAll('.tab-content').forEach(content => {
content.classList.add('hidden');
});
// Show selected tab content
document.getElementById(tabId).classList.remove('hidden');
});
});
// Mobile menu functionality
const mobileMenu = document.getElementById('mobile-menu-select');
mobileMenu.addEventListener('change', function() {
if (this.value === 'while') {
document.getElementById('while-section').style.display = 'block';
document.getElementById('dowhile-section').style.display = 'none';
} else {
document.getElementById('while-section').style.display = 'none';
document.getElementById('dowhile-section').style.display = 'block';
}
});
// Initialize mobile view
document.getElementById('while-section').style.display = 'block';
document.getElementById('dowhile-section').style.display = 'none';
// Run button event listeners
document.querySelectorAll('.run-btn').forEach(button => {
button.addEventListener('click', function() {
const codeId = this.getAttribute('data-code');
const inputs = this.getAttribute('data-inputs');
if (inputs) {
const inputIds = JSON.parse(inputs);
runCode(codeId, inputIds);
} else {
runCode(codeId);
}
});
});
});
// Copy code functionality
function copyCode(elementId) {
const codeElement = document.getElementById(elementId);
const textArea = document.createElement('textarea');
textArea.value = codeElement.textContent;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
// Show feedback
const button = event.target;
const originalText = button.innerHTML;
button.innerHTML = '<i class="fas fa-check mr-1"></i>Copied!';
button.style.backgroundColor = '#10b981';
setTimeout(() => {
button.innerHTML = originalText;
button.style.backgroundColor = '';
}, 2000);
}
// Advanced code execution with simulation
function runCode(codeId, inputIds = []) {
const codeElement = document.getElementById(codeId);
const outputId = codeId.replace('-code', '-output');
const outputElement = document.getElementById(outputId);
const runButton = event ? event.target : document.querySelector(`[data-code="${codeId}"]`);
// Show loading state
const originalText = runButton.innerHTML;
runButton.innerHTML = '<div class="loading-spinner"></div>Running...';
runButton.disabled = true;
// Clear previous output
outputElement.textContent = 'Running program...\n\n';
// Get input values if provided
const inputs = {};
inputIds.forEach(id => {
const inputElement = document.getElementById(id);
if (inputElement) {
inputs[id] = inputElement.value;
}
});
// Simulate program execution with a delay
setTimeout(() => {
try {
const code = codeElement.textContent;
const result = simulateCProgram(code, inputs);
outputElement.textContent = result;
} catch (error) {
outputElement.textContent = `Error: ${error.message}`;
}
// Restore button state
runButton.innerHTML = originalText;
runButton.disabled = false;
}, 1000);
}
// Advanced C program simulation
function simulateCProgram(code, inputs) {
let output = '';
const lines = code.split('\n');
// Extract main logic based on program type
if (code.includes('Sum of first')) {
// Sum of natural numbers
const n = parseInt(inputs[Object.keys(inputs)[0]] || 10);
let sum = 0, i = 1;
output += `Enter a positive integer: ${n}\n`;
while (i <= n) {
sum += i;
i++;
}
output += `Sum of first ${n} natural numbers = ${sum}\n`;
}
else if (code.includes('Reversed number')) {
// Reverse a number
const num = parseInt(inputs[Object.keys(inputs)[0]] || 12345);
let reversed = 0, remainder, temp = num;
output += `Enter an integer: ${num}\n`;
while (temp !== 0) {
remainder = temp % 10;
reversed = reversed * 10 + remainder;
temp = Math.floor(temp / 10);
}
output += `Reversed number = ${reversed}\n`;
}
else if (code.includes('Number of digits')) {
// Count digits
const num = parseInt(inputs[Object.keys(inputs)[0]] || 123456789);
let count = 0, temp = Math.abs(num);
output += `Enter a number: ${num}\n`;
if (temp === 0) {
count = 1;
} else {
while (temp !== 0) {
temp = Math.floor(temp / 10);
count++;
}
}
output += `Number of digits: ${count}\n`;
}
else if (code.includes('Fibonacci Series')) {
// Fibonacci series
const n = parseInt(inputs[Object.keys(inputs)[0]] || 10);
let t1 = 0, t2 = 1, nextTerm, i = 1;
output += `Enter the number of terms: ${n}\n`;
output += `Fibonacci Series: `;
while (i <= n) {
output += `${t1}, `;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
i++;
}
output += `\n`;
}
else if (code.includes('palindrome')) {
// Palindrome check
const num = parseInt(inputs[Object.keys(inputs)[0]] || 12321);
let reversed = 0, remainder, temp = num;
output += `Enter an integer: ${num}\n`;
while (temp !== 0) {
remainder = temp % 10;
reversed = reversed * 10 + remainder;
temp = Math.floor(temp / 10);
}
if (num === reversed) {
output += `${num} is a palindrome.\n`;
} else {
output += `${num} is not a palindrome.\n`;
}
}
else if (code.includes('Calculator Menu')) {
// Calculator simulation
output += simulateCalculator();
}
else if (code.includes('Number Guessing Game')) {
// Guessing game simulation
output += simulateGuessingGame();
}
else if (code.includes('Factorial')) {
// Factorial calculation
const n = parseInt(inputs[Object.keys(inputs)[0]] || 5);
output += `Enter a positive integer: ${n}\n`;
if (n < 0) {
output += `Error: Factorial of a negative number doesn't exist.\n`;
} else {
let factorial = 1, i = 1;
do {
factorial *= i;
i++;
} while (i <= n);
output += `Factorial of ${n} = ${factorial}\n`;
}
}
else if (code.includes('Sum of digits')) {
// Sum of digits
const num = parseInt(inputs[Object.keys(inputs)[0]] || 12345);
let sum = 0, digit, temp = Math.abs(num);
output += `Enter a number: ${num}\n`;
do {
digit = temp % 10;
sum += digit;
temp = Math.floor(temp / 10);
} while (temp !== 0);
output += `Sum of digits = ${sum}\n`;
}
else if (code.includes('Multiplication table')) {
// Multiplication table
const num = parseInt(inputs[Object.keys(inputs)[0]] || 5);
let i = 1;
output += `Enter a number: ${num}\n`;
output += `Multiplication table of ${num}:\n`;
do {
output += `${num} × ${i} = ${num * i}\n`;
i++;
} while (i <= 10);
}
else {
output += "Program output simulation not implemented for this program.\n";
}
return output;
}
// Advanced simulations for interactive programs
function simulateCalculator() {
let output = '';
const operations = [
{ choice: 1, num1: 10, num2: 5, result: 15, op: '+' },
{ choice: 2, num1: 20, num2: 8, result: 12, op: '-' },
{ choice: 3, num1: 6, num2: 7, result: 42, op: '*' },
{ choice: 4, num1: 15, num2: 3, result: 5, op: '/' },
{ choice: 5 }
];
for (let i = 0; i < operations.length; i++) {
const op = operations[i];
output += `\nCalculator Menu:\n`;
output += `1. Addition\n`;
output += `2. Subtraction\n`;
output += `3. Multiplication\n`;
output += `4. Division\n`;
output += `5. Exit\n`;
output += `Enter your choice: ${op.choice}\n`;
if (op.choice >= 1 && op.choice <= 4) {
output += `Enter two numbers: ${op.num1} ${op.num2}\n`;
output += `Result: ${op.result.toFixed(2)}\n`;
} else if (op.choice === 5) {