File size: 9,110 Bytes
6ad0d89 |
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 |
source,target
"#include <stdio.h>
int main() {
printf(""Hello, world!\n"");
return 0;
}","A simple C program that prints 'Hello, world!' to the console."
"#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n = 5;
printf(""Factorial of %d is %d\n"", n, factorial(n));
return 0;
}",A C program to calculate the factorial of a number using recursion.
"#include <stdio.h>
int main() {
int n, sum = 0;
printf(""Enter a number: "");
scanf(""%d"", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf(""Sum of first %d natural numbers is %d\n"", n, sum);
return 0;
}",A C program to calculate the sum of the first 'n' natural numbers.
"#include <stdio.h>
int main() {
double a, b, product;
printf(""Enter two numbers: "");
scanf(""%lf %lf"", &a, &b);
product = a * b;
printf(""Product = %.2lf"", product);
return 0; }",A C Program to Multiply Two Numbers
"#include <stdio.h>
int main() {
printf(""Hello, World!"");
return 0;
}","Program to Display Hello, World!"
"#include <stdio.h>
int main() {
int number;
printf(""Enter an integer:"");
scanf(""%d"", &number);
printf(""You entered: %d"", number);
return 0;}",A C Program to Print an Integer
"#include <stdio.h>
int main() {
int number;
printf(""Enter an integer:""); scanf(""%d"", &number);
// displays output
printf(""You entered: %d"", number);
return 0; }", A Program to Print an Integer
"#include <stdio.h>
int main() {
char c;
printf(""Enter a character:"");
scanf(""%c"", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf(""ASCII value of %c = %d"", c, c);
return 0; }",A Program to Print SCII Value
"#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf(""Enter dividend:"");
printf(""Enter divisor:"");
scanf(""%d"", ÷nd);
scanf(""%d"", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf(""Quotient = %d
"", quotient);
return 0;
}",C Program to Compute Quotient and Remainder
" #include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf(""Size of int: %zu bytes
"", sizeof(intType));
printf(""Size of float: %zu bytes
"", sizeof(floatType));
printf(""Size of double: %zu bytes
"", sizeof(doubleType));
printf(""Size of char: %zu byte
"", sizeof(charType));
return 0; }","C Program to Find the Size of int, float, double and char"
"#include <stdio.h>
int main() {
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;
printf(""Size of int = %zu bytes
"", sizeof(a));
printf(""Size of long int = %zu bytes
"", sizeof(b));
printf(""Size of long long int = %zu bytes
"", sizeof(c));
printf(""Size of double = %zu bytes
"", sizeof(e));
printf(""Size of long double = %zu bytes
"", sizeof(f));
return 0;
}",C Program to Demonstrate the Working of Keyword long
"#include<stdio.h>
int main() {
double first, second, temp;
printf(""Enter first number:"");
scanf(""%lf"", &first);
printf(""Enter second number: "");
scanf(""%lf"", &second);
// value of first is assigned to temp
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to second
second = temp;
// %.2lf displays number up to 2 decimal points
printf(""
After swapping, first number = %.2lf
"", first);
printf(""After swapping, second number = %.2lf"", second);
return 0; }",C Program to Swap Two Numbers
" #include <stdio.h>
int main() {
int num;
printf(""Enter an integer: "");
scanf(""%d"", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf(""%d is even."", num);
else
printf(""%d is odd."", num);
return 0; }",C Program to Check Whether a Number is Even or Odd
"#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf(""Enter an alphabet:"");
scanf(""%c"", &c);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel || uppercase_vowel)
printf(""%c is a vowel."", c);
else
printf(""%c is a consonant."", c);
return 0;}", C Program to Check Whether a Character is a Vowel or Consonant
" #include <stdio.h>
int main() {
double n1, n2, n3;
printf(""Enter three different numbers: "");
scanf(""%lf %lf %lf"", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if(n1 >= n2 && n1 >= n3)
printf(""%.2f is the largest number."", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf(""%.2f is the largest number."", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf(""%.2f is the largest number."", n3);
return 0;}",C Program to Find the Largest Number Among Three Numbers usinf if statement
"#include <stdio.h>
int main() {
double n1, n2, n3;
printf(""Enter three different numbers:"");
scanf(""%lf %lf %lf"", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf(""%.2f is the largest number."", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf(""%.2f is the largest number."", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf(""%.2f is the largest number."", n3);
return 0; } ",C Program to Find the Largest Number Among Three Numbers using if else statements
"#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf(""Enter coefficients a, b and c:"");
scanf(""%lf %lf %lf"", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf(""root1 = %.2lf and root2 = %.2lf"", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf(""root1 = root2 = %.2lf;"", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf(""root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi"", realPart, imagPart, realPart, imagPart);
}
return 0;
} ",C Program to Find the Roots of a Quadratic Equation
" #include <stdio.h>
int main() {
double n1, n2, n3;
printf(""Enter three numbers:"");
scanf(""%lf %lf %lf"", &n1, &n2, &n3);
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf(""%.2lf is the largest number."", n1);
else
printf(""%.2lf is the largest number."", n3);
}
// outer else statement
else {
// inner if...else
if (n2 >= n3)
printf(""%.2lf is the largest number."", n2);
else
printf(""%.2lf is the largest number."", n3);
}
return 0;}", C Program to Find the Largest Number Among Three Numbers using nested if statement
"#include <stdio.h>
int main() {
int year;
printf(""Enter a year:"");
scanf(""%d"", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf(""%d is a leap year."", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf(""%d is not a leap year."", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf(""%d is a leap year."", year);
}
// all other years are not leap years
else {
printf(""%d is not a leap year."", year);
}
return 0; } ",C Program to Check Leap Year
"#include <stdio.h>
int main() {
double num;
printf(""Enter a number:"");
scanf(""%lf"", &num);
if (num <= 0.0) {
if (num == 0.0)
printf(""You entered 0."");
else
printf(""You entered a negative number."");
}
else
printf(""You entered a positive number."");
return 0;
}", C Program to Check Whether a Number is Positive or Negative
"#include <stdio.h>
int main() {
char c;
printf(""Enter a character:"");
scanf(""%c"", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf(""%c is an alphabet."", c);
else
printf(""%c is not an alphabet."", c);
return 0;}", C Program to Check Whether a Character is an Alphabet or not
|