You are on page 1of 14

Dev Angiras- CSA sem 1

Assignment 5
Code:
//Problem 1
#include <stdio.h>
void main()
{
int a, b, *x, *y, temp;
printf("enter two numbers a and b:\n");
scanf("%d%d", &a, &b);
x = &a;
y = &b;
temp = *y;
*y = *x;
*x = temp;
printf("after swapping a=%d b=%d", a, b);
}

//Problem 2
#include <stdio.h>
#include <string.h>
void main()
{
char str[50], temp;
char *p1, *p2;
printf("enter the string:\n");
gets(str);
int l = strlen(str);
for (int i = 0; i < l / 2; i++)
{
p1 = &str[i];
p2 = &str[l - i - 1];
temp = *p2;
*p2 = *p1;
*p1 = temp;
}
printf("reversed string :");
puts(str);
}

//Problem 3
#include <stdio.h>
void main()
{
int a[5];
int *p = a;
printf("enter the 5 number in array:\n");
for(int i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("addition operator:");
p = p + 2;
printf("\nadding two to the pointer gives 3rd index value of array:%d", *p);
printf("\nsubtraction operator:");
p = p - 1;
printf("\nsubtracting one from the pointer gives 2rd index value of
array:%d", *p);
printf("\nmultiplication operator: Not Valid");
printf("\ndivision operator: Not Valid");
}

//Problem 4
#include <stdio.h>
void main()
{
int a[5];
int b[5];
int *p;
printf("enter the 5 number in array:\n");
for(int i=0;i<5;i++)
{
scanf("%d",&a[i]);
p=&a[i];
b[i]=*p;
}
printf("copied array:\n");
for(int i=0;i<5;i++)
{
printf("%d ",b[i]);
}

//Problem 5
#include <stdio.h>
void main()
{
char a[50];
char *p=a;
int l = 0;
printf("enter the string:\n");
gets(a);
while (*p != '\0')
{
l++;
p++;
}
printf("length of string = %d",l);
}

//Problem 6
#include <stdio.h>
void main()
{
int a[5] = {1, 2, 3, 4, 5};
int i = 5;
int *pa;
int *pi;
pi = &i;
printf("array of int: 1 2 3 4 5\nint variable value i =5");
printf("\npointer to an interger is assigned by 'pi = &i' and value is retreived
by *pi: %d", *pi);
pa = a;
printf("\npointer to an array of interger is assigned simply by 'pa = a' and
value is retreived by *pa: %d", *pa);
printf("\nno meaning of increment or decrement in integer pointer");
pa++;
printf("\nincrementing the pointer to an array of integer gives the value of
next array index: %d", *pa);
}

Output:
1.

2.
3.

4.

5.

6.
Dev Angiras- CSA sem 1
Assignment 6
Code:
//Problem 1
#include <stdio.h>
void main()
{
int a[5], i, max;
int *p;
printf("enter the array elements:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &a[i]);
}
max = a[0];
p = &max;
for (i = 0; i < 5; i++)
{
if (max < a[i])
*p = a[i];
}
printf("max value in array is: %d", *p);
}
//Problem 2
#include <stdio.h>
void ref(int *a);
void val(int a);
void main()
{
int a;
printf("Enter a number:\n");
scanf("%d", &a);
printf("Call by value");
printf("Before function call a=%d \n", a);
val(a);
printf("After function call a=%d \n\n\n", a);
printf("Call by refernce\n");
printf("Before function call a=%d \n", a);
ref(&a);
printf("After function call a=%d \n", a);
}
void val(int a)
{
printf("Before adding value inside function a=%d \n", a);
a = a + 20;
printf("After adding value inside function a=%d \n", a);
}
void ref(int *a)
{
printf("Before adding value inside function a=%d \n", *a);
(*a) += 20;
printf("After adding value inside function a=%d \n", *a);
}

//Problem 3
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n, i;
int sum = 0;
printf("enter the length of array\n");
scanf("%d", &n);
int *ar = (int *)malloc(n * sizeof(int));
printf("enter the array elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &ar[i]);
sum += ar[i];
}
printf("sum of numbers in array is %d", sum);
}

//Problem 4
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n;
char *c, temp;
printf("enter the length of string\n");
scanf("%d", &n);
c = (char *)malloc(n * sizeof(char));
printf("enter the string\n");
scanf("%c", &temp);
gets(c);
printf("Entered sting is:%s", c);
free(c);
}

//Problem 5
#include <stdio.h>
int sum(int n)
{
if (n <= 1)
return n;
return n + sum(n - 1);
}
void main()
{
int n;
printf("Enter the natural number upto which sum is required: ");
scanf("%d", &n);
printf("sum = %d", sum(n));
}
//Problem 6
#include <stdio.h>
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
void main()
{
int a, b;
printf("Enter the two numbers of which GCD is required: ");
scanf("%d%d", &a, &b);
printf("GCD = %d", gcd(a, b));
}
//Problem 7
#include <stdio.h>
#include <math.h>
int dto(int n)
{
int on = 0, i = 1;
int r;
while (n != 0)
{
r = n % 8;
n = n / 8;
on = on + (r * i);
i = i * 10;
}
return on;
}
int otd(int on)
{
int dn = 0, i = 0;
while (on != 0)
{
dn += (on % 10) * pow(8, i);
++i;
on /= 10;
}
i = 1;
return dn;
}
void main()
{
int dn, on;
printf("Please Enter the decimal number you want to convert: \n");
scanf("%d", &dn);
printf("Octal Number = %d\n\n", dto(dn));
printf("Please Enter the octal number you want to convert: \n");
scanf("%d", &on);
printf("Decimal Number = %d", otd(on));
}

Output:
1.

2.
3.

4.

5.

6.

7.

You might also like