Skip to main content

Posts

Showing posts from 2013

wap in c to print the prime numbers between two numbers using sieve method.

To know about sieve method go to -- http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes #include<stdio.h> #include<math.h> int main() { int count; long int n,m,i,j,t=1; while(t--) { printf("enter the numbers between you want to print the prime numbers"); scanf("%d %d",&m,&n); if(m==1||m==2)     printf("%d\n",2); if(m%2==0) m=m+1; for(i=m;i<=n;i=i+2) { count=0; for(j=3;j<=sqrt(i);j=j+2) if(i%j==0) {count++;break;} if(count==0&&i!=1) printf("%d\n",i); } printf("\n"); } return(0); }

wap in c to implement newton's forward interpolation formula.

Question -- using newton forward interpolation formula , find the area of a circle of diameter 82 metre                                from the given table of diameter and area of circle. Diameter(metre)      80          85           90          95            100 Area(metre^2)      5026        5674      6362     7088         7854 code to solve the above question. #include<stdio.h> #include<math.h> main() { int i,j,a=0,n; float x[10],fx[10],delfx[10],result=0,point,f,h,p,fact=1.000; printf("\nthe point at which u want to calculate the value\n"); scanf("%f",&point); printf("\nenter the no of elements in x\n"); scanf("%d",&n); printf("enter the elements of x\n"); for(i=0;i<n;i++) scanf("%f",&x[i]); h=fabs(x[0]-x[1]); p=(point-x[0])/h; printf("\nthe value of p is %f\n",p); printf("enter the elements of f(x)\n"); for(i=0;i<n;i++) scanf("%f",

wap to implement newton raphson method to find the root of x*sin(x)+cos(x).

#define f(x) x*sin(x)+cos(x) #define df(x) sin(x)+x*cos(x)-sin(x) #include<stdio.h> # include<math.h>  main() { float i,a,b,c; for(i=0;;i++) { if(f(i)>0&&f(i+1)<0) { a=i;b=i+1;break;} if((f(i)<0)&&(f(i+1)>0)) { a=i+1;b=i;break;} } for(i=0;;i++) { if(f(b)>0&&df(b)<0) c=b+(f(b)/fabs(df(b))); if(f(b)<0&&df(b)>0) c=b+(fabs(f(b)/df(b))); if((f(b)>0&&df(b)>0)||(f(b)<0&&df(b)<0)) c=b-(fabs(f(b))/fabs(df(b))); if(fabs(c-b)<.00001) break; b=c; } printf("the root is %f",c); }

wap to store data of sparse matrix efficiently.

#include<stdio.h> main() { int arr[100][100],arr1[100][3],i,j,m,n,a=1,b=0; printf("enter the no of columns and rows\n"); scanf("%d%d",&m,&n); arr1[0][0]=m;arr1[0][1]=n; printf("enter the elements of the matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&arr[i][j]); if(arr[i][j]!=0) { arr1[a][b]=i; arr1[a][b+1]=j; arr1[a][b+2]=arr[i][j]; a++; } } arr1[0][2]=a-1; printf("the sparse matrix is as follows\n"); for(i=0;i<a;i++){ for(j=0;j<3;j++) printf("%d\t",arr1[i][j]); printf("\n");} }

wap to find the second largest element from an array.

#include<stdio.h> main() { int i,n,arr[100],a=0,b=0; printf("enter the no of elements in the array\n"); scanf("%d",&n); printf("enter the elements of the array\n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); for(i=1;i<n;i++) if(arr[a]<arr[i]) { b=arr[a]; a=i; } else if(b<arr[i]) b=arr[i]; printf("\n the second largest number is %d ",b); }

wap to implement bisection method to find the root of (X)*log10(X)-1.2.

#define f(x) (x)*log10(x)-1.2 #include<stdio.h> # include<math.h>  main() { float i,a,b,c,t=0; for(i=0;;i++) { if(f(i)>0&&f(i+1)<0) { a=i;b=i+1;break;} if((f(i)<0)&&(f(i+1)>0)) { a=i+1;b=i;break;} } for(i=0;;i++) { c=(a+b)/2; if(f(c)>0) a=c; else b=c; if(fabs(c-t)<=.00005) break; t=c; } printf("\nthe root is %f",c); }

define a macro that does not make use of a bitwise XOR operator to swap the contents of two variables.

#include<stdio.h> #define swap(a,b) {a=a+b;b=a-b;a=a-b;printf("x=%d y=%d",a,b);} void main() {     int x,y;     printf("enter the two numbers\n");     scanf("%d%d",&x,&y);     printf("the numbers before swapping are\n x=%d y=%d\n",x,y);     printf("the numbers after swapping are\n");     swap(x,y); } NOTE-  i have compiled this program in  codeblocks  if u do it  in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last. output image

wap to find whether a number entered by user is automorphic or not.

if u want to know what is automorphic number then go to- http://en.wikipedia.org/wiki/Automorphic_number #include<stdio.h> void main() {     long int count=1,n,a,m,div=1,i;     printf("enter the number \n");     scanf("%d",&n);     a=n;     while((n/10)!=0)     {     n=n/10;     count++;     }     m=a*a;     for(i=1;i<=count;i++)         div=div*10;     if(m%div==a)     printf("\nit is an automorphic number");         else     printf("\nit is not an automorphic number"); } NOTE-  i have compiled this program in  codeblocks  if u do it  in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last. output window (click to enlarge).

define a macro to find the factors of a number.

#include<stdio.h> #define factor(num) {do{if(num%a==0){num=(num/a);printf("\n%d",a);}else a++;}while(num!=1);} void main() {     int a=2,num;     printf("enter the number\n");     scanf("%d",&num);     printf("%d",1);     factor(num); } NOTE-  i have compiled this program in  codeblocks  if u do it  in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last. output image  (click to enlarge).

wap to find the lcm of three numbers.

#include<stdio.h> void main() {     int num,num2,num3,a=2,lcm=1;     printf("enter the three numbers\n");     scanf("%d%d%d",&num,&num2,&num3);     while((num!=1)||(num2!=1)||(num3!=1))         if((num%a==0)||(num2%a==0)||(num3%a==0))         {         if(num%a==0)num=num/a;     if(num2%a==0)num2=num2/a;     if(num3%a==0)num3=num3/a;     lcm=lcm*a;         }         else             a++;         printf("\nthe lcm of the three numbers is  %d",lcm); } NOTE-  i have compiled this program in  codeblocks  if u do it  in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last. here is the proof of the code running..........(click on the image to enlarge)

wap to find the factors of a number.

#include<stdio.h> void main() {     int a=2,num;     printf("enter the number");     scanf("%d",&num);     printf("the factors of the number are\n%d",1);     do     {         if(num%a==0)         {             num=(num/a);         printf("\n%d",a);         }         else             a++;     }while(num!=1); } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last.

Wap to find the nth term of the following series__ 1,2,2,4,4,4,4,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16......................................

#include<stdio.h> #include<math.h> void main() {     int a=1,b=1,c,n;     printf("enter the no for which u want the term");     scanf("%d",&n);     while(a<n)         a=a+pow(2,b++);     c=pow(2,--b);     if((c<=n)&&(n<=a))         printf("\n%d",c); } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last.

Wap to count the number of occurrences of a particular word in the string.

#include<stdio.h> #include<conio.h> #include<string.h> void main() {     int a,b=0,count=1,i,j,c;     char name1[1000],name2[1000];     printf("enter the string \n");     gets(name1);     printf("enter the word that u want to search in the string\n");     gets(name2);     a=strlen(name2);     for(i=0;name1[i]!='\0';i++)     if(name1[i]==name2[0])     {         c=i,count=1;         for(j=1;j<a;j++)         if(name1[++c]==name2[j])         count++;         if(count==a)         b++;     }     printf("the word occures %d times in the string",b);     getch(); }

How to install Code Blocks?

you may be thinking why  have i written a post for installing code blocks as it is very easy to install programs on windows  now a days may be a child studying in grade 3 would be able to do it, but there are somethings we always overlook which leads to a severe problem that we are not able to compile c code snippets on code blocks. So here is the right process. 1) first go to the download website of code blocks i.e http://www.codeblocks.org/downloads/26#windows 2)you will get this type of window now here  download the 98 mb  file named   codeblocks-12.11mingw-setup.exe. 3) now after downloading the file install the file and then open code blocks  then goto settings then select compiler  there select   toolchain executables then in the the compilers directory click the auto detect button and then click ok , and that's it ,now code blocks is ready to compile and run c code snippets.

wap to print the following pattern.

#include<stdio.h> void main() { int i,j,k,a=1,b=1,n; printf("enter the number of rows"); scanf("%d",&n); for(i=n;i>=1;i--)  {    {     for(j=1;j<=i-1;j++)     printf("  ");     for(k=1;k<=b;k++)     if((k&1)==0)     printf("  ");     else     printf("%c ",'a');     }   b=b+2;   printf("\n");   } } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

wap to print the following pattern....

#include<stdio.h> void main() { int i,c=1,a=1,n; printf("enter the no of rows"); scanf("%d",&n); for(i=1;i<=n;i++) { if((i&1)==0)    {     for(c=1;c<=i;c++)     if((c&1)==0)     printf("%d ",1);     else     printf("%d ",0);     } else     {     for(a=1;a<=i;a++)     if((a&1)==0)     printf("%d ",0);     else     printf("%d ",1);     } printf("\n"); } }

wap to print the following pattern.

                                      #include<stdio.h> void main() { int i,j,n,a=1,k,m,b=1; printf("enter the no. of rows"); scanf("%d",&n); for(i=n;i>=1;i--) {  {  for(j=1;j<=i-1;j++)  printf("  ");  for(k=b;k<=a;k++)  printf("%d ",k);  for(m=a-1;m>=b;m--)  printf("%d ",m);  }  printf("\n");  a=a+2;  b=b+1;  } } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

wap to print the sum of the following series.

(2^2)+(2^2+2^4)+(2^2+2^4+2^6)+...........n terms. #include<stdio.h> void main() { int i,j,sum,seriesum=0,p,n; printf("enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) {  p=2,sum=0; for(j=1;j<=i;j++) {  sum=sum+pow(2,p);  p=p+2; } seriesum=seriesum+sum; } printf("%d",seriesum); } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

wap to delete all vowels from a sentence and print the sentence without vowels.

#include<stdio.h> void main() { char sen[1000],sen2[100]; int i,a,count=0,j=0; gets(sen); a=strlen(sen); for(i=0;sen[i]!='\0';i++) {  if(sen[i]=='a'||sen[i]=='e'||sen[i]=='i'||sen[i]=='o'||sen[i]=='u')         count++;         else     {     sen2[j]=sen[i];     j++;     }     } printf("%d\n%d",i,a); printf("\n%s",sen2); } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

wap that reads string from the keyboard and determine whether string is a palindrome or not.

#include<stdio.h> #include<string.h> #include<conio.h> void main() { char string[50],reverse[50]; int i; printf("enter the string\t"); gets(string); strcpy(reverse,string); strrev(reverse); i=stricmp(reverse,string); if(i==0) printf("the string entered is a palindrome"); else printf("the enterted string is not a palindrome."); getch(); }

wap to implement binary search.

#include<stdio.h> int binarysearch(int list[],int low,int high,int key); void main() { int list[20],num,i,key,low,high,index; printf("enter the number of elements(max 20)\t"); scanf("%d",&num); printf("enter the elements in ascending order"); for(i=0;i<num;i++) scanf("%d",&list[i]); printf("enter the key that u want to search"); scanf("%d",&key); index=binarysearch(list,0,num-1,key); if (index==-1) printf("does not exist in the list"); else printf("exists at the location %d",index+1); } int binarysearch(int list[],int low,int high,int key) { int mid; if(low==high) { if(list[low]==key) return low; else return -1; } else { mid=(low+high)/2; if(list[mid]==key) return mid; else if(list[mid]>key) return binarysearch(list,low,mid-1,key); else return binarysearch(list,mid+1,high,key); } } NOTE-  i have compiled this program in  codeblocks  if u do i

wap to initialize an array dynamically and print the elements of the array in reverse order. the size of the array and elements of the array are to be taken by the user.

#include<stdio.h> #include<malloc.h> void main() { int *array,size,i; printf("enter the size of the array"); scanf("%d",&size); array=(int*)malloc(size*sizeof(int)); printf("enter the elements"); for(i=0;i<size;i++) scanf("%d",&array[i]); for(i=size-1 ;i>=0;i--) printf("%d",array[i]); } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

wap to print the following pattern.

A A B A B C A B C D A B C D E #include<stdio.h> void main() { int i,j; for(i=65;i<=70;i++) {   {    for(j=65;j<=i;j++)    printf("%c ",j);    }  printf("\n"); } } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last

WAP to find the GCD(greatest common divisor) of three numbers entered by the user.

#include<stdio.h> void main() { int a,b,c,d,e,f,i; scanf("%d%d%d",&a,&b,&c); for(i=a;i>=1;i--) {   d=a%i;   e=b%i;   f=c%i;  if((d==0)&&(e==0)&&(f==0))  {  printf("the GCD(greatest common divisor) of the numbers is=%d",i);   break;  }  } } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last.

WAP TO PRINT THE FOLLOWING SERIES UPTO n NO OF COLUMNS.

* # # * * * # # # # * *  * * * # # # # # # #include<stdio.h> void main() { int i,j,n; printf("enter the value of n"); scanf("%d",&n); for(i=1;i<=n;i++)  {    {     if((i&1)==0)       {        for(j=1;j<=i;j++)        printf("# ");        }      else       {        for(j=1;j<=i;j++)        printf("* ");        }      }   printf("\n");   } } NOTE-  i have compiled this program in  codeblocks  if u do it in turbo c++  use   conio.h   header file in the beginning  and  getch()   a the  last.

WAP to enter two square matrix of order n*n and display sum of the two matrices , sum of principal diagonal elements of the matrix formed after add, the upper triangular half of the matrix formed after addition.

#include<stdio.h> void main() { int array1[10][10],array2[10][10],array3[10][10],array4[10][10],i,j,m,n,b=0; printf("enter the size of the array"); scanf("%d%d",&m,&n); printf("enter the elements of array1"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&array1[i][j]); printf("enter the elements of array2"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&array2[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) array3[i][j]=array1[i][j]+array2[i][j]; printf("the elements of matrices after adding the two matrix are\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d\t",array3[i][j]); printf("\n"); } for(i=0;i<m;i++) b=b+array3[i][i]; printf("the sum of principal diagonal of the square mattrix is=%d",b); for(i=0;i<m;i++) for(j=0;j<m;j++) if(i<=j) array4[i][j]=array3[i][j]; else array4[i][j]=0; printf("\

write a program which tells the day on 1st january of a year entered by the user. the year entered should be after 2001.

#include<stdio.h> void main() { int year,i,count=0,sub; printf("enter the year after 2001 for which u want the day"); scanf("%d",&year); for(i=2001;i<year;i++) { if(((i%4==0)&&(i%100!=0))||(i%400==0)) {     printf("leap year%d",i); count=count+1; } } printf("\nthe value of count is=%d\n",count); sub=year-2001; printf("%d",sub); sub=sub+count; printf("\nthe value of sub is=%d\n",sub); for(sub;sub>7;sub=sub-7); printf("\nthe value of sub is=%d\n",sub); switch(sub) { case 1: printf("tuesday"); break; case 2: printf("wednesday"); break; case 3: printf("thursday"); break; case 4: printf("friday"); break; case 5: printf("saturday"); break; case 6: printf("sunday"); break; case 7: printf("monday"); break; } } NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h   header file in the beginning  a

wap to print the following pattern.

wap to print the following pattern in c----     #include<stdio.h> void main() { int i,j,k,f=1; for(i=65;i<=71;i++) printf("%c",i); for(i=70;i>=65;i--) printf("%c",i); printf("\n"); for(i=1;i<=6;i++) {   {    for(j=65;j<=71-i;j++)    printf("%c",j);    }      {       for(k=1;k<=f;k++)        printf(" ");        f=f+2;       for(k=71-i;k>=65;k--)       printf("%c",k);      }  printf("\n"); } } NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h   header file in the beginning  and getch()   a the  last.

wap to input a five digit number and add 1 to each digit such that the output is as follows ..e.g 12569...so the output should be..23670..... this is to be done without using any loop....

#include<stdio.h> void main() { long int num,a,b,c,d,e,f,g,h,eureka; printf("enter the five digit number"); scanf("%d",&num); a=num/10000; b=num%10000; c=b/1000; d=b%1000; e=d/100; f=d%100; g=f/10; h=f%10; a=((a+1)%10)*10000; c=((c+1)%10)*1000; e=((e+1)%10)*100; g=((g+1)%10)*10; h=((h+1)%10)*1; eureka=a+c+e+g+h; printf("the increased no. is %d",eureka); }

write a function factorial to find the factorial of a number and use this function to solve the following series:::: y=(1!)+(1!+2!)+(1!+2!+3!)+............n terms

the following program  came in my sessional. and i wrote it   and is in front of u ,  check it out.::::::::::: #include<stdio.h> #include<conio.h> int fact(int i); int main() { int sum=0 ,i,n,e; printf("enter the no. til u want the series"); scanf("%d",&n); for(i=1;i<=n;i++) { e=fact(i); sum=e+sum; } printf("the sum of the series is  %d",sum); } int fact(int i) { int t,j,a=1,d=0; for(t=1;t<=i;t++) { for(j=1;j<=t;j++) { a=a*j; } d=d+a; a=1; printf("%d \n",d); } return(d); }

WHAT IS C?

C  IS A programming language devleoped at at and t bell labs.it was initially developed by DENNIS RITCHIE(rip).there are a lot of software written in c, to view those just try the link ( http://en.wikipedia.org/wiki/Category:Free_software_programmed_in_C). A very famous software we all use in our daily life is vlc media player   programmed in c.the reason for becoming c to be so popular because it is very easy to use.major parts of popular operating systems such as windows, unix ,  linux are written in c. although there are many languages today such as java , c++ , c# ( pronounced c sharp),  but today also c is very popular in programmers because of its speed of execution.  and also we can use c for developing apps for some os like for windows phone 8 we have visual studio  framework.