COMPUTER SCIENCE PRACTICAL PYQ NBU - CS HONS

0



Programming Fundamentals using C Lab

1. Write a program to enter two numbers and find their product.

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);

    printf("The Product of %d and %d is %d", a, b, a * b);

    return 0;
} (code-box)

 

 2. Write a program to reverse a number given by the user.

#include <stdio.h>

int main()
{
    int n, reverse = 0, remainder;

    printf("Enter number: ");
    scanf("%d", &n);

    while (n != 0)
    {
        remainder = n % 10;
        reverse = reverse * 10 + remainder;
        n /= 10;
    }

    printf("Reversed Number: %d", reverse);

    return 0;
} (code-box)

 

 3. Write a program to swap two numbers.

#include <stdio.h>

int main()
{
    int a, b, temp;

    printf("Enter A: ");
    scanf("%d", &a);
    printf("Enter B: ");
    scanf("%d", &b);

    temp = a;
    a = b;
    b = temp;

    printf("A = %d \nB = %d", a, b);

    return 0;
} (code-box)

4. Write a program to check Whether a Number is Even or Odd.

#include <stdio.h>

int main()
{
    int n;

    printf("Enter Number: ");
    scanf("%d", &n);

    if (n % 2 == 0)
    {
        printf("Even");
    }
    else
    {
        printf("Odd");
    }

    return 0;
} (code-box)

5. Write a program to enter three numbers and find their sum.

#include <stdio.h>

int main()
{
    int x, y, z, sum;

    printf("Enter Three Numbers: \n");
    scanf("%d %d %d", &x, &y, &z);

    sum = x + y + z;
    printf("Sum of Three Numbers is: %d", sum);

    return 0;
} (code-box) 

6. Write a program to swap two numbers without using any third variable.

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter A: ");
    scanf("%d", &a);
    printf("Enter B: ");
    scanf("%d", &b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping: \nA = %d \nB = %d", a, b);

    return 0;
} (code-box)

7. Write a program to calculate GCD of two numbers.

#include <stdio.h>

int main()
{
    int a, b, gcd;

    printf("Enter two numbers: \n");
    scanf("%d %d", &a, &b);

    for (int i = 1; i <= a && i <= b; ++i)
    {
        if (a % i == 0 && b % i == 0)
        {
            gcd = i;
        }
    }

    printf("GCD of %d and %d is %d", a, b, gcd);

    return 0;
} (code-box)

8. Write a program to compute the sum of first n terms of the series S = 1 + 2 + 3 + 4 + 5...

#include <stdio.h>

int main()
{

    int n, sum = 0;

    printf("Enter N: ");
    scanf("%d", &n);

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

    printf("Sum of first %d terms of the series is %d\n", n, sum);

    return 0;
} (code-box)

9. Write a program to check whether a given number is a Prime or Composite.

#include <stdio.h>

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

    printf("Enter number: ");
    scanf("%d", &n);

    if (n <= 1)
    {
        printf("%d is not a prime number\n", n);
        return 0;
    }

    for (int i = 2; i <= n / 2; ++i)
    {
        if (n % i == 0)
        {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
    {
        printf("%d is a prime number\n", n);
    }
    else
    {
        printf("%d is a composite number\n", n);
    }

    return 0;
} (code-box)

10. Write a program to compute the factors of a given number.

#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    printf("Factors of %d are: ", n);
    for (int i = 1; i <= n; ++i)
    {
        if (n % i == 0)
        {
            printf("%d ", i);
        }
    }
    return 0;
} (code-box)

11. Write a program to calculate LCM of two numbers.

#include <stdio.h>

int main() {

    int n1, n2, max;

    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);

    // maximum number between n1 and n2 is stored in max
    max = (n1 > n2) ? n1 : n2;

    while (1) {
        if ((max % n1 == 0) && (max % n2 == 0)) {
            printf("The LCM of %d and %d is %d.", n1, n2, max);
            break;
        }
        ++max;
    }
    return 0;
} (code-box) 

12. Write a program to compute the sum of first n terms of the series S = 1 + 8 + 27 + 64 + ...

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

int main() {
    int n, i;
    double sum = 0;
    printf("Enter the value of n: ");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) {
        sum += pow(i, 3);
    }

    printf("Sum of first %d terms of the series is %.0lf\n", n, sum);

    return 0;
} (code-box)

13. Write a program to copy the contents of one text file to another text file.

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fptr1, *fptr2;
    char filename1[100], filename2[100], c;

    printf("Enter the filename to open for reading: ");
    scanf("%s", filename1);

    // Open the input file
    fptr1 = fopen(filename1, "r");
    if (fptr1 == NULL) {
        printf("Cannot open file %s \n", filename1);
        exit(0);
    }

    printf("Enter the filename to open for writing: ");
    scanf("%s", filename2);

    // Open the output file
    fptr2 = fopen(filename2, "w");
    if (fptr2 == NULL) {
        printf("Cannot open file %s \n", filename2);
        exit(0);
    }

    // Copy contents of the input file to the output file
    c = fgetc(fptr1);
    while (c != EOF) {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }

    printf("Contents copied from %s to %s.\n", filename1, filename2);

    fclose(fptr1);
    fclose(fptr2);

    return 0;
} (code-box)

14. Write a program to display n terms of Fibonacci series, n would be provided by the user.

#include <stdio.h>

int main() {
    int n, i, t1 = 0, t2 = 1, nextTerm;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (i = 1; i <= n; ++i) {
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }

    return 0;
} (code-box)

15. Write a program to compute the sum of first n terms of the series S = 1 + 4 + 9  + 16 + 25 + ...

#include <stdio.h>

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

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++) {
        sum += i * i;
    }

    printf("The sum of the first %d terms is %d\n", n, sum);

    return 0;
} (code-box)

16. Write a program to calculate Factorial of a number given by the user.

#include <stdio.h>

int main() {
    int n, i, factorial = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) {
        factorial *= i;
    }

    printf("Factorial of %d = %llu", n, factorial);

    return 0;
} (code-box)

17. Write a program to store user given 10 integers in an array and display the smallest number.

#include <stdio.h>

#define SIZE 10

int main() {
    int arr[SIZE], i, min;

    printf("Enter %d integers:\n", SIZE);

    // Read input integers and store in array
    for (i = 0; i < SIZE; i++) {
        scanf("%d", &arr[i]);
    }

    // Find smallest number
    min = arr[0];
    for (i = 1; i < SIZE; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    printf("The smallest number is %d", min);

    return 0;
} (code-box)

18. Write a program to find the sum of digits of a given number.

#include <stdio.h>

int main() {
    int num, sum = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

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

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

    return 0;
} (code-box)

19. Write a program to print the sum and product of digits of an integer.

#include <stdio.h>

int main() {
    int num, sum = 0, product = 1, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

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

    printf("Sum of digits = %d\n", sum);
    printf("Product of digits = %d", product);

    return 0;
} (code-box)

20. Write a program to check whether a character is Vowel or Consonant.

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        printf("%c is a vowel", ch);
    } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
        printf("%c is a consonant", ch);
    } else {
        printf("Invalid input");
    }

    return 0;
} (code-box)

21. Write a program to convert a decimal number to its equivalent binary number.

#include <stdio.h>

int main() {
    int decimalNum, binaryNum = 0, base = 1, remainder;

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNum);

    while (decimalNum > 0) {
        remainder = decimalNum % 2;
        binaryNum += remainder * base;
        decimalNum /= 2;
        base *= 10;
    }

    printf("Binary equivalent = %d", binaryNum);

    return 0;
} (code-box) 

22. Write a program to find the Largest number among three numbers.

#include <stdio.h>

int main() {
    int num1, num2, num3;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2) {
        if (num1 > num3) {
            printf("%d is the largest number.", num1);
        } else {
            printf("%d is the largest number.", num3);
        }
    } else {
        if (num2 > num3) {
            printf("%d is the largest number.", num2);
        } else {
            printf("%d is the largest number.", num3);
        }
    }

    return 0;
} (code-box)

23. Write a program to copy the contents of one text file to another text file, after removing all whitespaces.

#include <stdio.h>

int main() {
    FILE *inputFile, *outputFile;
    char inputFilename[50], outputFilename[50], c;

    printf("Enter the name of the input file: ");
    scanf("%s", inputFilename);

    inputFile = fopen(inputFilename, "r");
    if (inputFile == NULL) {
        printf("Error opening input file.");
        return 1;
    }

    printf("Enter the name of the output file: ");
    scanf("%s", outputFilename);

    outputFile = fopen(outputFilename, "w");
    if (outputFile == NULL) {
        printf("Error opening output file.");
        return 1;
    }

    while ((c = fgetc(inputFile)) != EOF) {
        if (c != ' ' && c != '\t' && c != '\n') {
            fputc(c, outputFile);
        }
    }

    printf("File contents copied successfully.");

    fclose(inputFile);
    fclose(outputFile);

    return 0;
} (code-box)

Post a Comment

0Comments
Post a Comment (0)