Tuesday, September 16, 2008

MCA SOLVED ASSIGNMENTS

MCS 011
Problem Solving and Programming


Q1: The factorial of non-negative integer n is written n! and is defined as follows:
n! = n * (n – 1) * (n – 2) . …. .1 (for values of n greater than or equal to 1).
and
n! = 1 (for n =0).
Perform the following:
1. Write a C program that reads a non-negative integer and computes and prints its factorial.


#include

main()
{
int n,i,fact=1;
clrscr();
start:
printf("Enter n value: ");
scanf("%d",&n);
if(n<0)
{
printf("Please enter non-negetive value\n");
goto start;
}
for(i = 1;i<=n;i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d\n",n,fact);
getch();
}





2. Write a C program that estimates the value of the mathematical constant e by using the formula: e = 1 + 1/1! + 1/2! + 1/3! + …..


#include
main()
{
int n,i,f=1,j;
float e = 1.0;
clrscr();
start:
printf("Enter n value: ");
scanf("%d",&n);
if(n<0)
{
printf("Please enter non-negetive value\n");
goto start;
}
for(i = 1;i<=n;i++)
{
for(j = 1;j<=i;j++)
{
f = f*j;
}
e=e+((float)1/f);
f=1;
}
printf("Result is: %f\n",(float)e);
getch();
}






3. Write a C program that computes the value ex by using the formula
ex= 1 + x/1! + x2/2! + x3/3! + …..


#include
#include

main()
{
int n,i,f=1,j,x;
float e = 1.0;
clrscr();
start:
printf("Enter n value: ");
scanf("%d",&n);
printf("Enter x value: ");
scanf("%d",&x);
if(n<0)
{
printf("Please enter non-negetive n value\n");
goto start;
}
if(x<0)
{
printf("Please enter non-negetive x value\n");
goto start;
}
for(i = 1;i<=n;i++)
{
for(j = 1;j<=i;j++)
{
f = f*j;
}
e = e+((float)pow(x,i)/f);
f = 1;
}
printf("Result e^x = %f\n",(float)pow(e,x));
getche();
}




Q2: Write a program to print this triangle:
*
**
*
****
*
******
*
********
*
**********
Don't use ten printf statements; use two nested loops instead. You'll have to use braces around the body of the outer loop if it contains multiple statements.

#include
main()
{
int i,j;
clrscr();
printf("Triangle\n\n");
for(i=1;i<=10;i++)
{
/* if i is odd number print sigle star*/
if(i%2 != 0)
printf("*");
else
{
/* if i is even number*/
for(j=1;j<=i;j++)
{
printf("*");
}
}
printf("\n");
}
getche();
}



Q3: The standard library contains a function called atoi, which takes a string (presumably a string of digits) and converts it to an integer. For example, atoi(“345”) would return the integer 345. Write a program that reads lines (using getline), converts each line to an integer using atoi, and computes the average of all the numbers read. Also compute the standard deviation.

#include
#include

main()
{
char str[40];
int i,l,c=0;
clrscr();
printf("Enter line no:");
scanf("%d",&l);
for(i=0;i<=l;i++)
{
gets(str);
c=c+atoi(str); s
}
printf("Avg is: %f",(float)c/l);
getche();
}






Q4: Write the function int countchtr(char string[ ], int ch);
which returns the number of times the character ch appears in the string. For example, the call
countchtr(“She lives in NEWYORK”, ‘e’)
would return 3.

#include
#define Max 50
int countchar(char[Max], char);

main()
{
char str1[Max],ch1;
int c;
clrscr();
printf("Enter string: ");
gets(str1);
printf("Enter char which u want to count: ");
scanf("%c",&ch1);
c = countchar(str1,ch1);
printf("Count is: %d",c);
getch();
}
int countchar(char str[], char ch)
{
int i,count=0;
for(i=0;i{
if(str[i]==ch)
count = count+1;
}
return count;
}




Q5:

Write a program that takes three variables (a, b, c) in as separate parameters and rotates the values stored so that value a goes to b, b, to c and c to a.

#include
main()
{
char ch1;
char ch2;
char ch3;
char temp;
clrscr();
printf("Enter characters: \n");
scanf("%c %c %c",&ch1,&ch2,&ch3);
printf("Rotated list is: \n");
temp = ch1;
ch1 = ch2;
ch2 = ch3;
ch3 = temp;
printf("%c\t",ch1);
printf("%c\t",ch2);
printf("%c",ch3);
getche();
}


for more information contact at : masteragarwal@yahoo.com







Website counter






No comments: