Harsh
Harsh
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);
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);
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;
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;
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;
*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;
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;
di = 2 * radius;
ci=2 * PI * radius;
ar=PI * radius * radius;
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;
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);
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;
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;
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;
}