Lab Report -leading university

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Lab report :-2

NAME:MD.SAKIB RUHANI.
BATCH:D(63)
ID NO:0182410012101179

1. Printing Hello World!

#include <stdio.h>
int main(){
printf("Hello World!");
return 0;
}

2.Adding two numbers:

#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\\n", sum);
return 0;
}
Output:
3. Subtracting two numbers:

#include <stdio.h>
int main() {
int a, b, diff;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
diff = a - b;
printf("Difference: %d\\n", diff);
return 0;
}

4. Multiplying two numbers:

#include <stdio.h>
int main() {
int a, b, product;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
product = a * b;
printf("Product: %d\\n", product);
return 0;
}
5. Dividing two numbers:

#include <stdio.h>
int main() {
float a, b, result;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
if (b != 0) {
result = a / b;
printf("Result: %.2f\\n", result);
} else {
printf("Division by zero is not allowed.\\n");
}
return 0;
}

6. Finding the square of a number:


#include <stdio.h>
int main() {
int num, square;
printf("Enter a number: ");
scanf("%d", &num);
square = num * num;
printf("Square: %d\\n", square);
return 0;
}

7. Finding the average of two numbers:

#include <stdio.h>
int main() {
float a, b, average;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
average = (a + b) / 2;
printf("Average: %.2f\\n", average);
return 0;
}
8. Finding the factorial:

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter a positive number: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\\n");
} else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d: %llu\\n", n, fact);
}
return 0;
}
9. Reverse a number :.

#include <stdio.h>
int main() {
int n, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
printf("Reversed Number: %d\\n", reversed);
return 0;
}

10. Determining whether a number is odd or even:

#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("The number is Even.\\n");
} else {
printf("The number is Odd.\\n");
}
return 0;
}

11. Finding the number of digits in a number:

#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
n = n / 10;
++count;
}
printf("Number of digits: %d\\n", count);
return 0;
}

12. Checking prime numbers:

#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (num == 1) {
printf("1 is neither prime nor composite.\\n");
} else {
if (flag == 0) {
printf("%d is a prime number.\\n", num);
} else {
printf("%d is not a prime number.\\n", num);
}
}
return 0;
}

13. Finding the Factor:

#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factors of %d are: ", n);
for (i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
printf("\\n");
return 0;
}

14. Determining the Average (Array):

#include <stdio.h>
int main() {
int n, i;
float sum = 0.0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%f", &arr[i]);
sum += arr[i];
}
average = sum / n;
printf("Average: %.2f\\n", average);
return 0;
}
15. Boolean Checking (True/False):

#include <stdio.h>
int main() {
int num;
printf("Enter a number (1 for True, 0 for False): ");
scanf("%d", &num);
if (num == 1) {
printf("True\\n");
} else {
printf("False\\n");
}
return 0;
}

16. Checking the highest and lowest numbers:


#include <stdio.h>
int main() {
int n, i, max, min;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Max: %d, Min: %d\\n", max, min);
return 0;
}

17. Factors and element numbers:


#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int i;
int product = 1;
for (i = 0; i < 5; i++) {
product *= arr[i];
}
printf("Product: %d\\n", product);
return 0;
}

18. Reverse Earle (String):

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

int main() {
char str[100];
int i, len;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

str[strcspn(str, "\n")] = '\0';

len = strlen(str);
printf("Reversed string: ");
for (i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n"); return 0;
}

19. Checking string palindromes:


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

int main() {
char str[100];
int i, len, flag = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin); than gets()

by fgets
str[strcspn(str, "\n")] = '\0';

len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 1;
break;
}
}
20. Build a CGPA calculator.with switch case:

#include <stdio.h>
int main(){
int score;
printf("Enter your score: ");
scanf("%d", &score);
switch (score){
case 80 ... 100:
printf("Your grade is A+");
break;
case 75 ... 79:printf("Your grade is A");
break;
case 70 ... 74:
printf("Your grade is A-");
break;
case 65 ... 69:
printf("Your grade is B+");
break;
case 60 ... 64:
printf("Your grade is B");
break;
case 55 ... 59:
printf("Your grade is B-");
break;
case 50 ... 54:
printf("Your grade is C+");
break;
case 45 ... 49:
printf("Your grade is C");
break;
case 40 ... 44:
printf("Your grade is D");
break;
case 0 ... 39:
printf("Your grade is F");
break;
default:
printf("Invalid input");}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy