Mr.Pinaki welcomes u.

‘C’ solution Set for Sem-end

Posted by: pinakinayak on: 15/12/2008

Answers:

  1. Write a program to find the roots of a quadratic equation.

Ans: #include<stdio.h>

main()

{

float a,b,c,r,l,d;

printf(“enter three nos of equation:”);

scanf(“%f %f %f”,&a,&b,&c);

if(a= =0)

printf(“value of a should not be zero.”);

else

{

d=b*b-4*a*c;

if(d>0)

{

r=(-b+sqrt(d))/(2*a);

l=(-b-sqrt(d))/(2*a);

printf(“roots are real and unequal\n”);

printf(“root1 =%f”,r);

printf(“\nroot2=%f”,l);

}

else if(d= =0)

{

r =-b/(2*a);

l=-b/(2*a);

printf(“roots are real and equal\n”);

printf(“root1 =%f”,r);

printf(“\nroot2=%f”,l);

}

else

printf(“roots are complex and imaginary:”);

}

getch();

}

  1. Towers of Hanoi (Using recursion)

/* Hint: Towers of Hanoi is a well known children’s game, played with three poles and a different-sized disks. Each disk has hole at the centre, allowing it to be stacked around any of the poles. Initially, the disks are stacked on the left most pole in the order of decreasing size, i.e., the largest on the bottom and the smallest on the top.

The object of the game is to transfer the disks from the left most pole to the right most pole, without ever placing a larger disk on the top of a smaller disk. Only one disk may be moved at a time, and each disk must always be placed around one of the poles. */

#include<stdio.h>

void transfer(int n, char from, char to, char temp);

main()

{ int n;

Printf(“Welcome to Tower of Hanoi\n”);

Printf(“How many disks”);

Scanf(“%d”,&n);

transfer(n, ’L’, ’R’, ’C’);

}

void transfer(int n, char from, char to, char temp)

{

If(n>0)

{

transfer(n-1,from,temp,to);

printf(“Move disk %d from %c to %c\n”, n, from, to);

transfer(n-1, temp, to, from);

}

return;

}

  1. Find sum of ‘n’ Fibonacci numbers, using recursion.

Ans:

#include<stdio.h>

int f1=0,f2=1,f3;

main()

{

void fibo(int);

int n;

printf(“Enter the no of fibonacii series”);

scanf(“%d”,&n);

if(n==1)

printf(“\n%d”,f1);

else if(n>2)

{printf(“|n%d\n%d”,f1,f2);

fibo(n);

}

getch();

}

return(0);

}

void fibo (int n)

{

if(n==0)

return(0);

else

{

f3=f1+f2;

f1=f2;

f2=f3;

printf(“\n%d”,f3);

fibo(n-1);

}

  1. Find sum of digits of a no, using recursion.

Ans) main()

{ int n,s=0,sum(int,int);

printf(“Enter the number”);

scanf(“%d”,&n);

printf(“The sum of the digits of the number is %d”,sum(n.s));

getch();
}

int sum(int n, int s)

{ int r;

r=n%10;

s+=r;

If(n==0)

return(s);

else

return(sum(n/10,s));

}

  1. Accept a binary number from user and convert it into decimal number.

Ans)

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

{

int b,r,dec=0,i=0;

clrscr();

printf(“Enter a binary number:”);

scanf(“%d”,&b);

while(b!=0)

{

r=b%10;

b=b/10;

dec=dec+(r*pow(2,i));

i++;

}

printf(“%d”,dec);

}

  1. Accept a decimal number from user and convert it into binary number.

Ans)

main()

{

int no,n,rem,arr[10],r=0,i;

clrscr();

printf(“Enter a decimal no:”);

scanf(“%d”,&no);

n=no;

while(no>0)

{

rem=no%2;

arr[r]=rem;

no/=2;

r++;

}

printf(“\nThe binary equivalent of %d is:\n”,n);

for(i=r-1;i>=0;i–)

printf(“%d”,arr[i]);

}

7. Write a program that computes LCM & GCD of two given integers.

main()

{

int i,a,b,lcm=1,small,gcd;

clrscr();

printf(“Enter 2 numbers : “);

scanf(“%d %d”,&a,&b);

if(a<b)

small=a;

else

small=b;

for(i=2;i<=small;i++)

{

if(a%i==0 && b%i==0)

{

a=a/i;

b=b/i;

lcm=lcm*i;

i=1;

}

}

gcd=lcm;

lcm=lcm*a*b;

printf(“\n GCD = %d”,gcd);

printf(“\n LCM = %d”,lcm);

getch();

}


8. Write a program to generate a series of Armstrong numbers.

Ans: #include<stdio.h>

void main()

{

int i,n,r,sum=0;

printf(“The Armstrong series is:”);

for(i=1;i<=1000;i++)

{

sum=0;

n=i;

while(n!=0)

{

r=n%10;

sum=sum + r*r*r;

n=n/10;

}

if(sum= =i)

printf(“%d, ”,i);

}

getch();

}

9. Write a program to find reverse of a no and check whether it is palindrome or not.

Ans: #include<stdio.h>

void main()

{

int i,n,r,rev=0,num;

printf(”enter the no”);

scanf(”%d”,&num);

n=num;

printf(”enter the no:”);

scanf(”%d”,&num);

n=num;

while(n!=0)

{

r=n%10;

rev=rev*10+r;

n=n/10;

}

printf(”reverse is %d\n”,rev);

if(rev= =num)

printf(“the no is palindrome”);

else

printf(“the no is not palindrome”);

getch();

}

10. Write a program to display the pattern:

1234554321

1234 4321

123 321

12 21

1 1

Ans: #include<stdio.h>

void main()

{

int i,j,k,m=0;

for(i=5;i>=1;i–)

{

for(j=1;j<=1;j++)

{

printf(“%d”,j);

}

for(k=1;k<=m;k++)

{

Printf(“ ”);

}

for(j=i;j>=1;j–)

{

printf(“%d”,j);

}

printf(“\n”);

m=m+2;

}

getch();

}

11. Write a program to find the sum:

Sum=1+22+33+44+……..

Ans: #include<stdio.h>

#include<math.h>

void main()
{

int i,n,sum=0;

printf(“enter the value of n”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

sum=sum+pow(i,i);

}

printf (“sum is %d”,sum);

getch();

}

  1. Write a program to find the sum of the digits of a no reducing it to one digit.

Ans: #include<stdio.h>

void main()

{

int n,r,sum,i;

printf(“Enter a no:”);

scanf(“%d”,&n);

abc:

sum=0;

do

{

r=n%10;

sum=sum+r;

n=n/10;

}

while(n!=0);

if(sum>9)

{

n=sum;

goto abc;

}

else

printf(“the result is %d”,sum);

getch();

}

Output: enter a no: 576

The result is 9

  1. Write a program to enter a string and sort it alphabetically.

Ans: #include<stdio.h>

#include<string.h>

void main()

{

char a[10],k

int i,j;

printf(“\n enter the string:”);

scanf(“%s”,a);

printf(“\n the string in alphabetical order is:”);

for(i=0;a[i]!=”;i++)

{

for(j=i+1;a[j]!=”;j++)

{

if(a[i]>a[j])

{

k= a[i];

a[i]=a[j];

a[j]=k;

}

}

}

printf(“%s”,a);

getch();

}

Output: enter the string: India

The string in alphabetical order is: adiin

14. Write a program to copy the contents of one string to another string without using any standard string functions.

Ans: #include<stdio.h>

void main()

{

char s[20],s1[20];

int i;

printf(“enter the string:”);

gets(s);

for(i=0;s[i]!=’’;i++)

s1[i]=s[i];

s1[i]=’’;

printf(“source string is %s”,s);

printf(“destination string is %s”,s1);

}

  1. Write a program to compare two strings.

Ans: include<stdio.h>

void main()

{

char str[20],str1[20];

int i=0;

printf(“enter the 1st string”);

gets(str);

printf(“enter the 2nd string”);

gets(str1);

while (str[i]=’’ && str1[i]=’’)

{

if(str[i]==str1[i])

i++;

}

if(str[i]==str1[i])

printf(“\n\t strings are equal”);

else

printf(“\n\t strings are equal”);

getch();

}

  1. string concatenation.

Ans: First input 2-string.

main()

{ char name[40], name1[20];

int i,j;

i=j=0;

.

.

.

while(name[i]!=’’)

i++;

while(name1[j]!=’’)

name[i++]=name1[j++];

name[i]=’’;

printf(\nThe concatenated string is %s” ,name);

}

  1. Write a program to extract a substring from the given string.

Ans: #include<stdio.h>

void main()

{

char str[30];

int i,n,pos,len;

printf(”enter a string”);

gets(str);

len=strlen(str);

printf(”enter the position of the required substring:”);

scanf(“%d”,&pos);

printf(“enter the no. of character to be extracted:”);

scanf(“%d”,&n);

if(pos+n-1>len)

printf(“substring is “);

else

{

printf(“substring is”);

for(i=pos-1;i<pos+n-1;i++)

printf(“%c”,str[i]);

}

getch();

}

  1. Wap to function to merge 2-arrays.

Ans:

void mergearray((int a[],int b[], int c[], int m, int n)

{ int i,j=0;

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

{

c[j]=a[i];

j++;

}

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

{

c[j]=b[i];

j++;

}

}

  1. Find out the sum of diagonal elements of a matrix.

Ans: Presume the matrix is declared and all elements were input, the logic for sum of diagonal elements-

if(r!=c) //r- row size, c- column size.

{

Printf(“\nSum of the diagonal elements is not possible”);

Exit(0);

}

For(i=0;i<r;i++)

For(j=0;j<c;j++)

Sum=sum+a[i][i]; //sum gives the ans.

  1. Print series:0, 1,2,4,6,10,12,16,18,22…20 elements.( Prime no.s-1)

void main ()

{

int f,i,n,c=1;

for(n=2;n<100;n++)

{ f=1;

for(i=2;i<n;i++)

{

if(n%i==0)

f=0;

}

if(f==1)

{ printf(“%d “,n-1);

c++;

if(c>20)

{

exit(0);

}

}

}

}

  1. Write a program to compute, sum=1! + 2! + 3! + 4!……………..n!.

main()

{

int fact=1,num,i,j,sum=0;

printf(“Enter a number: “);

scanf(“%d”,&num);

for(i=1;i<=num;i++)

{

for(j=1;j<=i;j++)

fact=fact*j;

sum=sum+fact;

fact=1;

}

printf(“%d is the result”,sum);

}

  1. Write a program that accept a number from user, and check whether the digits of the entered number

are in increasing order or not.

void main()

{

int num,num1,r,r1,f=1;

printf(“Enter a number: “);

scanf(“%d”,&num);

num1=num;

r1=num%10;

while(num>0)

{

r=num%10;

num=num/10;

if(r>r1)

{

printf(“\n Number not increasing order”);

f=0;

break;

}

r1=r;

}

if(f==1)

printf(“\n Number increasing order=%d”,num1);

}

23. Write a program that accept two numbers from user, and find out the large number between them without using any if-statement or conditional operator.

void main()

{

int x,y;

printf(“\n accept two numbers :”);

scanf(“%d %d”,&x,&y);

while(x>y)

{

printf(“%d is greater then %d”,x,y);

break;

}

while(y>x)

{

printf(“%d is greater than %d”,y,x);

break;

}

}

24. Write a program to insert a new element at beginning of the array without overlapping the first.

main()

{

int x[30],i,n,y;

printf(“Enter number of elements u want in the array(less than 30) : “);

scanf(“%d”,&n);

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

{

printf(“\nEnter a number : “);

scanf(“%d”,&x[i]);

}

printf(“\nEnter the number to be inserted in the begining of the array :”);

scanf(“%d”,&y);

for(i=n-1;i>=0;i–)

x[i+1]=x[i];

x[0]=y;

for(i=0;i<n+1;i++)

printf(“ %d”,x[i]);

}

25. Write a program that accept a name/ text from user, and reverse each words of a given name or text.

Ans: main()

{

int i,j,k,l,a,b;

char x[30],y[30];

printf(“Enter a text:”);

scanf(“%[^\n]“,x);

i=0;

j=0;

l=0;

while(x[i]!=”)

{

j=i;

while(x[i]!=’ ‘ && x[i]!=”)

i++;

if(i>j)

{

k=i;

while(k>=j)

{

y[l]=x[k-1];

k–;

l++;

}

}

i++;

}

y[l+1]=”;

printf(“%s”,y);

getch();

}

26. Write a program that accept a string and check whether the entered string palindrome or not.

void main()

{

char str[20];

int i,j,k;

printf(“Enter a string:”);

scanf(“%s”,str);

k=strlen(str);

j=k-1;

i=0;

while(i<k/2)

{

If(str[i]==str[j])

{ i++;

j–;

}

}

if(i==k/2)

printf(“\n Word is palindrome”);

else

printf(“\n Word is not a palindrome”);

}

  1. Joining of two string to a third string is similar to concatenation of strings, but instead of two string 3-string are taken.

Also see the merging of two arrays logic.(Q-18).

  1. Constructing a Pascal triangle

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,x,n;

printf(“\n Enter the value of row: “);

scanf(“%d”,&n);

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

{

for(j=i;j<=n;j++)

printf(“ “);

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

{

if(j==0)

x=1;

else

x=x*(i-j+1)/j;

printf(“%4d”,x);

}

printf(“\n”);

}

29. Write a program to print a series of prime numbers without using any looping statements(Using ‘goto’)

void main()

{

int i=1,f,j,count=0;

printf(“\n Series of Prime Numers :–>”);

aa:

f=1;

j=2;

bb:

if(i%j==0)

f=0;

j++;

if(j<i)

goto bb;

if(f==1)

{ printf(“ %d”,i);

count++;

}

i++;

if(count<=20)

goto aa;

}

  1. For printing a merged sorted list of two given array.

void main()

{

int x[5],y[5],a[10];

int t,k,i,j,temp;

clrscr();

printf(“Enter the 1st Array element\n”);

for(i=0;i<5;i++) //For entering the number of the 1st array

{

printf(“Enter number %d : “,i);

scanf(“%d”,&x[i]);

}

printf(“\nEnter the 2nd Array element\n”);

for(i=0;i<5;i++) //For entering the numbers of the 2nd array

{

printf(“enter number %d : “,i);

scanf(“%d”,&y[i]);

}

//——————sorting 1st list————-

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

for(j=i+1;j<5;j++)

if (x[i]>x[j])

{ temp=x[i];

x[i]=x[j];

x[j]=temp;

}

printf(“\nSorted list of 1st array is “);

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

printf(“%d “,x[i]);

//——————-sorting 2nd list———–

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

for(j=i+1;j<5;j++)

if (y[i]>y[j])

{ temp=y[i];

y[i]=y[j];

y[j]=temp;

}

printf(“\nSorted list of 2nd array is “);

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

printf(“%d “,y[i]);

//—————————————————–//

i=0;

j=0;

k=0;

while(i<5&&j<5)

{

if(x[i]>y[j])

{

a[k]=y[j];

j++;

}

else

{ a[k]=x[i];

i++;

}

k++;

}

if(i<=4)

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

{ a[k]=x[t];

k++;

}

if(j<=4)

for(t=j;t<5;t++)

{ a[k]=y[t];

k++;

}

printf(“\n After merging into the 3rd array\n”);

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

printf(“%d “,a[i]);

}

31. Write a program to insert a new element at beginning of the array without overlapping the first.

main()

{

int x[30],i,n,y;

printf(“Enter number of elements u want in the array(less than 30) : “);

scanf(“%d”,&n);

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

{

printf(“\nEnter a number : “);

scanf(“%d”,&x[i]);

}

printf(“\nEnter the number to be inserted in the beginning of the array :”);

scanf(“%d”,&y);

for(i=n-1;i>=0;i–)

x[i+1]=x[i];

x[0]=y;

for(i=0;i<n+1;i++)

printf(“ %d”,x[i]);

}

Leave a Reply


  • pinakinayak: ok, u can reproduce them in your weekly
  • sudarshana: i want to reproduce your cartoons in our weekly vikrama,is it ok?

Categories