Archive for the ‘Lab 2’ Category

Problem Statement- Create four integers, four pointers to these integers and four references to them. Store these pointers and references in two arrays and print out the values of four integers using these arrays.

#include<stdio.h>
int main()
{
	int n[4],*arr[4],*ref[4];
	int i;
	printf("Enter any 4 nos\n");
	for(i=0;i<4;i++)
		scanf("%d",&n[i]);
	for(i=0;i<4;i++)
		arr[i]=&n[i];
	for(i=0;i<4;i++)
		ref[i]=&*arr[i];
	for(i=0;i<4;i++)
		printf("%d ",*ref[i]);
	return 0;
}


	
Advertisements

C program-Length of string

Posted: January 19, 2012 in Lab 2

Write a program which takes a string as input from user and returns the length of that string?


#include<stdio.h>
int main()
{
	char str[100],*ptr;
	int l=0;
	printf("Enter any string\n");
	fflush(stdin);
	gets(str);
	ptr=str;
	for(;*ptr!='\0';ptr++)
		l++;
	printf("length is %d",l);
	return 0;
}

C program for swap function

Posted: January 19, 2012 in Lab 2

Problem Statement- Complete the following program by defining the function swapb() and its prototype such that the output of the program is 20 10.
#include
void swapa(int&, int&);
void main()
{
int a=10, b=20;
swapa(a,b);
cout<<a<<b;
}

void swapa(int& x, int& y)
{
swapb(x,y);
}

#include <stdio.h>
void swapa(int *, int *);
int swapb(int *, int *);
void main()
{
   int a=10, b=20;
   swapa(&a,&b);
   printf("%d %d",a,b);
}

void swapa(int *x, int *y)
{
   swapb(&*x,&*y);
}
int swapb(int *x, int *y)
{
	int t;
	t=*y;
	*y=*x;
	*x=t;
	return(*x,*y);
}

C program-Factorial through recursion

Posted: January 19, 2012 in Lab 2

#include <stdio.h>

long factorial(long );

void main()
{
  long number = 0;
  printf("\nEnter an integer value: ");
  scanf(" %ld", &number);

  printf("\nThe factorial of %ld is %ld\n", number, factorial(number));
}

/* recursive factorial function */
long factorial(long N)
{
  if( N < 2 )
    return N;
  else 
    return N*factorial(N - 1);
}
#include<stdio.h>
#include<math.h>
void square(void);
void square()
{
int i,sq;
for(i=1000;i<10000;i++)
{
if(i%10==(i/10)%10&&(i/100)%10==(i/1000)%10)
{
sq=sqrt(i);
if((sq*sq)==i)
printf("%d\n",i);
}
}
}
void main()
{
square();
}

C Program to find no. of digits

Posted: January 19, 2012 in Lab 2
#include<stdio.h>
void digitcount(int *);
void digitcount(int *n)
{
int count=0;
while(*n>0)
{
*n=*n/10;
count++;
}
printf("%d",count);
}
void main()
{
int n;
printf("Enter no.\n");
scanf("%d",&n);
digitcount(&n);
}

#include<stdio.h>
void week(int * ,int*);
void week(int *a,int *c)
{
int b;
b=*a%*c;
if(b==0)
printf("%d",*a);
else
printf("%d",*a+*c-b);
}
void main()
{
int i,j;
printf("Enter the value of i and j");
scanf("%d%d",&i,&j);
week(&i,&j);
}
#include<stdio.h>
#include<math.h>
int main()
{
long long b=2,n,i=0,ans=0,*ptr;
printf("Enter no.\n");
scanf("%lld",&n);
ptr=&n;
while(*ptr>0)
{
ans=ans+(*ptr%10)*pow(b,i);
*ptr=*ptr/10;
i++;
}
printf("%lld\n",ans);
return 0;
}