0% found this document useful (0 votes)
2 views

Cse Da1 Theory

The document contains various C programming tasks, including calculating the difference between two dates, managing character arrays with operations reflecting on ASCII values, and event management using structures. It also covers concepts of local and global variables, Kaprekar numbers, and arithmetic operations on arrays. Each section includes code snippets and expected outputs for better understanding.
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)
2 views

Cse Da1 Theory

The document contains various C programming tasks, including calculating the difference between two dates, managing character arrays with operations reflecting on ASCII values, and event management using structures. It also covers concepts of local and global variables, Kaprekar numbers, and arithmetic operations on arrays. Each section includes code snippets and expected outputs for better understanding.
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/ 27

CSE DA1 THEORY

Name : Bandugula Vinay Babu

Regd No : 21BPS1014

1) Accomplish the following tasks using a C Program.


• Calculate the number of days, months and years taken for you to join in VIT from your
date of birth.
• Also check whether the year of date of birth or date of joining is a leap year (3Marks)
• If the years are exceeding 19, and also you have born on the leap year, then print “I am lucky adult and
adult means responsibility Otherwise print “ I am aspiring to become
an responsible adult“.

#include<stdio.h>

#include<stdlib.h>

int valid_date(int date, int mon, int year);

int main(void)

int day1, mon1, year1,day2, mon2, year2;

int day_dif, mon_dif, year_dif;

printf("Enter date of joining in VIT (DD/MM/YYYY): ");

scanf("%d/%d/%d", &day1, &mon1, &year1);

printf("Enter your date of birth (DD/MM/YYYY): ");

scanf("%d/%d/%d", &day2, &mon2, &year2);

if(!valid_date(day1, mon1, year1))

printf("First date is invalid.");

}
if(!valid_date(day2, mon2, year2))

printf("Second date is invalid.");

exit(0);

if(day2 > day1)

{ day_dif=day2-day1;

else{

day_dif=day1-day2;

if(mon2>mon1)

mon_dif=mon2-mon1;

else{

mon_dif=mon1-mon2;

year_dif = year1 - year2;

printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);

if(year_diff>=19)

printf("I am lucky adult and adult means responsibility.");

else

printf("I am aspiring to become an responsible adult .");

return 0; // returns 0

}
int valid_date(int day, int mon, int year)

int is_valid = 1, is_leap = 0;

if (year >= 1800 && year <= 9999)

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

is_leap = 1;

if(mon >= 1 && mon <= 12)

// check for days in feb

if (mon == 2)

if (is_leap && day == 29)

is_valid = 1;

else if(day > 28)

is_valid = 0;

// check for days in April, June, September and November

else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)

if (day > 30)

is_valid = 0;

}
// check for days in rest of the months

// i.e Jan, Mar, May, July, Aug, Oct, Dec

else if(day > 31)

is_valid = 0;

else

is_valid = 0;

else

is_valid = 0;

return is_valid;

OUTPUT :

Enter the date of joining the VIT : 13/08/2021

Enter your date of birth : 01/07/2004

No of days months and years taken to join VIT : 12 01 17

Your date birth is leap year

I am lucky and adult means responsibility

-----------------------------------------------------------------------------------------------------------------------------------------------------------
1:
B
Create two arrays where the following operations on first array are reflected on the second array as per the
specification
1. Addition of characters in the first array should result in addition of the ASCII values of the same
characters in the second array.
2. Deletion of characters in the first array should result in the deletion of the ASCII values of the same
characters in the second array
2. Modification of characters in the first array should result in the deletion of the ASCII values of the same
characters in the second array

#include <stdio.h>

void int_display(int arr[], int n) // Function to display integer array

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

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

void char_display(char arr[], int n) // Function to display character array

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

printf("%c ", arr[i]);

int main()
{

char arr1[1000], ch, mch;

int arr2[1000];

int a1 = 0, a2 = 0, i, flag = 0;

int choice;

do

flag = 0;

printf("1.Add character2.Delete character3.Modify character4.ExitChoose your option: ");

scanf("%d", &choice);

switch (choice)

case 1: // To add character

printf("Enter character to be added: ");

scanf("%c", &ch);

arr1[a1++] = ch;

arr2[a2++] = (int)ch;
printf("Array1: ");

char_display(arr1, a1);

printf("");

printf("Array2: ");

int_display(arr2, a2);

printf("");

break;

case 2: // To delete character

printf("Enter character to be deleted: ");

scanf("%c", &ch);

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

if (arr1[i] == ch)

flag = 1;

break;

}
}

while ((i < a1) && flag)

arr1[i] = arr1[i + 1];

++i;

--a1;

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

if (arr2[i] == (int)ch)

break;

while (i < a2)

arr2[i] = arr2[i + 1];

++i;

}
--a2;

printf("Array1: ");

char_display(arr1, a1);

printf("");

printf("Array2: ");

int_display(arr2, a2);

printf("");

break;

case 3: // To modify character

printf("Enter character to be modified: ");

scanf("%c", &ch);

printf("Enter the character to which it should be modified: ");

scanf("%c", &mch);

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

if (arr1[i] == ch)

{
arr1[i] = mch;

flag = 1;

break;

if (flag == 0)

printf("Character couldnt be found!!!");

continue;

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

if (arr2[i] == (int)ch)

break;

while (i < a2)

{
arr2[i] = arr2[i + 1];

++i;

--a2;

printf("Array1: ");

char_display(arr1, a1);

printf("");

printf("Array2: ");

int_display(arr2, a2);

printf("");

break;

} while (choice != 4);

---------------------------------------------------------------------------------------------------------------------------------------------------------
1C
Create a program for managing an event using the arrays. The program should print the available events for
registration to the student. Once when the event is selected by the student, then the registration fee, venue
and the coordinator name and contact details need to be printed for the student with the option to select the
next event. Once the student selects all the events, his dashboard should have all his selected events with all
the details of the events.

#include<stdio.h>

#include<conio.h>

Struct event info {

Char event [25];

Int fee ;

Char venue [25];

Char coordinator name [25];

Long int contact ;

};

Void main () {

Struct event info dashboard [10];

Printf (“\n enter student information “);

For (x=0;x<3;;x++0)

Printf(“\n enter even name : “);

Scanf(“%s”,dashboard[x].event);

Printf(“\n enter fee “);

Scanf(“%i”,&dashboard[p].fee);

Printf(“\n enter venue “);

Scanf(“%s”,&dashboard[p].venue );

Printf(“\n enter coordinator name “);

Scanf(“%s”,&dashboard[p].cordinator name);

Printf(“\n enter contact “);

Scanf(“%i”,&dashboard[p].contact);

Getch ();

---------------------------------------------------------------------------------------------------------------------------------------------------------
2A
Define the local and global variables in C with a function or set of functions to print the following
occurrences. The value of the local or global variables are assigned from random number
generator user defined functions
• Same value of variables inside and outside functions
• Different value of variables inside and outside functions on modification of variables with in function

ANSWER :

Local variables:

The variables which are declared inside the function and those variables are accessed in that function only is
called local variables.

Global variables:

The variables which are declared outside the function and those variables are accessed throughout the
program is called global variables.

a) program:

call by value:

#include<stdio.h>

void func1(int a,int b)//formal parameters

printf(" before swapping %d and %d ",a,b);

a=a+b;

b=a-b;

a=a-b;

printf(" after swapping %d and %d ",a,b);

int main()

int a,b;//local variables

printf("enter a ,b values:");

scanf("%d%d",&a,&b);
//Printing a,b values before function call

printf("before function call a,b values are: %d %d ",a,b);

//Passing a,b values to formal parameters

func1(a,b);//actual parameters

//Printing a,b values after function call

.....no change of values after function call...

printf(" after function call a,b values are %d %d",a,b);

Output:

enter a,b values: 10 20 before function call a,b values are: 10 20 before swapping 10 and 20 after swapping
20 and 10 after f

b)program :

call by reference:

#include<stdio.h>

void func1(int *a,int *b)//formal parameters

printf(" before swapping %d and %d ",*a,*b);

*a=*a+*b;

*b=*a-*b;

*a=*a-*b;

printf(" after swapping %d and %d ",*a,*b);

int main()

int a,b;

printf("enter a ,b values:");

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

//Printing a,b before function call


printf("before function call a,b values are: %d %d ",a,b);

func1(&a,&b);//actual parameters (passing address)

//Printing a,b after function call

......values are modified after function call......

printf(" after function call a,b values are %d %d",a,b);

Output:

enter a,b values: 10 20 before function call a,b values are: 10 20 before swapping 10 and 20 after swapping
20 and 10 after f

-----------------------------------------------------------------------------------------------------------------------------------------------------------

2B
Kaprekar number is a number is the number which gives the same value of its own when it is added
using digit sum method. Ex1: 92 = 81, and 8 + 1 = 9
Ex2 : 2972 = 88209 and 88 + 209 = 297
Find the kaprekar numbers till the given number n using C Program with functions. If you find a kaprekar
number, then remove the digit 9 from the number and find the digit sum. Compare the original digit sum that
is found while the kaprekar number is checked and the second digit sum after removing the number 9. If
they are found same, then print “no change”. If they are different, then print “changed”. The function should
find the kaprekar number and the remaining checking should be found in main function

Kaprekar Number : Consider an n-digit number k . Square it and add the right n digits to the left n or (n-1) digits. If
the resultant sum is k , then k is called a Kaprekar number .

C code snippet :

# include <stdio.h>

# include <stdbool.h>

# include <math.h>
bool chkkaprekar(int n)

if (n == 1)

return true;

int sqr_n = n * n;

int ctr_digits = 0;

while (sqr_n)

ctr_digits++;

sqr_n /= 10;

sqr_n = n*n;

for (int r_digits=1; r_digits<ctr_digits; r_digits++)

int eq_parts = pow(10, r_digits);

if (eq_parts == n)

continue;

int sum = sqr_n/eq_parts + sqr_n % eq_parts;

if (sum == n)

return true;

return false;

int getSum(int n)

int x;

int sum = 0;

while (n != 0) {

sum = sum + n % 10;

n = n / 10;

}
x=sum-9;

return (x);

int main()

int kpno,x;

printf("Check whether a given number is a Kaprekar number: ");

printf(" -------------------------------------------------------");

printf("Input a number: ");

scanf("%d",&kpno);

if (chkkaprekar(kpno)==true)

printf("%d is a Kaprekar number. ",kpno);

else

printf("%d is not a Kaprekar number. ",kpno);

printf("Sum of digit after removing the number 9 : %d ", getSum(kpno));

if(x==kpno)

printf("no change");

else

printf("changed");

return 0;

Output :
Check whether a given number is a Kaprekar number: Input a number: 297 297 is a Kaprekar number. Sum of digit
after removing

3A
Perform the following operations on three arrays
and print the results
1. Arithmetic operations on the values in the same index of various arrays using the address of
variables
2. Print the higher of two values in the same index of two arrays using the address of variables
10 1Y
Difficult (D)
2,3

CODE :
1.

#include<stdio.h>

int main()

int Size, i, a[10], b[10],c[10];

int Addition[10], Subtraction[10], Multiplication[10], Module[10];

float Division[10];

int *ptr1,*ptr2,*ptr3; // these are pointers to array address

printf(" Please Enter the Size of the Array");

scanf("%d", &Size);

printf("Please Enter the First Array Elements");

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

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

printf(" Please Enter the Second Array Elements");

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


{

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

printf(" Please Enter the Third Array Elements");

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

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

ptr1 = a; //point to the base address of array a

ptr2 = b; //point to the base address of array b

ptr3 = c; //point to the base address of array b

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

Addition [i]= *ptr1 + *ptr2 + *ptr3; //*ptr refers to the value at address

Subtraction [i]= *ptr1 - *ptr2 - *ptr3;

Multiplication [i]= *ptr1 * *ptr2 * *ptr3;

Division [i] = *ptr1 / *ptr2 / *ptr3;

Module [i] = *ptr1 % *ptr2 % *ptr3;

ptr1++; //ptr is incremented

ptr2++;

ptr3++;

printf(" Add Sub Multi Div Mod");

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

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

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

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

printf("%.2f ", Division[i]);

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


}

return 0;

Output

Please Enter the Size of the Array

Please Enter the First Array Elements

Please Enter the Second Array Elements

Please Enter the Third Array Elements

Add Sub Multi Div Mod

3 -1 1 1.00 0

6 -2 8 0.00 0

9 -3 27 0.00 0

2.)
CODE

#include<stdio.h>

int main()

int Size, i, a[10], b[10];

int *ptr1,*ptr2;

printf(" Please Enter the Size of the Array");

scanf("%d", &Size);

printf("Please Enter the First Array Elements");

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

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

printf(" Please Enter the Second Array Elements");

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

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

ptr1 = a;

ptr2 = b;

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

if(*ptr1 > *ptr2 ) //compare the values at address which pointer points to

b[i] = *ptr1; //if value at pointer 1 is higher then value of array b is changed to value at 1

}
else

a[i] = *ptr2; //else value of array a is changed to value at pointer 2

ptr1++; //pointer is incremented

ptr2++;

printf(" array 1 array 2");

for(i = 0; i < Size; i++) //printing the values of array 1 and array 2

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

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

return 0;

Output

Please Enter the Size of the Array

Please Enter the First Array Elements

Please Enter the Second Array Elements

5
4

array 1 array 2

22

55

66

Screen

Please Enter the size of the Array 3 Please Enter the First Array Elements 2 4 6 Please Enter the Second Array
Elements 1 5 4
3B

Perform the following operations on three arrays


and print the results
1. Square the numbers and store the array elements in the same index using address of the variables
2. Print the highest element from all the three arrays using the address of the variables

#include <stdio.h>
int main() {
// Initializing an array
int array1[5], array2[5], array3[5];
// Taking input of array elements
printf("Tell the elements of first array. Input 5 elements\n");
for (int i = 0; i<5; i++){
int k;
scanf("%d", &k);
array1[i] = k;
}
printf("\n");
printf("Tell the elements of second array. Input 5 elements\n");
for (int i = 0; i<5; i++){
int k;
scanf("%d", &k);
array2[i] = k;
}
printf("\n");
printf("Tell the elements of third array. Input 5 elements\n");
for (int i = 0; i<5; i++){
int k;
scanf("%d", &k);
array3[i] = k;
}
printf("\n");
// Using addresses of variables to find the maximum number in all the arrays
int max = 0;
for (int k = 0; k<5; k++){
int a = array1[k];
int* ptr;
ptr = &a;
if (*ptr>=max){
max = *ptr;
}
}
for (int k = 0; k<5; k++){
int a = array2[k];
int* ptr;
ptr = &a;
if (*ptr>=max){
max = *ptr;
}
}
for (int k = 0; k<5; k++){
int a = array3[k];
int* ptr;
ptr = &a;
if (*ptr>=max){
max = *ptr;
}
}
//printing the maximum value of all the three arrays
printf("The maximum value in all of the three arrays is %d", max);
return 0;
}
Output
Tell the elements of first array. Input 5 elements
1
4
53
3
46
Tell the elements of second array. Input 5 elements
80
2
52
7
41
Tell the elements of third array. Input 5 elements
79
101
121
3
2
The maximum value in all of the three arrays is 121
3C

Perform the following operations on three arrays


and print the results
1. Make an even numbered array even if the odd numbers are entered from user using the address of the
variables
2. Print the lowest element from all the three arrays using the address of the variables

CODE :

You might also like