Cse Da1 Theory
Cse Da1 Theory
Regd No : 21BPS1014
#include<stdio.h>
#include<stdlib.h>
int main(void)
}
if(!valid_date(day2, mon2, year2))
exit(0);
{ day_dif=day2-day1;
else{
day_dif=day1-day2;
if(mon2>mon1)
mon_dif=mon2-mon1;
else{
mon_dif=mon1-mon2;
printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);
if(year_diff>=19)
else
return 0; // returns 0
}
int valid_date(int day, int mon, int year)
is_leap = 1;
if (mon == 2)
is_valid = 1;
is_valid = 0;
is_valid = 0;
}
// check for days in rest of the months
is_valid = 0;
else
is_valid = 0;
else
is_valid = 0;
return is_valid;
OUTPUT :
-----------------------------------------------------------------------------------------------------------------------------------------------------------
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>
int main()
{
int arr2[1000];
int a1 = 0, a2 = 0, i, flag = 0;
int choice;
do
flag = 0;
scanf("%d", &choice);
switch (choice)
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;
scanf("%c", &ch);
if (arr1[i] == ch)
flag = 1;
break;
}
}
++i;
--a1;
if (arr2[i] == (int)ch)
break;
++i;
}
--a2;
printf("Array1: ");
char_display(arr1, a1);
printf("");
printf("Array2: ");
int_display(arr2, a2);
printf("");
break;
scanf("%c", &ch);
scanf("%c", &mch);
if (arr1[i] == ch)
{
arr1[i] = mch;
flag = 1;
break;
if (flag == 0)
continue;
if (arr2[i] == (int)ch)
break;
{
arr2[i] = arr2[i + 1];
++i;
--a2;
printf("Array1: ");
char_display(arr1, a1);
printf("");
printf("Array2: ");
int_display(arr2, a2);
printf("");
break;
---------------------------------------------------------------------------------------------------------------------------------------------------------
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>
Int fee ;
};
Void main () {
For (x=0;x<3;;x++0)
Scanf(“%s”,dashboard[x].event);
Scanf(“%i”,&dashboard[p].fee);
Scanf(“%s”,&dashboard[p].venue );
Scanf(“%s”,&dashboard[p].cordinator name);
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>
a=a+b;
b=a-b;
a=a-b;
int main()
printf("enter a ,b values:");
scanf("%d%d",&a,&b);
//Printing a,b values before function call
func1(a,b);//actual parameters
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>
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
int main()
int a,b;
printf("enter a ,b values:");
scanf("%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;
if (eq_parts == n)
continue;
if (sum == n)
return true;
return false;
int getSum(int n)
int x;
int sum = 0;
while (n != 0) {
n = n / 10;
}
x=sum-9;
return (x);
int main()
int kpno,x;
printf(" -------------------------------------------------------");
scanf("%d",&kpno);
if (chkkaprekar(kpno)==true)
else
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()
float Division[10];
scanf("%d", &Size);
scanf("%d", &a[i]);
scanf("%d", &b[i]);
scanf("%d", &c[i]);
Addition [i]= *ptr1 + *ptr2 + *ptr3; //*ptr refers to the value at address
ptr2++;
ptr3++;
return 0;
Output
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 *ptr1,*ptr2;
scanf("%d", &Size);
scanf("%d", &a[i]);
scanf("%d", &b[i]);
ptr1 = a;
ptr2 = b;
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
ptr2++;
for(i = 0; i < Size; i++) //printing the values of array 1 and array 2
return 0;
Output
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
#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
CODE :