Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Easy Code Requests

The document provides beginner-friendly C code examples for various programming topics, including calculating the area and perimeter of a circle, solving quadratic equations, checking if a number is even or odd, and implementing a grade system using switch cases. It also covers more advanced topics such as recursion for factorial and Fibonacci series, string manipulation, and using pointers for arithmetic operations. Each code snippet is accompanied by comments and explanations to aid understanding for novice programmers.

Uploaded by

Shanon Jensen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Easy Code Requests

The document provides beginner-friendly C code examples for various programming topics, including calculating the area and perimeter of a circle, solving quadratic equations, checking if a number is even or odd, and implementing a grade system using switch cases. It also covers more advanced topics such as recursion for factorial and Fibonacci series, string manipulation, and using pointers for arithmetic operations. Each code snippet is accompanied by comments and explanations to aid understanding for novice programmers.

Uploaded by

Shanon Jensen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Sure, Shopnil! Let me know the topic, and I'll write the simplest, beginner-friendly C code for you.

Here’s the simplest C code for each topic you mentioned, written in a beginner-friendly way:

1. Area and Perimeter of a Circle

#include <stdio.h>
#define PI 3.1416

int main() {
float radius, area, perimeter;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;


perimeter = 2 * PI * radius;

printf("Area of the circle: %.2f\n", area);


printf("Perimeter of the circle: %.2f\n", perimeter);

return 0;
}

2. Solve ax² + bx + c = 0

#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");


scanf("%f %f %f", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant >= 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Root 1 = %.2f\n", root1);


printf("Root 2 = %.2f\n", root2);
} else {
printf("No real roots exist.\n");
}

return 0;
}

3. Even or Odd

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/9
c

#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0)
printf("%d is Even.\n", number);
else
printf("%d is Odd.\n", number);

return 0;
}

4. Palindrome Check

#include <stdio.h>

int main() {
int num, reversed = 0, original, remainder;

printf("Enter a number: ");


scanf("%d", &num);

original = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed)
printf("The number is a Palindrome.\n");
else
printf("The number is not a Palindrome.\n");

return 0;
}

5. Grade System using Switch Case

#include <stdio.h>

int main() {
char grade;

printf("Enter the grade (A, B, C, D, F): ");


scanf(" %c", &grade);

switch (grade) {
case 'A':
printf("Excellent!\n");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/9
break;
case 'B':
printf("Good Job.\n");
break;
case 'C':
printf("Well Done.\n");
break;
case 'D':
printf("You Passed.\n");
break;
case 'F':
printf("Better Luck Next Time.\n");
break;
default:
printf("Invalid Grade.\n");
}

return 0;
}

6. Simple Calculator using Switch Case

#include <stdio.h>

int main() {
float num1, num2, result;
char operator;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0)
result = num1 / num2;
else {
printf("Division by zero is not allowed.\n");
return 1;
}
break;
default:
printf("Invalid Operator.\n");
return 1;
}

printf("Result: %.2f\n", result);


return 0;
}

7. Diamond Pattern

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/9
c

#include <stdio.h>

int main() {
int n, i, j;

printf("Enter the number of rows for the diamond: ");


scanf("%d", &n);

// Upper half
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}

// Lower half
for (i = n - 1; i >= 1; i--) {
for (j = 1; j <= n - i; j++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}

return 0;
}

8. Sum of Series (1 + 2 + 3 + ... + n)

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter the value of n: ");


scanf("%d", &n);

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


sum += i;

printf("Sum of the series: %d\n", sum);


return 0;
}

Let me know if you need any explanations or modifications! 😊


Here's beginner-friendly C code for each of the topics you requested:

1. Summation and Multiplication of Two Arrays

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/9
c

#include <stdio.h>

int main() {
int n, i;

printf("Enter the size of the arrays: ");


scanf("%d", &n);

int arr1[n], arr2[n], sum[n], product[n];

printf("Enter elements of the first array:\n");


for (i = 0; i < n; i++)
scanf("%d", &arr1[i]);

printf("Enter elements of the second array:\n");


for (i = 0; i < n; i++)
scanf("%d", &arr2[i]);

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


sum[i] = arr1[i] + arr2[i];
product[i] = arr1[i] * arr2[i];
}

printf("Sum of arrays:\n");
for (i = 0; i < n; i++)
printf("%d ", sum[i]);

printf("\nProduct of arrays:\n");
for (i = 0; i < n; i++)
printf("%d ", product[i]);

return 0;
}

2. Factorial Using Recursion

#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

printf("Factorial of %d is %d\n", n, factorial(n));


return 0;
}

3. Fibonacci Series Using Recursion

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/9
c

#include <stdio.h>

int fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");


for (int i = 0; i < n; i++)
printf("%d ", fibonacci(i));

return 0;
}

4. Reverse a String

#include <stdio.h>
#include <string.h>

int main() {
char str[100], reversed[100];
int length, i;

printf("Enter a string: ");


scanf("%s", str);

length = strlen(str);

for (i = 0; i < length; i++)


reversed[i] = str[length - 1 - i];

reversed[length] = '\0'; // Null-terminate the string

printf("Reversed string: %s\n", reversed);


return 0;
}

5. String Length

#include <stdio.h>

int main() {
char str[100];
int length = 0;

printf("Enter a string: ");


Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/9
scanf("%s", str);

while (str[length] != '\0')


length++;

printf("Length of the string: %d\n", length);


return 0;
}

6. Count Vowels and Consonants

#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
int vowels = 0, consonants = 0, i;

printf("Enter a string: ");


scanf("%s", str);

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


char ch = tolower(str[i]);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


vowels++;
else if (ch >= 'a' && ch <= 'z')
consonants++;
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);
return 0;
}

7. Swap Two Pointers

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x, y;

printf("Enter two numbers: ");


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

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


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

return 0;
}

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/9
8. Add, Subtract, Multiply, Divide Using Pointers

#include <stdio.h>

void calculate(int a, int b, int *sum, int *diff, int *prod, float *div) {
*sum = a + b;
*diff = a - b;
*prod = a * b;
if (b != 0)
*div = (float)a / b;
else
*div = 0; // Avoid division by zero
}

int main() {
int a, b, sum, diff, prod;
float div;

printf("Enter two numbers: ");


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

calculate(a, b, &sum, &diff, &prod, &div);

printf("Sum: %d\n", sum);


printf("Difference: %d\n", diff);
printf("Product: %d\n", prod);
if (b != 0)
printf("Division: %.2f\n", div);
else
printf("Division: Not possible (division by zero)\n");

return 0;
}

9. Find Max and Min Using Pointers

#include <stdio.h>

void findMaxMin(int arr[], int n, int *max, int *min) {


*max = arr[0];
*min = arr[0];

for (int i = 1; i < n; i++) {


if (arr[i] > *max)
*max = arr[i];
if (arr[i] < *min)
*min = arr[i];
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n], max, min;

printf("Enter the elements:\n");


for (int i = 0; i < n; i++)

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/9
scanf("%d", &arr[i]);

findMaxMin(arr, n, &max, &min);

printf("Maximum: %d\n", max);


printf("Minimum: %d\n", min);

return 0;
}

Let me know if you’d like any explanations or tweaks! 😊

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/9

You might also like