JAYASHREE ORACLE CERTIFIED JAIN UNIVERSITY

Friday, October 8, 2010

C LAB PROGRAM PART A

/* 1.Program to print the reverse of an Interger */
#include
#include
void main()
{
long rev, n , num;
int r;
clrscr();
printf("\n\nProgram to print the reverse of an integer \n\n\n\n");
printf("Enter a Number ");
scanf("%ld",&num);
rev=0;
do
{
r = num %10;
num = num/10;
rev = rev*10 + r;
}
while (num >0);
printf("\n\nThe reverse is %ld ",rev);
getch();
}



---------------------------------------------------------------------

/* 2.Program to print the odd and even series of N Numbers */
#include
#include
void main()
{
int i,n;
clrscr();
printf("\n\n Program to print the odd and even series of No's\n\n\n\n");
printf("Enter the last Number ");
scanf("%d",&n);

printf("\n\nThe Odd Series\n\n");
for(i=1;i<=n; i++)
{
if(i%2==0)
printf("%3d",i);
else
printf("%3d",i);
}
getch();
}

----------------------------------------------------------------------
/*3:Get a string and convert the lowercase to uppercase and vice--versa using getchar() and putchar().


#include
#include
#include
void main()
{

char c;
while((c=getchar())!='\n')
{
c=toupper(c);
putchar(c);

getch(); }
}
----------------------------------------------------------------------
/* 4:Counting each vowels in string

#include
#include
#include

void main()
{
int a=0,e=0,i=0,o=0,u=0, c, length;
char str[100];
clrscr();

printf("\n\nProgram to get a string and find number of each of vowels appered in the string\n\n");
printf("Enter a String ");
gets(str);
length=strlen(str);

for(c=0;c switch (toupper (str[c]) )
{
case 'A':
a++;
break;
case 'E':
e++;
break;
case 'I':
i++;
break;
case 'O':
o++;
break;
case 'U':
u++;
break;
}

printf("\n\n There are %d a's \n" , a);
printf("\n\n There are %d e's \n" , e);
printf("\n\n There are %d i's \n" , i);
printf("\n\n There are %d o's \n" , o);
printf("\n\n There are %d u's \n" , u);
getch();
}
----------------------------------------------------------------------------------------------
/*
Program 5:Program to accept N words and make it as a sentence by inserting
blank spaces and a full stop at the end.
*/

#include
#include
#include

void main()
{

char str[10][20];
char sentence[200]="";
int n,i;
clrscr();
printf("\n\nProgram to accept N words and make it as a sentence by inserting blank spaces and a full stop at the end\n\n\n\n");
printf("\n Enter Number of words: ");
scanf("%d",&n);
for (i=0;i<=n; i++)
{
printf("Enter a String: ");
gets(str[i]);
if (i != n )
strcat(str[i]," ");
else
strcat(str[i], ".");

strcat(sentence,str[i]);
}
printf("\n\n%s", sentence);
getch();
}

------------------------------------------------------------------------------------------------
/*
Program 6:Program to print the reverse of a String
*/

#include
#include
#include

void main()
{

char str1[10], str2[10];
clrscr();
printf("\n\nProgram to print the reverse of a string \n\n");
printf("\nEnter a string: ");
scanf("%s",str1);
strcpy(str2,str1);
strrev(str2);
printf("\n\nThe reversed string is %s\n\n\n", str2);
if (strcmp(str1,str2) == 0)
printf("%s is a palindrome",str1);
else
printf("%s is not a palindrome",str1);
getch();
}
--------------------------------------------------------------------------------------
//Program 7:FINDING THE FIRST N TERMS OF FIBONACCI SERIES*/

#include
#include
void main()
{
int i,fib1=0,fib2=1,fib,n;
clrscr();

printf("\n How many Terms ? ");
scanf("%d",&n);

printf("\n %d \n %d",fib1,fib2);
for ( i=3;i <=n ; i++)
{
fib = fib1 + fib2;
printf("\n %d",fib);
fib1=fib2;
fib2=fib;
}

getch();
-------------------------------------------------------------------------------
//Program 8: Finding the maximum of 4 numbers by defining a macro for the maximum of two numbers.

#include
#include
#define MAX(n1,n2)((n1>n2)?n1:n2)
void main()
{
int a,b,c,d,t,lar;
clrscr();

printf("Input 4 nos : ");
scanf("%d%d%d%d",&a,&b,&c,&d);
t=MAX(a,b);
lar=MAX(MAX(t,c),d);

printf("\nLargest of %d, %d, %d, %d is %d",a,b,c,d,lar);
getch();

}


--------------------------------------------------------------------------------
//Program 9: Recursive program to find the factorial of an integer.

#include
#include
int fact(int);
void main()
{
int n;
clrscr();
printf("Enter any non-negative integer \n");
scanf("%d",&n);

if(n<0)
printf("invalid Input");
else
printf("Factorial of %d is = %d \n",n,fact(n));/*Function call*/
getch();
}

/*Recursive function to find the factorial of a number */
int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
----------------------------------------------------------------------------
//Program 10:Demonstration of bitwise operations.
//Demonstration of bitwise operation.
#include
#include
void main()
{
int x,y,w=-1;
clrscr();

printf("Enter two integers: ");
scanf("%d%d",&x,&y);

printf("\n w= %d One's Compliment = ~w = %d \n",w,~w);
printf("\n x & y = %d \n",x & y);
printf("\n x | y = %d \n",x | y);
printf("\n x ^ y = %d \n",x ^ y);
printf("\n x << y = %d \n",x<<2);
printf("\n x >> y = %d \n",x>>2);

getch();
}












No comments:

Post a Comment