Total Pageviews

Saturday, February 24, 2024

15. Write a program to find sum of n elements entered by the user. To write this program, allocate memory dynamically using malloc() / calloc() functions or new operator.

 #include <stdio.h>

#include <stdlib.h>


int main()

{

    int* ptr; 

    int limit; 

    int i; 

    int sum; 


    printf("Enter limit of the array: ");

    scanf("%d", &limit);

    ptr = (int*)malloc(limit * sizeof(int));


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

        printf("Enter element %02d: ", i + 1);

        scanf("%d", (ptr + i));

    }

    

    printf("\nEntered array elements are:\n");

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

        printf("%d\n", *(ptr + i));

    }

    sum = 0; //assign 0 to replace garbage value

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

        sum += *(ptr + i);

    }

    printf("Sum of array elements is: %d\n", sum);

        free(ptr); 

    return 0;

}

14. Write a program which takes the radius of a circle as input from the user, passes it to another function that computes the area and the circumference of the circle and displays the value of area and circumference from the main() function.

 #include<stdio.h>

const float PI = 3.1415927;

float area(float radius);

float circum(float radius);

void main() {

 float radius;


 printf("Enter radius: ");

 scanf("%f", &radius);

 printf("Area : %.3f\n", area(radius));

 printf("Circumference: %.3f\n", circum(radius));


}


float area(float radius) {

  return PI * radius * radius;

}


float circum(float radius) {

  return 2 * PI * radius;

}


12. Write a program that swaps two numbers using pointers.

 #include <stdio.h>


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

    int temp;

    temp = *a;

    *a = *b;

    *b = temp;

}


int main() {

    int num1, num2;

    int *ptr1, *ptr2;

    printf("Enter two numbers:\n");

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

    ptr1 = &num1;

    ptr2 = &num2;

    printf("before swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);

    swap(ptr1, ptr2);

    printf("After swapping:\n");

    printf("Number 1 = %d\n", num1);

    printf("Number 2 = %d\n", num2);


    return 0;

}

8. Write a macro that swaps two numbers. WAP to use it.

 #include <stdio.h>


#define SWAP(x, y) (x ^= y ^= x ^= y)


int main()

{

    int num1, num2;


    

    printf("Enter any two number to swap: ");

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


    printf("Values before swapping\n");

    printf("num1 = %d, num2 = %d\n\n", num1, num2);


    SWAP(num1, num2);


    printf("Values after swapping\n");

    printf("num1 = %d, num2 = %d\n", num1, num2);


    return 0;

}

7.WAP to compute the factors of a given number.

 #include <stdio.h> 


int main() 

int num, i;

    printf("Enter a positive integer: ");

    scanf("%d", &num);

    printf("Factors of %d are: ", num);

    for (i = 1; i <= num; ++i) {

        if (num % i == 0) {

            printf("%d ", i);

        }

    }

return 0;

}


6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less than 100.

 #include <stdio.h> 

int checkPrime(int N) 

int flag = 1; 

for (int i = 2; i <= N / 2; i++) { 

if (N % i == 0) { 

flag = 0; 

break; 

return flag ; 



int main() 

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

if(checkPrime(i)&&i!=1)

    printf("%d ",i);


return 0;

}


Friday, February 23, 2024

5. Write a function that checks whether a given string is Palindrome or not. Use this function to find whether the string entered by user is Palindrome or not.

 #include <stdio.h>

#include <string.h>



void Lower_case(char str[]) {

    int i = 0;

    while (str[i] != '\0') {

        if (str[i] > 64 && str[i] < 91) 

            str[i] += 32; 


        i++; 

    } 


void CheckPalindrome(char str[]) 


    

    int left = 0; 

    int right = strlen(str) - 1; 


    while (right > left) 

    {

        if (str[left++] != str[right--]) {

            printf("The String %s is not a palindrome", str);

            return;

        }

    }

    printf("The String %s is palindrome", str);

}

int main() 

{

    char str1[50];

    printf("enter a string");

    gets(str1);

    Lower_case(str1);

    CheckPalindrome(str1);

    

    return 0;

}

4. WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….

 #include <stdio.h>


int main()

{

    int n;

    int sum=0,i;

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

    scanf("%d",&n);


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

    {

        if(i%2==0)

            sum-=i;

        else

            sum+=i;

    }


    printf("The sum of the series = %d",sum);

    return 0;

}


3. WAP to compute the sum of the first n terms of the following series S = 1+1/2+1/3+1/4+……

 #include <stdio.h>


int main()

{

    double number, sum = 0, i;

 

    printf("\n enter the number ");

    scanf("%lf", &number);

    for (i = 1; i <= number; i++)

    {

        sum = sum + (1 / i);

        if (i == 1)

            printf("\n 1 +");

        else if (i == number)

            printf(" (1 / %lf)", i);

        else

            printf(" (1 / %lf) + ", i);

    }

    printf("\n The sum of the given series is %.2lf", sum);


    return 0;

}


2. WAP to reverse a number.

 #include <stdio.h>


int main()

{

    int n, reverse = 0, remainder;


  printf("Enter an integer: ");

  scanf("%d", &n);


  while (n != 0) {

    remainder = n % 10;

    reverse = reverse * 10 + remainder;

    n /= 10;

  }


  printf("Reversed number = %d", reverse);


    return 0;

}


1. WAP to print the sum and product of digits of an integer.

 #include <stdio.h>


int main()

{

    int n;

    int dig, sum,pro;

     

    printf("\nEnter an integer number :");

    scanf("%d",&n);

  

    /*Calculating Sum and Product*/

    sum=0;

    pro=1;

     

    while(n>0)

    {

        dig=n%10; /*get digit*/

        sum+=dig;

        pro*=dig;

        n=n/10;

    }

     

    printf("\nSUM of all Digits is : %d",sum);

    printf("\nPRODUCT of all digits: %d",pro);

    return 0;

}


WEST BENGAL STATE UNIVERSITY computer science NEP Syllabus

 WEST BENGAL STATE UNIVERSITY computer sciecne NEP Syllabus 


SEMESTER 2 computer sciecne NEP Syllabus 



COMJPUTER SCIECNE NEP SYLLABUS WBSU SEM2
MAJOR THEORYData Structures using C++ CLICK HERE
MAJOR PRACTICALData Structures using C++ CLICK HERE
SECPython ProgrammingCLICK HERE
MDC

WEST BENGAL STATE UNIVERSITY NEP Syllabus for B.Sc. Computer Science Major, Minor and Interdisciplinary

CMSACOR02T: Data Structures using C++ 
Theory: 45 Lectures

 Introduction (5 Lectures) 
Data Object, Abstract Data Type, Data Structures and Data Types. Types of Data Structures – Linear and non-linear Data Structures.Single and Multi-dimensional Arrays, Address Calculations, Sparse Matrices (Array Representation). 

Linked Lists (7 Lectures) 
Singly, Doubly and Circular Lists (Array and Linked representation); Operations on Lists. Sparse Matrices (Linked Representation). 

Stacks and Queues (9 Lectures) 
Implementing single / multiple stack/s in an Array; Prefix, Infix and Postfix expressions, Utility and conversion of these expressions from one to another; Applications of stack; Limitations of Array representation of stack. Array and Linked representation of Queue, De-queue, Priority Queues

 Recursion (5 lectures)
Developing Recursive Definition of Simple Problems and their implementation; Advantages and Limitations of Recursion; Understanding what goes behind Recursion (Internal Stack Implementation)

 Binary Trees (10 Lectures) 
Introduction; Properties, Binary Trees Traversals (Recursive and Non-Recursive), Binary Search Trees (Insertion, Deletion), Recursive and Iterative Traversals on Binary Search Trees; Threaded Binary Trees (Concept only); Height-Balanced Trees (Concept only). Searching, 

Sorting and Hashing (9 Lectures)
Linear Search, Binary Search, Comparison of Linear and Binary Search, Selection Sort, Insertion Sort, Bubble Sort, Comparison of Sorting Techniques. Introduction to Hashing, Resolving collusion by Open Addressing, Coalesced Hashing, Separate Chaining and simple examples.

WEST BENGAL STATE UNIVERSITY computer science NEP Syllabus

 SEMESTER 1

WEST BENGAL STATE UNIVERSITY computer sciecne NEP Syllabus 


SEMESTER 1 computer sciecne NEP Syllabus 



COMJPUTER SCIECNE NEP SYLLABUS WBSU SEM1
MAJOR THEORYComputer Fundamentals and programming with CCLICK HERE
MAJOR PRACTICALComputer Fundamentals and programming with CCLICK HERE
SECR ProgrammingCLICK HERE
MDCComputer Fundamentals

Wednesday, February 21, 2024

WAP to calculate GCD of 2 number (i) with recursion (ii) without recursion

 #include <iostream>

using namespace std;


class cl

{

private:

int n1,n2;

public:

void input()

{

cout<<"\nEnter 2 nunbers: ";

cin>>n1>>n2;

}

void gcd()


{

int g;

for(int i=1;i<=n1 && i<=n2;i++)

{

if(n1%i==0 && n2%i==0)

g=i;

}

cout<<"GCD= "<<g;

}

int gcd(int a, int b)

{

// Everything divides 0

if (a == 0)

return b;

if (b == 0)

return a;


// base case

if (a == b)

return a;


// a is greater

if (a > b)

return gcd(a-b, b);

return gcd(a, b-a);

}

};

int main()

{

cl ob;

int ch,m,n;



do

{

    cout<<"**GCD of 2 numbers**\n\n1.using Recursion \n2. Not using Recursion\n3.Exit";

cout<<"\n\nEnter ur Choice: ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\nEnter 2 numbers: ";

cin>>m>>n;

cout<<"GCD= "<<ob.gcd(m,n);

break;


case 2:

ob.input();

ob.gcd();

break;


case 3:

cout<<"\nEXIT";

break;


default:

cout<<"\nSorry! Invalid Choice";

}

}

while(ch!=3);

return 0;

}

WAP to display Fibonacci series (i)using recursion, (ii) using iteration

 #include<iostream>

using namespace std;


class cl

{


public:

int a=0,b=1,l;

void input()

{


cout<<"\nEnter the Length of Fibonacci series: ";

cin>>l;

}

void fib()

{


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

{

int c=a+b;

cout<<" "<<a;

a=b;

b=c;

}

}

int fib(int x)

{

if((x==1)||(x==0))

{

return(x);

}

else

{

return(fib(x-1)+fib(x-2));

}

}

};

int main()

{

cl ob;

int ch,n,i=0;



do

{

  cout<<"\nFibonacci series \n\n1.using Iteration \n2.using Recursion\n3.Exit";  

cout<<"\nEnter ur Choice: ";

cin>>ch;

switch(ch)

{

case 1:

ob.input();

ob.fib();

break;


case 2:

ob.input();

cout << "\nFibonnaci Series : ";

i=0;

while(i<ob.l)

{

cout << " "<<ob.fib(i);

i++;

}

break;


case 3:

cout<<"\nEXIT";

break;


default:

cout<<"\nSorry! Invalid Choice";

}

}

while(ch!=3);


return 0;

}

WAP to implement Diagonal Matrix using one-dimensional array.

 // C++ Program to print the Diagonals of a Matrix


#include <bits/stdc++.h>

using namespace std;

#define MAX 20




// Function to print the Principal Diagonal

void printPrincipalDiagonal(int mat[][MAX], int n)

{

cout << "Principal Diagonal: ";


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

for (int j = 0; j < n; j++) {


// Condition for principal diagonal

if (i == j)

cout << mat[i][j] << ", ";

}

}

cout << endl;

}


// Function to print the Secondary Diagonal

void printSecondaryDiagonal(int mat[][MAX], int n)

{

cout << "Secondary Diagonal: ";


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

for (int j = 0; j < n; j++) {


// Condition for secondary diagonal

if ((i + j) == (n - 1))

cout << mat[i][j] << ", ";

}

}

cout << endl;

}


// Driver code

int main()

{

int n ;

int a[20][20]; 

cin>>n;

cout<<"enter eleents";

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

for (int j = 0; j < n; j++) {


cin>> a[i][j] ;

}

}


printPrincipalDiagonal(a, n);

printSecondaryDiagonal(a, n);

return 0;

}


WAP to reverse the order of the elements in the stack using additional Queue.

 #include<stdio.h> 

#include<stdlib.h> 


#define MAX_SIZE 100


struct Queue

{

  int items[MAX_SIZE];

  int front;

  int rear;

};


struct Stack

{

  int items[MAX_SIZE];

  int top;

};


void enqueue (struct Queue *q, int item)

{

  if (q->rear == MAX_SIZE - 1)

    {

      printf ("Queue overflow\n");

      return;

    }


  if (q->front == -1)

    {

      q->front = 0;

    }


  q->rear++;

  q->items[q->rear] = item;

}


int dequeue (struct Queue *q)

{

  if (q->front == -1)

    {

      printf ("Queue underflow\n");

      return -1;

    }


  int item = q->items[q->front];

  q->front++;


  if (q->front > q->rear)

    {

      q->front = q->rear = -1;

    }


  return item;

}


void display (struct Queue *q)

{

  if (q->rear == -1)

    {

      printf ("Queue is empty\n");

      return;

    }


  for (int i = q->front; i <= q->rear; i++)

    {

      printf ("%d ", q->items[i]);

    }

  printf ("\n");

}


void push (struct Stack *s, int item)

{

  if (s->top == MAX_SIZE - 1)

    {

      printf ("Stack overflow\n");

      return;

    }


  s->top++;

  s->items[s->top] = item;

}


int pop (struct Stack *s)

{

  if (s->top == -1)

    {

      printf ("Stack underflow\n");

      return -1;

    }


  int item = s->items[s->top];

  s->top--;


  return item;

}


int main ()

{

  struct Queue q;

  q.front = -1;

  q.rear = -1;


  struct Stack s;

  s.top = -1;


  enqueue (&q, 1);

  enqueue (&q, 2);

  enqueue (&q, 3);

  enqueue (&q, 4);


  printf ("Queue before reversing:\n");

  display (&q);


  while (q.front != -1)

    {

      push (&s, dequeue (&q));

    }


  while (s.top != -1)

    {

      enqueue (&q, pop (&s));

    }


  printf ("Queue after reversing:\n");

  display (&q);


  return 0;

}

WAP to reverse the order of the elements in the stack using additional stack

 #include <stdio.h>  

 #define MAXSIZE 10  


 struct Stack {  

    int top;  

    int array[MAXSIZE];  

} st;  

  


 void initialize() {  

 st.top = -1;  

}  


int isFull() {     

    if(st.top >= MAXSIZE-1)  

        return 1;  

    else  

        return 0;  

}  

  

int isEmpty() {  

 if(st.top == -1)  

     return 1;  

 else  

     return 0;  

}  

   


void push(int num) {  

    if (isFull())  

        printf("Stack is Full...\n");  

    else {  

        st.array[st.top + 1] = num;  

        st.top++;  

    }  

}  

   


int pop() {  

    if (isEmpty())  

        printf("Stack is Empty...\n");  

    else {  

     st.top = st.top - 1;  

        return st.array[st.top+1];  

    }  

}  

   


void printStack(){  


 if(!isEmpty())  

{  

     int temp = pop();  

     printStack();  

     printf(" %d ", temp);  

     push(temp);  

    }  

}  


void insertAtBottom(int item) {  

    if (isEmpty()) {  

        push(item);  

    } else {  

       int top = pop();  

        insertAtBottom(item);  

        push(top);  

    }  

}  

 

void reverse() {  

    if (!isEmpty()) {  

         int top = pop();  

        reverse();  

        insertAtBottom(top);  

    }  

}  

  

int getSize(){  

 return st.top+1;  

}  


 int main() {  

 initialize(st);    

  push(1);     

    push(2);    

    push(3);  

    push(4);      

    push(5);    

    printf("Original Stack\n");  

    printStack();  

    reverse();  

    printf("\nReversed Stack\n");  

    printStack();  

    return 0;  

}  

Find out whether a list is a palindrome.

 

Find out whether a list is a palindrome.

my_reverse(L1,L2) :- my_rev(L1,L2,[]).


my_rev([],L2,L2) :- !.

my_rev([X|Xs],L2,Acc) :- my_rev(Xs,L2,[X|Acc]).

is_palindrome(L) :- reverse(L,L).