0% found this document useful (0 votes)
33 views10 pages

Harsh

Uploaded by

Harsh Mehta
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)
33 views10 pages

Harsh

Uploaded by

Harsh Mehta
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/ 10

Name : Harsh Mehta

Sem : 4th
Div : B
Roll no : 91
Sub : IOT(402)

q-1:
Write a C program to create, initialize and use pointers.
#include<stdio.h>
int main()
{
int n, i;
int *ptr = &n;
printf("enter the value= ");
scanf("%d", &n);

printf("value of the variable = %d\n", *ptr);


return 0;

q-2
Write a C program to add two numbers using pointers.
#include<stdio.h>
int main() {
int a,b;
int *ptr = &a;
int *p = &b;
printf("enter the value of a and b= ");
scanf("%d %d", &a,&b);

printf("value of the sum = %d\n", *ptr + *p);

return 0;
}

q-3
Write a C program to swap two numbers using pointers.
#include<stdio.h>
int main() {
int a,b,temp;
int *ptr = &a;
int *p = &b;
printf("enter the value of a and b= ");
scanf("%d %d", &a,&b);

temp=*ptr;
*ptr=*p;
*p=temp;

printf("swap value = %d %d \n", *ptr, *p);


return 0;
}
Q-4
Write a C program to input and print array elements using a pointer.
#include<stdio.h>
int main() {
int n[5], i;
// int *ptr;
for (i = 0; i < 5; i++) {
printf("enter the value= ");
scanf("%d", &n[i]);
}

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


{
int *ptr = &n[i];
printf("value of the variable = %d\n", *ptr);
}

return 0;
}

Q-5
Write a C program to reverse an array using pointers

#include<stdio.h>
#define n 5
int main()
{

int a[n],i;
for (i = 0; i < n; i++)
{
printf("Enter the elements =");
scanf("%d", &a[i]);
}

int *start = a;
int *end = a + n - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
printf("Reversed array: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}

Q-6
Write a C program to search an element in array using pointers
#include<stdio.h>
#define n 5
int main() {
int i, search;
int a[n];
for (i = 0; i < n; i++)
{
printf("Enter the elements of the array: ");
scanf("%d", &a[i]);
}
printf("Enter the element to search: ");
scanf("%d", &search);
int *ptr = a;
for (i = 0; i < n; i++)
{
if (*ptr == search)
{
printf("Element %d found at index %d.\n", search, i);
return 0;
}
ptr++; }
printf("Element %d not found in the array.\n",search);
return 0;
}
q-7
Write a C program to find length of string using pointers.

#include<stdio.h>
int main()
{
char s1[20];
char *p=s1;

printf("enter the string=");


scanf("%s",s1);

int length=0;

while(*p!='\0')
{
length++;
p++;
}
printf("length of string=%d",length);
return 0;
}

q-8
Write a C program to copy one string to another using pointers.

#include<stdio.h>
int main()
{
char s1[20],s2[20];
char *p1=s1;
char *p2=s2;
printf("enter the string=");
scanf("%s",s1);

while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';

printf("\nstring 1=%s",s1);
printf("\nstring 2=%s",s2);
return 0;
}

q-9
concatenate string without using inbuilt string function.

#include<stdio.h>
int main()
{
char s1[20], s2[20];
char *p1 = s1;
char *p2 = s2;

printf("Enter first string: ");


scanf("%s", s1);

printf("Enter second string: ");


scanf("%s", s2);
while (*p1 != '\0')
{
p1++;
}
while (*p2 != '\0')
{
*p1 = *p2;
p1++;
p2++;
}

*p1 = '\0';
printf("Concatenated string: %s\n", s1);
return 0;
}

Function

Q-1
Write a C program to find cube of any number using function

#include <stdio.h>
float cube()
{
int n,result;
printf("Enter a number: ");
scanf("%d", &n);

result=n*n*n;

printf("Cube of %d = %d\n", n,result);

int main()
{
cube();
return 0;
}

q-2
Write a C program to find diameter, circumference and area of circle using functions.

#include <stdio.h>
#define PI 3.14

float area_circle()
{
float radius,di,ci,ar;

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


scanf("%f", &radius);

di = 2 * radius;
ci=2 * PI * radius;
ar=PI * radius * radius;

printf("diameter of the circle= %.2f\n", di);


printf("circumference of the circl= %.2f\n", ci);
printf("area of the circle= %.2f\n", ar);
}
int main()
{
area_circle();
return 0;
}

q-3
Write a C program to find maximum and minimum between two numbers using functions.

#include <stdio.h>
int max(int a,int b)
{
if (a > b) {
return a;
}
else{
return b;
}
}
int min(int a,int b)
{ if (a < b) {
return a;
}
else{
return b;
}
}
int main() {
int n1,n2, large,small;

printf("Enter the first number: ");


scanf("%d", &n1);

printf("Enter the second number: ");


scanf("%d", &n2);
large = max(n1,n2);
small = min(n1,n2);
printf("large number between %d and %d is= %d\n", n1,n2, large);
printf("small number between %d and %d is=%d\n", n1,n2, small);

return 0;
}

Q-4
Write a C program to check whether a number is
even or odd using functions.

#include<stdio.h>
int odd_even()
{
int a,n;

printf("enter value=");
scanf("%d",&n);
if(n%2==0)
{
printf(" %d is even number",n);
}
else
{
printf("\n %d is odd number",n);
}
}

int main()
{
int result;

odd_even();

return 0;}
q-5
Write a C program to find power of any number using recursion.

#include<stdio.h>
int power(int,int);
int power(int base,int a)
{
if(a>0)
{
return (base *power(base,a-1));
}
else
{
return 1;
}
}
int main()
{
int base,a,result;
printf("enter base value=");
scanf("%d",&base);

printf("enter power value=");


scanf("%d",&a);

result=power(base,a);
printf("%d",result);
return 0;
}

Bitwise
Q-1
Write a C program to count total zeros and ones in a binary number.

#include <stdio.h>
#define s sizeof(int) * 8

Int main(
{
int num, zeros, ones, i;
printf("Enter any number: ");
scanf("%d", &num);
zeros = 0;
ones = 0;
for(i=0; i<s; i++)
{
if((num & 1) ==1)
Ones++;
Else
Zeroes ++;
num = num >> 1;
}
printf("Total zero bit is %d\n", zeros);
printf("Total one bit is %d", ones);
return 0;

Q-2
Write a C program to rotate bits of a given number.

#include <stdio.h>
#define INT_BITS 32 // Assuming 32-bit integers
unsigned int leftRotate(unsigned int num, unsigned int d) {
return (num << d) | (num >> (INT_BITS - d));
}
unsigned int rightRotate(unsigned int num, unsigned int d) {
return (num >> d) | (num << (INT_BITS - d));
}
int main()
{
unsigned int num, d;
printf("Enter a number: ");
scanf("%u", &num);
printf("Enter the number of bits to rotate: ");
scanf("%u", &d);
printf("Left rotated number: %u\n", leftRotate(num, d));
printf("Right rotated number: %u\n", rightRotate(num, d));
return 0;
}

Q-4
3. Write a C program to convert decimal to binary number system using bitwise operator.

#include <stdio.h>
int main()
{
int decimal_num, c, result;

printf("Enter an integer in decimal number system:\n");


scanf("%d", &decimal_num);

for (c = 9; c >= 0; c--)


{
result = decimal_num >> c;
if (result & 1)
printf("1" );
else
printf("0");
}
printf("\n");
return 0;
}

q-4
Write a C program to check whether a number is even or odd using functions.

#include <stdio.h>
void swap(int *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}

int main() {
int num1, num2;

printf("enter the first number: ");


scanf("%d", &num1);
printf("enter the second number: ");
scanf("%d", &num2);

swap(&num1, &num2);
printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);

return 0;
}

q-5
Write a C program to check whether a number is even or odd using a bitwise operator.

#include <stdio.h>
int odd_even()
{
int n5;
printf("Enter a number: ");
scanf("%d", &n);

if ((n & 1) == 0)
{
printf("%d is even.\n", n);
}
else {
printf("%d is odd.\n", n);
}
}

int main()
{
odd_even();

return 0;
}

You might also like