0% found this document useful (0 votes)
64 views28 pages

Set-1 CTSD

The document contains C code snippets for various programming questions related to arrays, structures, functions, loops, conditional statements etc. It includes code to calculate the surface area of a cylinder, find roots of a quadratic equation, check divisibility of numbers, swap values using bitwise operators, find GCD and perfect numbers, check if a triangle can be formed, perform linear and binary search, bubble sort an array, implement a stack using array, define a structure to store voter details etc. The code is organized into multiple sections with comments describing each question/problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views28 pages

Set-1 CTSD

The document contains C code snippets for various programming questions related to arrays, structures, functions, loops, conditional statements etc. It includes code to calculate the surface area of a cylinder, find roots of a quadratic equation, check divisibility of numbers, swap values using bitwise operators, find GCD and perfect numbers, check if a triangle can be formed, perform linear and binary search, bubble sort an array, implement a stack using array, define a structure to store voter details etc. The code is organized into multiple sections with comments describing each question/problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

------------------------------------------------ CO-1 -----------------------------------------------------

//Q.No 1

#include<stdio.h>

int main()

float tsa;

float h,r;

printf("Enter the height of the cylinder : ");

scanf("%f",&h);

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

scanf("%f",&r);

tsa=(2*3.14*r)*(r+h);

printf("Total Surface Area of Cylinder : %.2f ",tsa);

//Q.No 2 Quadratic Equation

#include <stdio.h>

#include <math.h>

int main()

int a,b,c;

printf("Enter value of a : ");

scanf("%d",&a);

printf("Enter value of b : ");

scanf("%d",&b);

printf("Enter value of c : ");

scanf("%d",&c);

printf("Quadratic form of given coefficients is (%d)x^2 + (%d)x + (%d) = 0\n",a,b,c);


int p=pow(b,2)-(4*a*c);

if(p>=0)

float x = (-b+(sqrt(p)))/(2*a) ;

float y = (-b-(sqrt(p)))/(2*a) ;

printf("Roots of given quadratic euation are %.2f and %.2f",x,y);

else

printf("For Given quadratic eqation real roots does not exit (i.e Imaginary Roots exit) ");

//Q.No 3A

#include <stdio.h>

int main()

int a,b;

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

if(a%b==0)

int n=a/b;

n%2==0?printf("Evenly Divisible") : printf("Oddly Divisible");

else

printf("Not Divisible");

//Q.No 3B
#include <stdio.h>

int main()

float a,b;

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

float *p=&a,*q=&b;

printf("%.2f",*p+*q);

//Q.No 4A

#include <stdio.h>

int main()

float a;

scanf("%f",&a);

printf("%.2fcm",a*2.54);

//Q.No 4B

//Swap numbers using BITWISE Operators

#include <stdio.h>

int main()

int a,b;

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

a=a^b;

b=a^b;
a=a^b;

printf("Values after swaping are %d and %d",a,b);

------------------------------------------------------ CO-2 ---------------------------------------------------------------------

//Q.No 5 GCD

#include <stdio.h>

int main()

int a,b;

printf("Enter two numbers : ");

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

printf("GCD of given two numbers is : ");

if(a==0 && b==0)

printf("-1");

else if(a==0)

printf("%d",b);

else if(b==0)

printf("%d",a);

else if(a==b)

printf("%d",a);

else

int max=a>b?a:b;

for(int i=max;i>=1;i--)

if(a%i==0 && b%i==0)

{
printf("%d",i);

break;

//Q.No 6 & 8B PERFECT NUMBER

#include <stdio.h>

int main()

int a;

printf("Enter number : ");

scanf("%d",&a);

int sum=0;

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

if(a%i==0)

sum+=i;

if(sum==a)

printf("PERFECT NUMBER");

else

printf("NOT a Perfect Number");

//Q.No 7A Using DMA

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

int main()

int *p,sum=0;

p=(int *)calloc(3,sizeof(int));

printf("Enter values of angles in a triangle : ");

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

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

sum+=*(p+i);

if(sum==180)

printf("Triangle can be formed");

else

printf("Can not form Triangle");

//Q.No 7A Normal way

#include <stdio.h>

int main()

int a,b,c;

printf("Enter values of angles in a triangle : ");

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

int sum=a+b+c;

sum==180?printf("Triangle can be formed") : printf("Can not form Triangle");

//Q.No 7B

#include <stdio.h>
int main()

int a,count=0;

scanf("%d",&a);

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

if(a%i==0)

count++;

count==1?printf("Prime"):printf("Composite");

------------------------------------------- CO-3 --------------------------------------------------------

//Q.No 9 USING DMA

#include <stdio.h>

#include <stdlib.h>

int maxele(int *p,int n,int max);

int main()

int n;

printf("Enter number of elements in array : ");

scanf("%d",&n);

int *p;

p=(int *)calloc(n,sizeof(int));

printf("Enter elements : ");

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

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

int max = *p;


printf("%d",maxele(p,n,max));

int maxele(int *p,int n,int max)

if(n==0)

return max;

if(p[n-1]>max)

max=p[n-1];

maxele(p,n-1,max);

//Q.No 9 Normal Method

#include<stdio.h>

int find_max(int arr[],int n,int test_index,int max)

if(test_index==-1)

return max;

else{

if(arr[test_index]>max)

max=arr[test_index];

return find_max(arr,n,test_index-1,max);

}
int main()

int n;

printf("enter the array size : ");

scanf("%d",&n);

int arr[n];

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

printf("\nenter the element %d : ",i+1);

scanf("%d",&arr[i]);

int max=arr[n-1];

int answer=find_max(arr,n,n-1,max);

printf("\nTHE MAX ELEMENT IN THE ARRAY IS : %d ",answer);

return 0;

//Q.No 10

#include <stdio.h>

int main()

int m,n;

printf("Enter order of matrix : ");

scanf("%d %d",&m,&n);

int a[m][n];

printf("Enter elements of matrix : ");

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

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

scanf("%d",&a[i][j]);

printf("Elements in Middle row : ");

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

printf("%d ",a[m/2][i]);

printf("Elements in Middle Column : ");

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

printf("%d ",a[i][n/2]);

//Q.No 11A & 11B BUBBLE SORT Constructing Array using DMA

#include <stdio.h>

#include <stdlib.h>

int main()

int n;

printf("Enter number of Elements : ");

scanf("%d",&n);

int *p;

p=(int *)calloc(n,sizeof(int));

printf("Enter Elements : ");

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

{
scanf("%d",p+i);

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

for(int j=0;j<n-i-1;j++)

if(*(p+j) > *(p+j+1))

int temp = *(p+j);

*(p+j) = *(p+j+1);

*(p+j+1) = temp;

printf("Values after sorting : ");

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

printf("%d ",*(p+i));

//Q.No 12A Binary Search -- For This question they asked to TRACE Steps NOT to Write code --
Code is done just for Practice --

#include <stdio.h>

#include <stdlib.h>

int main()

int n;

printf("Enter number of Elements : ");

scanf("%d",&n);
int a[n];

printf("Enter Elements : ");

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

scanf("%d",&a[i]);

//If given Elements are not in sorted order then perform Bubble Sort

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

for(int j=0;j<n-i-1;j++)

if(a[j]>a[j+1])

int temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

int search;

printf("Enter element to search : ");

scanf("%d",&search);

int l=0,r=n-1;

while(l<=r)

int mid=(l+r)/2;

if(a[mid]==search)

printf("Element FOUND");

exit(0);

}
else

if(a[mid]>search)

r=mid-1;

else

l=mid+1;

printf("Element NOT Found");

//Q.No 12B Linear Search

#include <stdio.h>

#include <stdlib.h>

int main()

int n;

printf("Enter number of Elements : ");

scanf("%d",&n);

int a[n];

printf("Enter Elements : ");

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

scanf("%d",&a[i]);

int key;

printf("Enter Element to Search : ");

scanf("%d",&key);

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

{
if(key==a[i])

printf("Element Found");

exit(0);

printf("Element NOT Found");

------------------------------------------- CO-4 ------------------------------------------------------

//Q.No 13

#include <stdio.h>

#include <stdlib.h>

struct voters

long int id;

int age;

char name[30],address[100];

};

int main()

int n;

printf("Enter number of Voter : ");

scanf("%d",&n);

struct voters v[n];

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

printf("Enter name of voter %d : ",i+1);

scanf("%s",v[i].name);

printf("Enter %s's Voter ID : ",v[i].name);


scanf("%ld",&v[i].id);

printf("Enter %s's age : ",v[i].name);

scanf("%d",&v[i].age);

printf("Enter %s's Address : ",v[i].name);

scanf(" %[^\n]s",v[i].address);

printf("\n---------------------------------------- VOTER DETAILS ------------------------------------------------\n");

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

printf("\n");

printf("Name of voter %d : %s\n",i+1,v[i].name);

printf("%s's Voter ID : %ld\n",v[i].name,v[i].id);

printf("%s's age : %d\n",v[i].name,v[i].age);

printf("%s's Address : %s\n",v[i].name,v[i].address);

printf("\n");

//Q.No 14 STACK

//Here no need to write Display function

#include <stdio.h>

#include <stdlib.h>

void push(int value);

void pop();

void display();

#define n 50

int a[n];

int top=-1;

int main()

{
while(1)

int ch;

int value;

printf("\nMenu 1.PUSH 2.POP 3.Display 4.Exit");

printf("\nEnter number from menu : ");

scanf("%d",&ch);

switch(ch)

case 1 : printf("\nEnter value to push : ");

scanf("%d",&value);

push(value);

break;

case 2 : pop();

break;

case 3 : display();

break;

case 4 : exit(0);

default : printf("\n-------- Enter Value fron Menu --------\n");

void push(int value)

if(top==n-1)

{
printf("\n------- OVERFLOW ---------\n");

else

top++;

a[top]=value;

printf("\n---------- Value Inserted Successfully ---------\n");

void pop()

if(top==-1)

printf("\n---------- UNDERFLOW -------------\n");

else

printf("\n--------- Value Deleted --------- Deleted Value is %d -----------\n",a[top]);

top--;

void display()

if(top==-1)

printf("\n----------- STACk is EMPTY -------------\n");

else

printf("\n------------- Values in Stack --------------\n");

for(int i=top;i>=0;i--)
{

printf("%d ",a[i]);

printf("\n");

//Q.No 15A & 15B LINKED LIST

#include <stdio.h>

#include <stdlib.h>

void insert();

void insert_at_beginning(int value);

void insert_at_position(int value);

void insert_at_end(int value);

void delete_at_position();

void display();

struct node

int data;

struct node *next;

};

struct node *head=NULL;

int main()

while(1)

int ch;

printf("\nMenu\n1.Insert 2.Delete at Position 3.Display 4.Exit\n");

printf("\nEnter number form Menu : ");


scanf("%d",&ch);

switch(ch)

case 1 : insert();

break;

case 2 : delete_at_position();

break;

case 3 : display();

break;

case 4 : exit(0);

default : printf("\n------ Enter number from Menu ----------\n");

void insert()

int ch;

struct node *newnode;

newnode = (struct node *)malloc(sizeof(struct node));

int value;

printf("Enter value to insert : ");

scanf("%d",&value);

printf("\nMenu\nAt which position u desired to insert\n1.Insert at Beginning 2.Insert at Position


3.Insert at END\n");

printf("\nEnter number form Menu : ");

scanf("%d",&ch);
switch(ch)

case 1 : insert_at_beginning(value);

break;

case 2 : insert_at_position(value);

break;

case 3 : insert_at_end(value);

break;

default : printf("\n-------- Enter number from Menu -----------\n");

void insert_at_beginning(int value)

struct node *newnode;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

newnode->next = NULL;

if(head == NULL)

head = newnode;

else

newnode->next=head;

head=newnode;

printf("\n---------- Value Inserted Successfully ------------\n");

void insert_at_position(int value)


{

struct node *newnode;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

newnode->next = NULL;

int pos;

printf("\nEnter position of Node where u want to insert : ");

scanf("%d",&pos);

if(head == NULL)

head=newnode;

printf("\n------------------ Insertion Successfully ------------------\n");

else if(pos==1)

newnode->next=head;

head=newnode;

printf("\n------------------ Insertion Successfully ------------------\n");

else

struct node *temp1,*temp=head;

//struct node *temp2;

int co=0;

while(temp!=NULL)

co++;

if(co==pos-1)

temp1=temp;

newnode->next=temp1->next;
temp1->next=newnode;

printf("\n------------------ Insertion Successfully ------------------\n");

break;

temp=temp->next;

if(pos>co+1)

printf("\n--------------------- insertion at %d node is not possible -------------------\n",pos);

void insert_at_end(int value)

struct node *newnode;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->data = value;

newnode->next = NULL;

if(head == NULL)

head=newnode;

else

struct node *temp = head;

while(head!=NULL)

if(temp->next == NULL)

temp->next = newnode;

break;

}
temp = temp->next;

printf("\n--------- Value Inserted Successfully -----------\n");

void delete_at_position()

if(head==NULL)

printf("\n--------- List is Empty ----------\n");

else

int pos;

printf("\nEnter position of Node that u want to delete : ");

scanf("%d",&pos);

int count=0;

struct node *temp=head;

if(pos==1)

head=temp->next;

printf("\n------------- Value deleted Successfully ------ Deleted value is %d ---------\n",temp-


>data);

free(temp);

else

while(temp!=NULL)

count++;

if(count+1==pos)

{
if(temp->next!=NULL)

struct node *temp2 = temp->next;

temp->next = temp2->next;

printf("\n------------- Value Deleted Successfully ------ Deleted Value is %d -----------


\n",temp2->data);

free(temp2);

break;

else

printf("\n----------------- Node NOT Exit --------------------\n");

temp=temp->next;

if(pos > count+1)

printf("\n----------------- Node NOT Exit --------------------\n");

void display()

if(head == NULL)

printf("\n------------- List is Empty ----------------\n");

else

struct node *temp=head;

printf("\n------------ Values in List --------------\n");

while(temp!=NULL)
{

printf("%d ",temp->data);

temp=temp->next;

printf("\n");

//Q.No 16A & 16B (Case-4 belongs to 16B)

#include <stdio.h>

#include <stdlib.h>

#define n 1000

int f=-1;

int r=-1;

int a[n];

void enqueue(int value);

void dequeue();

void display();

void maxele();

int main()

while(1)

printf("Menu\n1.Enqueue\t2.Dequeue\t3.Display\t4.Max Elemen\t5.Exit\n");

printf("Enter Corresponding number from menu to perform respective operation : ");

int ch,value;

scanf("%d",&ch);

switch(ch)

{
case 1 : printf("Enter Value to push in to Stack : ");

scanf("%d",&value);

enqueue(value);

break;

case 2 : dequeue();

break;

case 3 : display();

break;

case 4 : maxele();

break;

case 5 : exit(0);

default: printf("\n--------------------- Enter number from menu ------------------\n");

void enqueue(int value)

if(r==n-1)

printf("\n---------------------- Overflow ----------------------\n");

else

r++;

a[r]=value;

printf("\n--------------------- Value inserted Successfully --------------------\n");

void dequeue()
{

if(r==f)

printf("\n-------------------- Underflow ----------------------\n");

r=f=-1;

else

f++;

printf("\n--------------- Value deleted Successfully ------ Deleted Value is %d--------------\n",a[f]);

void display()

if(r==f)

printf("\n-------------------- Stack is empty ----------------------\n");

else

printf("\n-------------------- Values in Stack ----------------------\n");

for(int i=f+1;i<=r;i++)

printf("%d ",a[i]);

printf("\n");

void maxele()
{

if(r==f)

printf("\n-------------------- Stack is empty ----------------------\n");

else

int max=a[f+1];

for(int i=f+1;i<=r;i++)

if(max<a[i])

max=a[i];

printf("\nMax Element in queue : %d\n",max);

printf("\n");

You might also like