Wednesday, 17 May 2017

C Language Program Collection


Write a program to accept two values a & b and interchange their values

#include<stdio.h> #include<conio.h> main() { int a,b,temp; clrscr(); printf("Enter Numbers: "); scanf("%d%d",&a,&b); printf("\nBefore Swapping..\na=%d,b=%d",a,b); temp=a; a=b; b=temp; printf("\nAfter Swapping..\na=%d,b=%d",a,b); getch(); }

Program to show swap of two no’s without using third variable.

#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(“enter value for a & b: ”); scanf(“%d%d”,&a,&b); a=a+b; b=a-b; a=a-b; printf(“after swapping the value of a & b: %d %d”,a,b); getch(); }

Program to find greatest in 3 numbers.

#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf(“enter value of a, b & c: ”); scanf(“%d%d%d”,&a,&b,&c); if((a>b)&&(a>c)) printf(“a is greatest”); if((b>c)&&(b>a)) printf(“b is greatest”); if((c>a)&&(c>b)) printf(“c is greatest”); getch(); }

write a program to calculate roots of a quadratic equations in C language x=b^2-4ac if x=0 -> only one root , if x>0 -> roots are distinct (–b+x)/2a & (–b-x)/2a if x roots are imaginary

#include<stdio.h> #include<conio.h> main() { float x,r1,r2,a,b,c; clrscr(); printf("Enter a,b,c…"); scanf("%f%f%f",&a,&b,&c); x=b*b-4*a*c; r1=(-b+x)/2*a; r2=(-b-x)/2*a; if(x>0) printf("\nRoots are unequal…\n"); else if(x<0) printf("\nRoots are imaginary…\n"); else printf("\nRoots are same….\n"); printf("R1 = %f",r1); printf("R2 = %f",r2); getch(); }

Write a program to accept roll number ,and marks for three subjects, print total marks and average, also print grade by considering following conditions Avg>=60 Grade A Avg=50 Grade B Avg=40 Grade C Grade F.

#include<stdio.h> #include<conio.h> main() { int RollNumber,m1,m2,m3,total; float avg; clrscr(); printf("Enter Roll Number : "); scanf("%d",&RollNumber); printf("Enter marks for three subjects : "); scanf("%d%d%d",&m1,&m2,&m3); total=m1+m2+m3; avg=total/3.0; printf("\nTotal is……. %d",total); printf("\nAverage is….. %5.2f %",avg); if(avg>=60) printf("\nGrade : A"); else if((avg>=50)&&(avg<60)) printf("\nGrade : B"); else if((avg>=40)&&(avg<50)) printf("\nGrade : C"); else printf("\nGrade : F"); getch(); }

Write a Program to accept a number and print the number in reverse order. E.g. if 1324 is the number then the output will be 4231

#include<stdio.h> #include<conio.h> main() { int rem,n; clrscr(); printf("Enter n : "); scanf("%d",&n); while(n>0) { rem=n%10; printf("%d",rem); n=n/10; } getch(); }

Write a Program to accept a number and print sum of it’s digits

#include<stdio.h> #include<conio.h> main() { int rem,sum=0,n clrscr(); printf("Enter n : "); scanf("%d",&n); while(n>0) { rem=n%10; sum=sum+rem; n=n/10; } printf("Sum of digits….%d",sum); getch(); }

Write a program to accept a number from user and check it it is Armstrong number or not i.e. 153 = 1^3 + 5^3 + 3^3 = 153

#include<stdio.h> #include<conio.h> main() { int i=2,temp,rem,sum=0 ,n; clrscr(); printf("Enter n : "); scanf("%d",&n); temp=n; while(n>0) { rem=n%10; sum=sum+rem*rem*rem; n=n/10; } if(temp==sum) printf("Armstrong Number"); else printf("Not an Armstrong Number"); getch(); }

Write a program to print table of a given number n

#include<stdio.h> #include<conio.h> main() { int i,n; clrscr(); printf("Enter number : "); scanf("%d",&n); for(i=1;i<=10;i++) printf("%d X %d = %d\n",n,i,n*i); getch(); }

Write a program to print sum of given first n numbers

#include<stdio.h> #include<conio.h> main() { int n,newn,i,sum=0; clrscr(); printf("enter number : "); scanf("%d",&n); printf("Numbers entered….\n"); for(i=1;i<=n;i++) { scanf("%d",&newn); sum=sum+newn; } printf("Sum of given n digits is… %d",sum); }

Write a program to accept a number from user and print it’s factorial Eg: factorial of 5 is:-5! = 5 * 4 * 3 * 2 * 1=120

#include<stdio.h> #include<conio.h> main() { int i,fact=1,n; clrscr(); printf("Enter number…"); scanf("%d",&n); for(i=1;i<=n;i++) fact=fact*i; printf("facttorialof the given number is…%d",fact); getch();   }

Write a program to accept a number from user and print if it is prime or not

#include<conio.h> #include<process.h> main() { int i,n; clrscr(); printf("Enter number…"); scanf("%d",&n); for(i=2;i<=n/2;i++) { if(n%i==0) { printf("Not Prime"); getch(); exit(0); } } printf("Prime "); getch(); }

Write a program to accept a number and print prime numbers between 2 and n

#include<stdio.h> #include<conio.h> #include<process.h> main() { int i,flag=1,n,newn; clrscr(); printf("Enter number…"); scanf("%d",&n); for(newn=2;newn<=n;newn++) { flag=1; for(i=2;i<=newn/2 ;i++) { if(newn%i==0) { flag=0; break; } } if(flag==1) printf("%d ",newn); } getch(); }

Write a program to accept a number and print fibonacci series upto that level

#include<stdio.h> #include<conio.h> main() { int pre=1,cur=1,temp,i,n; clrscr(); printf("Enter number…"); scanf("%d",&n); printf("%d\t%d",pre,cur); for(i=3;i<=n;i++) { temp=cur; cur=pre+cur; pre=temp; printf("\t%d",cur); } getch(); }

Write a program to accept a number n from user and Add n terms of the series 1/2! + 2/3! + 3/4! + 4/5! + 5/6! + ………

#include<stdio.h> #include<conio.h> main() { int i,j,n; float sum=0,fact=1; clrscr(); printf("Enter number…."); scanf("%d",&n); for(i=1;i<=n;i++) { fact=1; for(j=1;j<=i+1;j++) fact=fact*j; sum=sum+i/fact; } printf("Sum of the series….%f",sum); getch(); }

Write a program to accept two numbers from user and print it’s addition,subtraction,multiplication,division using different functions in C language

#include<stdio.h> #include<conio.h> int add(int x,int y) { return(x+y); } int sub(int x,int y) { return(x-y); } int mul(int x,int y) { return(x*y); } int div(int x,int y) { return(x/y); } main() { int a,b; clrscr(); printf("Enter numbers : \n"); scanf("%d%d",&a,&b); printf("\nAddition : %d\n",add(a,b)); printf("\nSubtraction : %d\n",sub(a,b)); printf("\nMultiplication : %d\n",mul(a,b)); printf("\nDivision : %d\n",div(a,b)); getch(); }

Write a program to accept a number from user and print it’s factorial, check if it prime or not , and print it’s fibbonacci series using different functions in C language

#include<stdio.h> #include<conio.h>   fact(int x) { int i,fact=1; for(i=1;i<=x;i++) fact=fact*i; printf("Factorial is : %d",fact); } IsPrime(int x) { int i; for(i=2;i<x-1;i++) { if(x%i==0) { printf("\nNot a Prime Number\n"); return 0; } } printf("\nIt is a Prime number\n"); } fibbo(int x) { int pre=1,cur=1,i,temp; printf("%d %d",pre,cur); for(i=3;i<=x;i++) { temp=cur; cur=pre+cur; pre=temp; printf(" %d",cur); } } main() { int n; clrscr(); printf("Enter number…"); scanf("%d",&n); fact(n); IsPrime(n); fibbo(n); getch(); }

Write a program using recursions for fibbonacci series in C language

#include<stdio.h> #include<conio.h> #include<process.h> fibbo(int pre, int cur ,int x) { int temp; if(x==2) { getch(); exit(0); } temp=cur; cur=pre+cur; pre=temp; printf("%d ",cur); fibbo(pre,cur,x-1); } main() { int n,pre=1,cur=1; clrscr(); printf("Enter number : "); scanf("%d",&n); printf("%d %d ",pre,cur); fibbo(pre,cur,n); getch(); }

Write a program using reccursions for fibbionacci series factorial in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
int fact(int n)
{
int f;
if(n==1)
return 1;
else
f=n*fact(n-1);
return f;
}
main()
{
int n;
clrscr();
printf("Enter number : ");
scanf("%d",&n);
printf("\nFactorial is…%d\n",fact(n));
getch();
} 

Write a program to accept a number n from user and then accept n array elements from user and reprint them in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20]; clrscr(); printf("Enter number…"); scanf("%d",&n); printf("Enter array elements :\n" ); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); } for(i=0;i<n;i++) printf("\nElement %d : %d",i+1,a[i]); getch(); }

Write a program to accept a number n from user and then accept n array elements from user and reprint them in reverse order of inputs in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20]; clrscr(); printf("Enter number…"); scanf("%d",&n); printf("Enter array elements :\n" ); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); } for(i=n-1;i>=0;i–) printf("\nElement %d : %d",i+1,a[i]); getch(); }

Write a program to accept a number n from user and then accept n array elements from user and print addition of those n array elements in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20],sum=0; clrscr(); printf("Enter number…"); scanf("%d",&n); printf("Enter array elements :\n" ); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); }   for(i=0;i<n;i++) sum=sum+a[i]; printf("\nSum of given array elements is : %d",sum); getch(); }

Write a program to accept a number n from user and print fibbonacci series up to nth level using arrays in C language

#include<stdio.h> #include<conio.h> main() { int a[20],n,i; clrscr(); printf("Enter number : "); scanf("%d",&n); a[1]=a[2]=1;   for(i=3;i<=n;i++) a[i]=a[i-1]+a[i-2];   for(i=1;i<=n;i++) printf(" %d",a[i]);   getch(); }

Write a program to accept a number n from user and then accept n array elements from user and print maximum and minimum array element from that set of array in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20],max,min; clrscr(); printf("Enter number : "); scanf("%d",&n); printf("Enter array elements…\n"); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); } max=min=a[0]; for(i=0;i<n;i++) { if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } printf("\nMin : %d",min); printf("\nMax : %d",max); getch(); }

Write a program to accept a number n from user and then accept n array elements from user, print positive & Negative numbers separately in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20]; clrscr(); printf("Enter number : "); scanf("%d",&n); printf("Enter array elements…\n"); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); }   printf("\n\nNegative Elements : \n"); for(i=0;i<n;i++) if(a[i]<0) printf("\n%d",a[i]);   printf("\n\nPositive Elements : \n"); for(i=0;i<n;i++) if(a[i]>0) printf("\n%d",a[i]);   getch(); }

Write a program to accept a number n from user and then accept n array elements from user, print these array elements in ascending and descending order in C language

#include<stdio.h> #include<conio.h> main() { int n,i,a[20],j,max,min,temp; clrscr(); printf("Enter number : "); scanf("%d",&n); printf("Enter array elements…\n"); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); } max=a[0]; for(i=0;i<n;i++) { for(j=0;j<i;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("Ascending order…..\n"); for(i=0;i<n;i++) { printf(" %d ",a[i]); }   min=a[0]; for(i=0;i<n;i++) { for(j=0;j<i;j++) if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("\nDescending order….\n"); for(i=0;i<n;i++) { printf(" %d ",a[i]); }   getch(); }

Write a program to accept a mXn matrix and reprint it in matrix form in C language

#include<stdio.h> #include<conio.h> main() { int i,j,m,n,a[5][5]; clrscr(); printf("Enter order of matrix :\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } getch(); }

Write a program to accept two mXn matrices and add them

#include<stdio.h> #include<conio.h> main() { int i,j,m,n,a[5][5],b[5][5],c[5][5]; clrscr(); printf("Enter order of matrix :\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements of first matrix…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter matrix elements of second matrix…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; printf("Resultant matrix …..\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",c[i][j]); printf("\n"); } getch(); }

Write a program to accept a mXn matrix and print addition of their array elements

#include<stdio.h> #include<conio.h> main() { int i,j,m,n,a[5][5],sum=0; clrscr(); printf("Enter order of matrix :\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) sum=sum+a[i][j]; printf("\nSum of matrix elements…..%d",sum); getch(); }

Write a program to accept a nXn matrix and print addition of digonal elements of that matrix

#include<stdio.h> #include<conio.h> main() { int i,j,m,a[5][5],sum=0; clrscr(); printf("Enter order of matrix :\n"); scanf("%d",&m); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<m;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) for(j=0;j<m;j++) if(i==j) sum=sum+a[i][j]; printf("\nSum of diagonal elements…..%d",sum); getch(); }

Write a program to accept a N x N matrix and print addition of upper triangular matrix elements

#include<stdio.h> #include<conio.h> main() { int i,j,m,a[5][5],sum=0; clrscr(); printf("Enter order of matrix :\n"); scanf("%d",&m); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<m;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) for(j=0;j<m;j++) if(i<j) sum=sum+a[i][j]; printf("\nSum of upper triangular elements…..%d",sum); getch(); }

Write a program to accept a N x N matrix and print addition of lower triangular matrix elements

#include<stdio.h> #include<conio.h> main() { int i,j,m,a[5][5],sum=0; clrscr(); printf("Enter order of matrix :\n"); scanf("%d",&m); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<m;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) for(j=0;j<m;j++) if(i>j) sum=sum+a[i][j]; printf("\nSum of lower triangular elements…..%d",sum); getch(); }

Write a program to accept two m X n matices and print their addition and multiplication

#include<stdio.h> #include<conio.h> main() { int i,j,m,n,k,a[5][5],b[5][5],c[5][5]; clrscr(); printf("Enter order of matrix :\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements of first matrix…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter matrix elements of second matrix…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Resultant matrix …..\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",c[i][j]); printf("\n"); } getch(); }

Write a program to accept a m X n matrix and print it’s transpose matrix

#include<stdio.h> #include<conio.h> main() { int i,j,m,n,a[5][5]; clrscr(); printf("Enter order of matrix :\n"); scanf("%d%d",&m,&n); printf("Enter matrix elements…..\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[j][i]); printf("\n"); } getch(); }

Write a program to accept a string and print the string in reverse order

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1; clrscr(); printf("Enter string : "); scanf("%s",s1); printf("Reverse String : %s",strrev(s1)); getch(); }

Write a program to calculate lenght of a given string

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1; clrscr(); printf("Enter string : "); scanf("%s",s1); printf("Lenght: %d",strlen(s1)); getch(); }

Write a program to accept a string and print the string in upper case

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1; clrscr(); printf("Enter string:"); scanf("%s",s1); printf("String in UPPER CASE : %s",strupr(s1)); getch(); }

Write a program to accept a string and print the string in lower case

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1; clrscr(); printf("Enter string: "); scanf("%s",s1); printf("String in lower case : %s",strlwr(s1)); getch(); }

Write a program to accept a string and print no. of aphlabets, digits, special symbols present in it

##include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> main() { char *s; int i,alpha=0,digit=0,symbol=0; clrscr(); printf("Enter string : "); scanf("%s",s); for(i=0;s[i]!=’\0';i++) if(isalpha(s[i])) alpha++; else if(isdigit(s[i])) digit++; else symbol++; printf("\nAphabets : %d",alpha); printf("\nDigits : %d",digit); printf("\nSymbols : %d",symbol); getch(); }

Write a program to accept two strings and compare them

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1,*s2; clrscr(); printf("Enter string 1 : "); scanf("%s",s1); printf("Enter string 2 : "); scanf("%s",s2); if(strcmp(s1,s2)>0) printf("\nString 1 is greater.."); else if(strcmp(s1,s2)<0) printf("\nString 2 is greater.."); else printf("\nStrings are equal.."); getch(); }

Write a program to accept two strings and concatenate them

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1,*s2; clrscr(); printf("Enter string 1 : "); scanf("%s",s1); printf("Enter string 2 : "); scanf("%s",s2); printf("\nCocatinated string is…%s",strcat(s1,s2)); getch(); }

Write a program to accept a string and copy it into another string

#include<stdio.h> #include<conio.h> #include<string.h> main() { char *s1,*s2; clrscr(); printf("Enter string 1 : "); scanf("%s",s1); strcpy(s2,s1); printf("\nCopied string is…%s",s2); getch(); }

Write a program to print length of a given string

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i,len=0; char *s; clrscr(); printf("Enter the string…."); scanf("%s",s); for(i=0;s[i]!=’\0';i++) len++; printf("The lenth of the string is…%d",len); getch(); }

Write a program to copy a string into another string

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i; char *s1,*s2; clrscr(); printf("Enter the string 1…."); scanf("%s",s1); for(i=0;s1[i]!=’\0';i++) { s2[i]=s1[i]; } s2[i]=’\0'; printf("The copied string is…%s",s2); getch(); }

Write a program to convert given string to UPPER CASE

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i; char *s1; clrscr(); printf("Enter the string 1…."); scanf("%s",s1); for(i=0;s1[i]!=’\0';i++) { if((s1[i]>=’a’)&&(s1[i]<=’z’)) s1[i]=s1[i]-32; } printf("Resultant string is…%s",s1); getch(); }

Write a program to convert givern string to lower case

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i; char *s1; clrscr(); printf("Enter the string 1…."); scanf("%s",s1); for(i=0;s1[i]!=’\0';i++) { if((s1[i]>=’A’)&&(s1[i]<=’Z’)) s1[i]=s1[i]+32; } printf("Resultant string is…%s",s1); getch(); }

Write a program to concatinate two strings

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i,len=0,j; char *s1,*s2; clrscr(); printf("Enter the string 1…."); scanf("%s",s1); printf("Enter the string 2…."); scanf("%s",s2); for(i=0;s1[i]!=’\0';i++) { len++; } j=0; for(i=len;s2[j]!=’\0';i++) { s1[i]=s2[j]; j++; } s1[i]=’\0'; printf("Resultant string is…%s",s1); getch(); }

Write a program to count no, of alphlabets, digits, special symbols

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i,alpha=0,digit=0,symbol=0; char *s; clrscr(); printf("Enter the string 1…."); scanf("%s",s); for(i=0;s[i]!=’\0';i++) { if(((s[i]>=’A’)&&(s[i]<=’Z’))||((s[i]>=’a’)&&(s[i]<=’z’))) alpha++; else if((s[i]>=’0’)&&(s[i]<=’9’)) digit++; else symbol++; } printf("\nAlphabets : %d",alpha); printf("\nDigits : %d",digit); printf("\nSymbols : %d",symbol); getch(); } Write a program to compare two strings in C language #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> main() { int i; char *s1,*s2; clrscr(); printf("Enter the string 1…."); scanf("%s",s1); printf("Enter the string 2…."); scanf("%s",s2); for(i=0;s1[i]!=’\0';i++) { if(s1[i]!=s2[i]) { if(s1[i]>s2[i]) printf("\nString 1 is greater…"); else printf("\nString 2 is greater…"); getch(); exit(0); } } printf("String are equal…."); getch(); }

Write a program to count all vowels present in the string

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i,vowel=0; char *s; clrscr(); printf("Enter the string 1…."); scanf("%s",s); for(i=0;s[i]!=’\0';i++) { if((s[i]==’A’)||(s[i]==’E’)||(s[i]==’O’)||(s[i]==’U’)||(s[i]==’I’)|| (s[i]==’a’)||(s[i]==’e’)||(s[i]==’o’)||(s[i]==’u’)||(s[i]==’i’)) vowel++; } printf("\nVowels : %d",vowel); getch(); }

Write a program to reverse the given string

#include<stdio.h> #include<conio.h> #include<string.h> main() { int i,j,len=0; char *s1,*s2; clrscr(); printf("Enter the string …."); scanf("%s",s1); for(i=0;s1[i]!=’\0';i++) len++; j=0; for(i=len-1;i>=0;i–) { s2[j]=s1[i]; j++; } s2[j]=’\0'; printf("\nReversed String : %s",s2); getch(); }

Write a program to check if the given string is palindrome or not E.g. NITIN, LIRIL.

#include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> main() { int i,j,len=0; char *s1,*s2; clrscr(); printf("Enter the string …."); scanf("%s",s1); for(i=0;s1[i]!=’\0';i++) len++; j=0; for(i=len-1;i>=0;i–) { s2[j]=s1[i]; j++; } s2[j]=’\0'; for(i=0;s1[i]!=’\0';i++) if(s1[i]!=s2[i]) { printf("\nNot a palindrome"); getch(); exit(0); } printf("Palindrome"); getch(); }


Define a structure Employee having elements emp_id, name,etc. Accept data and reprint it

#include<stdio.h> #include<conio.h> struct Employee { char name[50]; int emp_id; long phone_no; }; main() { struct Employee e; clrscr(); printf("Enter name : "); scanf("%s",&e.name); printf("Enter emp_id: "); scanf("%d",&e.emp_id); printf("Enter Phone Number: "); scanf("%ld",&e.phone_no); printf("\n\nEnter name : %s",e.name); printf("\n\nEnter Emp Id : %d",e.emp_id); printf("\n\nEnter Phone Number : %ld ",e.phone_no); getch(); }

Define a structure Student having fields roll_no, name, marks, etc, for 5 students, accept data and reprint

#include<stdio.h> #include<conio.h> struct Student { char name[50]; int roll_no; int m1,m2,m3; }; main() { int i; struct Student s[5]; clrscr(); for(i=0;i<5;i++) { printf("\nEnter data for Student %d…..\n",i+1); printf("Enter name : "); scanf("%s",&s[i].name); printf("Enter Roll No. : "); scanf("%d",&s[i].roll_no); printf("Enter marks for sub1 : "); scanf("%d",&s[i].m1); printf("Enter marks for sub2 : "); scanf("%d",&s[i].m2); printf("Enter marks for sub3 : "); scanf("%d",&s[i].m3); } for(i=0;i<5;i++) { printf("\nStudent %d\n",i+1); printf("Name : %s\n",s[i].name); printf("Roll No.: %d\n",s[i].roll_no); printf("Sub1 : %d\n",s[i].m1); printf("Sub2 : %d\n",s[i].m2); printf("Sub3 : %d\n",s[i].m3); } getch(); }

Define a structure Employee having elements emp_id, name, DOB, DOJ etc. Accept data and reprint it. (use structure within structure)

#include<stdio.h>
#include<conio.h>
struct Date
{
int mm,dd,yy;
};
struct Employee
{
char name[50];
int emp_id;
struct Date DOB,DOJ;
};
main()
{ int i;
struct Employee e;
clrscr();
printf("\nEnter name : ");
scanf("%s",&e.name);
printf("\nEnter emp_id. : ");
scanf("%d",&e.emp_id);
printf("\nEnter Date of Joining\n ");
printf("(dd-mm-yy) : ");
scanf("%d-%d-%d", &e.DOJ.dd,&e.DOJ.mm,&e.DOJ.yy);
printf("\nEnter Date of birth\n ");
printf("(dd-mm-yy) : ");
scanf("%d-%d-%d", &e.DOB.dd,&e.DOB.mm,&e.DOB.yy);
printf("\nName : %s",e.name);
printf("\nEmployee ID : %d",e.emp_id);
printf("\nEnter DOJ : %d-%d-%d", e.DOJ.dd,e.DOJ.mm,e.DOJ.yy);
printf("\nEnter DOB : %d-%d-%d", e.DOB.dd,e.DOB.mm,e.DOB.yy);
getch();
}

No comments:

Post a Comment

Please write your view and suggestion....