Matrics
#include<stdio.h> #include<conio.h> void main() { int m1[2][2],m2[2][2]; int i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n enter value of[%d][%d] element:"); scanf("%d",&m1[i][j]); scanf("%d",&m2[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n sum of [%d][%d] indices:%d",i,j,m1[i][j]+m2[i][j]); } } getch(); } *********************************************************************************************************Array of pointer
#include<stdio.h> #include<conio.h> void main() { int a[5],i,*ptr; clrscr(); ptr=a; for(i=0;i<5;i++) { scanf("%d",(ptr+i)); } for(i=0;i<5;i++) { printf("%d",*(ptr+i)); } getch(); } /**--------------output------------\\ 10 20 30 40 50 1020304050 */ *******************************************************************************************************************************array of reverse number
#include<stdio.h> #include<conio.h> void main() { int a[5],i; int *ptr; clrscr(); for(i=0;i<5;i++) { printf("enter the value:"); scanf("%d",&a[i]); } ptr=&a[4]; for(i=0;i<5;i++) { printf("%d",*ptr); ptr--; } getch(); } /**-------------output---------\\ enter the value:10 enter the value:20 enter the value:30 enter the value:40 enter the value:50 5040302010 */ *******************************************************************************************************************pointer to pointer
#include<stdio.h> #include<conio.h> void main() { int *ptr,a,**pptr; clrscr(); scanf("%d",&a); ptr=&a; pptr=&ptr; printf("address of a=%d",ptr); printf("address of ptr=%d",pptr); printf("value of pptr=%d",*pptr); printf("value of ptr=%d",*ptr); printf("value of a=%d",**pptr); getch(); } /**----------output----------\\ 10 address of a=-1422address of ptr=-1420value of pptr=-1422value of ptr=10value of a=10 */ ********************************************************************************************************************************************pointer in function
#include<stdio.h> #include<conio.h> void main() { int a,b; void swap(int*,int*); clrscr(); printf("enter two number:"); scanf("%d",&a); scanf("%d",&b); swap(&a,&b); getch(); } void swap(int*x,int*y) { int tmp; tmp=*x; *x=*y; *y=tmp; printf("a=%d,b=%d",*x,*y); getch(); } /**-------------output---------------\\ enter two number:10 20 a=20,b=10 */ ***************************************************************************************************************************************sturcture
#include<stdio.h> #include<conio.h> struct student { int rno; int marks; char name[20]; }; void main() { struct student s; clrscr(); printf("enter roll number:"); scanf("%d",&s.rno); printf("\n enter marks:"); scanf("%d",&s.marks); printf("\n enter name:"); scanf("%s",s.name); printf("\n roll number:%d",s.rno); printf("\n marks:%d",s.marks); printf("\n name:%s",s.name); getch(); } /**---------------output-------------\\ enter roll number:16 enter marks:30 enter name:dharti roll number:16 marks:30 name:dharti */
*********************************************************************
///////////////////////////////////////////////////////////////////////ARRAY
#include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); printf("\n Enter five numbers:"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } printf("Values of array is....."); for(i=0;i<5;i++) { printf("\n %d",a[i]); } getch(); } ************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a[5],i,max; clrscr(); for (i=0;i<5;i++) { printf("enter value for %d element:",i); scanf("%d",&a[i]); } max=a[0]; for (i=0;i<5;i++) { if(a[i]>max) max=a[i]; } printf("maximum %d",max); getch(); } /*-- enter value for 0 element:50 enter value for 1 element:60 enter value for 2 element:30 enter value for 3 element:32 enter value for 4 element:45 maximum 60 */ ********************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int age=0; int discount=0; long int fare=0; clrscr(); printf("enter your fare"); scanf("%d",&fare); printf("enter your age"); scanf("%d",&age); if(age<=15) discount=50; else if(age>=50) discount=20; discount=20; fare= fare-(fare*discount/100); printf("\n net air fare=%ld",fare); getch(); } /* enter your fare50 enter your age25 net air fare=40 */ ************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); for(i=0;i<5;i++) { printf("\n enter the value in array:"); scanf("%d",&a[i]); } printf("value of array"); for (i=0;i<5;i++) { printf("\n%d",a[i]); } getch(); } /* enter the value in array:20 enter the value in array:30 enter the value in array:50 enter the value in array:10 enter the value in array:58 value of array 20 30 50 10 58 */ ********************************************************************************************************************************************* #include<stdio.h> #include<conio.h> void main() { int m1[2][2],m2[2][2]; int i,j; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("enter value [%d] [%d]element:",i,j); scanf("%d",&m1[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("enter value [%d] [%d]element:",i,j); scanf("%d",&m2[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n sum of [%d] [%d] indices:%d",i,j,m1[i][j]+m2[i][j]); } } getch(); } /* enter value [0] [0]element:5 enter value [0] [1]element:4 enter value [1] [0]element:3 enter value [1] [1]element:2 enter value [0] [0]element:1 enter value [0] [1]element:6 enter value [1] [0]element:7 enter value [1] [1]element:8 sum of [0] [0] indices:6 sum of [0] [1] indices:10 sum of [1] [0] indices:10 sum of [1] [1] indices:10 */ ************************************************************************************************************************************************ #include<stdio.h> #include<conio.h> void main() { int i,n,j; clrscr(); printf("enter number of row"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) printf(" %d ", j); printf("\n"); } getch(); } /* enter number of row12 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 */ *********************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int no1,no2; FILE *fptr; clrscr(); fptr=fopen("z:/fop/student.txt","w+"); if(fptr==NULL) { printf("error---!"); exit(1); } printf("enter nos:"); scanf("%d",&no1); fprintf(fptr,"no1=%d",no1); scanf("%d",&no2); fprintf(fptr,"no2=%d",no2); no1=no2=0; fscanf(fptr,"%d",&no1); printf("\n no1=%d",no1); fscanf(fptr,"%d",&no2); printf("\n no2=%d",no2); fclose(fptr); getch(); } /*------------------- enter nos:1 2 no1=1 no2=2 enter nos:40 23 no1=0 no2=0 */ ********************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("enter value of a b and c:"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) { printf("a is grater=%d",a); } else { if(b>c) { printf("b is grater=%d",b); } else { printf("c is grater=%d",c); } } getch(); } /* enter value of a b and c:20 12 10 a is grater=20 */ ************************************************************************************************************************************************************ #include<stdio.h> #include<conio.h> void main() { int no,i; clrscr(); printf("enter no:"); scanf("%d",&no); for (i=1;i<=no;i++) { if(i%2==0) printf("\nEven no : %d",i); else printf("\tOdd no : %d",i); } getch(); } /*enter no:23 Odd no : 1 Even no : 2 Odd no : 3 Even no : 4 Odd no : 5 Even no : 6 Odd no : 7 Even no : 8 Odd no : 9 Even no : 10 Odd no : 11 Even no : 12 Odd no : 13 Even no : 14 Odd no : 15 Even no : 16 Odd no : 17 Even no : 18 Odd no : 19 Even no : 20 Odd no : 21 Even no : 22 Odd no : 23 */ ****************************************************************************************************************************************** #include<stdio.h> #include<conio.h> struct student { int rno; char name[]; struct marks { int sub1; int sub2; int sub3; }m; }s; void main() { struct student s; clrscr(); //reading printf("enter the roll no="); scanf("%d",&s.rno); printf("enter the name="); scanf("%s",s.name); printf("enter the three sub marks="); scanf("%d",&s.m.sub1); scanf("%d",&s.m.sub2); scanf("%d",&s.m.sub3); //display printf("\n sub1:%d",s.m.sub1); printf("\n sub2:%d",s.m.sub2); printf("\n sub3:%d",s.m.sub3); getch(); } //---------------------------**-----------------------------// /* enter the roll no=8 enter the name=shweta enter the three sub marks=42 41 40 */ sub1:42 sub2:41 sub3:40 //-------------------------**----------------------------\\ ********************************************************************************************************************************************* #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); for(i=0;i<5;i++) { printf("enter no:"); scanf("%d",&a[i]); } for (i=1;i<5;i++) { if(a[i]%2==0) printf("\nEven no : %d", a[i]); } for (i=1;i<=5;i++) { if(a[i]%2!=0) printf("\nOdd no : %d", a[i]); } getch(); } /* enter no:13 enter no:20 enter no:56 enter no:15 enter no:20 Even no : 20 Even no : 56 Even no : 20 Odd no : 15 */ ********************************************************************************************************************************************* #include<stdio.h> #include<conio.h> struct marks { int rno; int marks; }; void main() { struct marks s; void printmarks(struct marks); clrscr(); //reading printf("enter the roll no="); scanf("%d",&s.rno); printf("enter the marks="); scanf("%d",&s.marks); printmarks(s); getch(); } void printmarks(struct marks m) { printf("roll no:%d",m.rno); printf("marks:%d",m.marks); } //---------------------------**--------------------------// /* enter the roll no=12 enter the marks=45 roll no:12marks:45 */ //----------------------------------------------------------/ ********************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int *ptr,a,**pptr; clrscr(); scanf("%d",&a); ptr=&a; pptr=&ptr; printf("address of a=%d",ptr); printf("address of ptr=%d",pptr); printf("value of pptr=%d",pptr); printf("value of ptr=%d",ptr); printf("value of a=%d",**pptr); getch(); } //-----------------------------------**--------------------------------\\ /* 20 address of a=-1438address of ptr=-1436value of pptr=-1436value of ptr=-1438value of a=20 */ ******************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a[5],i,max; int*ptr; clrscr(); ptr=a; for(i=0;i<5;i++) { printf("enter the number:"); scanf("%d",&a[i]); } max=a[0]; for(i=1;i<5;i++) { if(a[i]>max) max=a[i]; } printf("maximum= %d",max); getch(); } //---------------------------**---------------------------// /* enter the number:23 enter the number:56 enter the number:98 enter the number:45 enter the number:12 maximum= 98 */ ************************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int *ptr,a,**pptr; clrscr(); scanf("%d",&a); ptr=&a; pptr=&ptr; printf("address of a=%d",ptr); printf("address of ptr=%d",pptr); printf("value of pptr=%d",pptr); printf("value of ptr=%d",ptr); printf("value of a=%d",**pptr); getch(); } //-----------------------------------**--------------------------------\\ /* 20 address of a=-1438address of ptr=-1436value of pptr=-1436value of ptr=-1438value of a=20 */ ************************************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int* add(int*,int); int a[5],i,*s; clrscr(); for(i=0;i<5;i++) { printf("enter the value="); scanf("%d",&a[i]); s=add(a,5); } printf("%d",*s); getch(); } int* add(int*x,int size) { int i,sum=0; for(i=0;i<size;i++) sum=sum+x[i]; return(&sum); } //------------------------**---------------------------------------// enter the value=10 enter the value=52 enter the value=63 enter the value=85 enter the value=41 251 //-------------------------------------**------------------\\ ********************************************************************************************************************************************* #include<stdio.h> #include<conio.h> void main() { int a,b; // clrscr(); void swap(int *, int *); clrscr(); printf("enter value of a="); scanf("%d",&a); printf("\n enter value of b="); scanf("%d",&b); swap(&a,&b); getch(); } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; printf("a=%d,b=%d",*x,*y); } //-----------------------------**----------------------------// /* enter value of a=12 enter value of b=3 a=3,b=12 */ ************************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a,b; // clrscr(); void swap(int *, int *); clrscr(); printf("enter value of a="); scanf("%d",&a); printf("\n enter value of b="); scanf("%d",&b); swap(&a,&b); getch(); } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; printf("a=%d,b=%d",*x,*y); } //-----------------------------**----------------------------// /* enter value of a=12 enter value of b=3 a=3,b=12 */ ************************************************************************************************************************************************* #include<stdio.h> #include<conio.h> struct student { int rno; int marks; char name[]; }; void main() { struct student s[5]; int i,sum=0; clrscr(); //reading for(i=0;i<5;i++) { printf("enter rno="); scanf("%d",&s[i].rno); printf("enter marks="); scanf("%d",&s[i].marks); printf("enter student name="); scanf("%s",s[i].name); sum=sum+s[i].marks; } { printf("total sum of marks is=%d",sum); } getch(); } //------------------------**-------------------------------// enter rno=1 enter marks=25 enter student name=a enter rno=2 enter marks=30 enter student name=b enter rno=3 enter marks=40 enter student name=c enter rno=4 enter marks=50 enter student name=d enter rno=5 enter marks=45 enter student name=e total sum of marks is=190 //-------------------------------**-------------------------------- ********************************************************************************************************************************************** #include<stdio.h> #include<conio.h> struct student { int rno; int marks; char name; }; void main() { struct student s1,s2; clrscr(); //reading printf("enter rno="); scanf("%d",&s1.rno); printf("enter marks="); scanf("%d",&s1.marks); printf("enter student name="); scanf("%s",s1.name); printf("enter rno="); scanf("%d",&s2.rno); printf("enter marks="); scanf("%d",&s2.marks); printf("enter student name="); scanf("%s",s2.name); //display printf("\n rno;%d",s1.rno); printf("\n marks:%d",s1.marks); printf("\n name:%s",s1.name); printf("\n rno;%d",s2.rno); printf("\n marks:%d",s2.marks); printf("\n name:%s",s2.name); getch(); } //---------------------------------**-----------------------------// enter rno=1 enter marks=50 enter student name=ashish enter rno=2 enter marks=45 enter student name=shweta rno;1 marks:50 name:ashish rno;2 marks:45 name:shweta //--------------------------------**---------------------------------- ************************************************************************************************************************************************** #include<stdio.h> #include<conio.h> struct student { int rno; int marks; char name[20]; }; void main() { struct student s; //reading clrscr(); printf("enter rno="); scanf("%d",&s.rno); printf("enter marks="); scanf("%d",&s.marks); printf("enter student name="); scanf("%s",s.name); //display printf("\n rno:%d",s.rno); printf("\n marks:%d",s.marks); printf("\n name:%s",s.name); getch(); } //--------------------------**--------------------------------// enter rno=8 enter marks=40 enter student name=shweta rno:8 marks:40 name:shweta //-----------------------------**--------------------------// ************************************************************************************************************************************************* #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s3[20]; char s1[10]; char s2[5]; int ln=0; clrscr(); printf("Enter string S1 :"); scanf("%s",s1); printf("\nEnter string S2 :"); scanf("%s",s2); printf("\nConcatinated String : %s", strcat(s1,s2)); // strcpy(s1,s2); ln=strlen(s1); printf("\n\nLength if s1 = %d", ln); if(strcmp(s1,s2)==0) printf("\n\nequal strcmp"); else if(strcmp(s1,s2)<0) printf("\n\ns2 is greater"); else if(strcmp(s1,s2)>0) printf("\n\ns1 is greater"); getch(); } /* Enter string S1 :10 Enter string S2 :20 Concatinated String : 1020 Length if s1 = 4 s2 is greater */ ************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { char ch[10]; clrscr(); printf("enter city name:"); scanf("%s",ch); printf("\n entered city is %s",ch); getch(); } /* enter city name:kapadwanj entered city is kapadwanj */ *************************************************************************************************************************************** #include<stdio.h> #include<conio.h> void main() { int a[5],i,sum=0; int *ptr; ptr=a; clrscr(); for(i=0;i<5;i++) { printf("enter the value="); scanf("%d",&a[i]); } printf("sum of"); for(i=0;i<5;i++) { printf(" %d",*ptr); sum=sum+*ptr; ptr++; } printf("is:%d",sum); getch(); } //-----------------------**--------------------------// enter the value=1 enter the value=2 enter the value=3 enter the value=4 enter the value=5 sum of 1 2 3 4 5is:15
.Write a program to find out factorial of read number n. (eg. 4! = 4*3*2*1) #include<stdio.h> #include<conio.h> void ft(int); void main() { int n; clrscr(); scanf("%d",&n); ft(n); getch(); } void ft(int n) { int i, f=1; for(i=n;i>=1;i--) f=i*f; printf("%d",f); } /*------output----------- 10 24320 */ ******************************************************************************************************************************************* ****************************************************************************************************************************** Write a program to calculate the net salary of an employee, if a tax of 15% is levied on his gross-salary if it exceeds Rs. 10,000/- per month. #include<stdio.h> #include<conio.h> void main() { float a,b,c; clrscr(); printf("please enter your salary"); scanf("%f",&a); if(a>=10000) { b=a*0.15; c=a-b; printf("your net salary is=%f",c); } else printf("your salary %f",a); getch(); } //***************OUTPUT************// please enter your salary10000 your net salary is=8500.000000 */ ******************************************************************************************************************************** ************************************************************************************************************************** Write a program to print whether the given number is prime or not. #include<stdio.h> #include<conio.h> void main() { int n,i,sum=0; clrscr(); printf("enter your value:"); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) { sum++; break; } }//for over........... if(sum==0 && n!=1) { printf("this is prime number"); } else { printf("this is not prime number"); } getch(); } //*********OUTPUT**********// enter your value:7 this is not prime number **************************************************************************************************************************** *********************************************************************************************************************************************** 1.Write a program to calculate an Air ticket fare after discount on following conditions If passenger age is below 14 yrs then discount is 50% on fare If passenger age is above 50 yrs then discount is 20% on fare If other ages of passenger discount is 10% on fare #include<stdio.h> #include<conio.h> void main() { float a=0.0,b=0.0,c=0.0; printf("enter fare"); scanf("%d",&a); printf("enter age"); scanf("%d",&b); if(b>50) { c=(a*20)/100; printf("your fare after 20 percent discount%f",c); } if(b>14) { c=(a*10)/100; printf("your fare after 10 percent discount%f",c); } getch(); } ///***************OUTPUT*************/// //enter fare50 // enter age20 // your fare after 50 percent discount0.000000enter fare //ener fare14 // enter age10 // your fare after 50 percent discount0.000000 ***************************************************************************************************************************** **************************************************************************************************************************************************************************** Read alphabets from VIBGYOR and display appropriate color and put message for other characters. #include<stdio.h> #include<conio.h> void main() { char ans; clrscr(); printf("please enter is values"); scanf("%c",&ans); switch(ans) { case 'v': case 'V': printf("color is violet"); break; case 'i': case 'I': printf("color is indigo"); break; case 'b': case 'B': printf("color is blue"); break; case 'g': case 'G': printf("color is green"); break; case 'y': case 'Y': printf("color is yellow"); break; case 'o': case 'O': printf("color is orenge"); break; case'r': case'R': printf("color is red"); break; default: printf("please enter proper value"); break; } getch(); } //****************OUTPUT****************// // please enter is valuesv // color is violet // please enter is valuesi //color is indigo //please enter is valuesb //color is blue //please enter is valuesg // color is green //please enter is valuesy //color is yellow //please enter is valueso // color is orengee //please enter is valuesr // color is redd *********************************************************************************************************************************** ********************************************************************************************************************************** Write a program to generate Fibonacci series of n numbers, where n should be given by user. (eg. n=5 then 0, 1, 1, 2, 3 i.e. fi = fi-1 + fi-2) #include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e; clrscr(); printf("please enter your number:"); scanf("%d",&b); printf("fibonacci series:"); printf("\t %d %d",c,d); for(a=2;a<b;a++) { e=c+d; c=d; d=e; printf("\t%d",d); } getch(); } /*********************OUTPUT************** please enter your number:5 fibonacci series: 12803 1284 14087 15371 29458 ***************/ ******************************************************************************************************************************************************************* ******************************************************************************************************************************************************************* Generate the following “pyramid” of digits using nested loops. 1 232 34543 4567654 567898765 #include <stdio.h> #include<conio.h> int main() { int i,space,rows,k=0,count=0,count1=0; printf("Enter the number of rows: "); scanf("%d",&rows); for(i=1;i<=rows;++i) { for(space=1;space<=rows-i;++space) { printf(" "); ++count; } while(k!=2*i-1) { if (count<=rows-1) { printf("%d ",(i+k)); ++count; } else { ++count1; printf("%d ", (i+k-2*count1)); } ++k; } count1=count=k=0; printf("\n"); } getch(); } /****************output********************** Enter the number of rows: 6 1 2 3 3 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 10 11 10 9 8 7 66 ******************************************************************************************************************************************************** ********************************************************************************************************************************************************* Q] Determine the roots of the quadratic equation a2 +2ab+b2. ANS: #include<stdio.h> #include<conio.h> void main() { int a,b; int sq(int); clrscr(); scanf("%d",&a); scanf("%d",&b); printf("solution =%d",sq(a)+(2*a*b)+sq(b)); getch(); } int sq(int x) { return(x*x); } /*-------------------------------- a2+2ab+b2 solution =-32119 */ ********************************************************************************************************************************** ********************************************************************************************************************************** #include<stdio.h> #include<string.h> #include<conio.h> int main() { char str[100], temp; int i, j = 0; printf("\nEnter the string :"); gets(str); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("\nReverse string is :%s", str); getch(); } /************output************ Enter the string :vishal Reverse string is :lahsiv Enter the string :dhamo Reverse string is :omahd Enter the string : Reverse string is : Enter the string :vishal Reverse string is :lahsiv*****/-------------------------------------------------------------------------------------------------------------------------------------------------
[POINTER] Q1]WAP to swap the numbers using pointer. ANS: #include<stdio.h> #include<conio.h> void main() { int a,b; void swap(int* ,int*); clrscr(); printf("enter two number:"); scanf("%d",&a); scanf("%d",&b); swap(&a,&b); getch(); } void swap(int*x,int*y) { int temp; temp=*x; *x=*y; *y=temp; printf("a = %d,b = %d",*x ,*y); } /*------------------------ enter two number:45 25 a = 25,b = 45 */ ============================================================================================== Q2]WAP to find the maximum value of an array of five elements using pointer. ANS: #include<stdio.h> #include<conio.h> void main() { int a[5],i,max; int *ptr; clrscr(); ptr =a; for(i=0;i<5;i++) { printf("enter the number:"); scanf("%d",&a[i]); } max=a[0]; for(i=1;i<5;i++) { if(a[i]>max) max=a[i]; } printf("maximum =%d",max); getch(); } /**------------------------**----------------------------\\ enter the number:49 enter the number:56 enter the number:97 enter the number:98 enter the number:99 maximum =99 */ ===================================================================================================================== Q[3]WAP to read 3 words in various scalar variables and using an array of pointers display them in sorted order. ANS: #include<stdio.h> #include<conio.h> #include <string> #include <iostream> using namespace std; int main(int argc, char *argv[]) { // Create an array holding 3 fruits. string fruit[] = {"Apple", "Orange", "Banana"}; // This would not work if fruit was a pointer. int size = sizeof(fruit)/sizeof(string); // Loop through and print all values. for(int i=0; i < size; i++) { cout << fruit[i] << endl; } } /*---------------------------------- Apple Orange Banana */ ====================================================================================================== Q[4]WAP to display transposed matrix using pointer to matrix. ANS: #include<stdio.h> #include<conio.h> int main() { int a[10][10], trans[10][10], r, c, i, j; clrscr(); printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &c); /* Storing element of matrix entered by user in array a[][]. */ printf("\nEnter elements of matrix:\n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } /* Displaying the matrix a[][] */ printf("\nEntered Matrix: \n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("%d ",a[i][j]); if(j==c-1) printf("\n\n"); } /* Finding transpose of matrix a[][] and storing it in array trans[][]. */ for(i=0; i<r; ++i) for(j=0; j<c; ++j) { trans[j][i]=a[i][j]; } /* Displaying the transpose,i.e, Displaying array trans[][]. */ printf("\nTranspose of Matrix:\n"); for(i=0; i<c; ++i) for(j=0; j<r; ++j) { printf("%d ",trans[i][j]); if(j==r-1) printf("\n\n"); } return 0; } /*-------------------------------- Enter rows and column of matrix: 2 3 Enter elements of matrix: Enter elements a11: 1 Enter elements a12: 2 Enter elements a13: 9 Enter elements a21: 0 Enter elements a22: 4 Enter elements a23: 7 Entered Matrix: 1 2 9 0 4 7 Transpose of Matrix: 1 0 2 4 9 7 */ ======================================================================================================================== Q[5]WAP to define pointer to a function for display product of two numbers. ANS: #include<stdio.h> #include<conio.h> void main() { int* add(int*,int); int a[2],i,*s; clrscr(); for(i=0;i<2;i++) { printf("enter the two number:"); scanf("%d",&a[i]); } s=add(a,2); printf("%d",*s); getch(); } int* add(int *x,int size) { int i,sum=0; for(i=0;i<size;i++) sum=sum+(*(x+i)); return(&sum); } /*-------------------------------- enter the two number:2 enter the two number:3 5 */ ============================================================================================================================= [Structure] [1] WAP to generate Electricity Bill for five users. ANS: #include<stdio.h> #include<conio.h> struct bill { int rno,amount; char name[20]; }; void main() { struct bill s[5]; int i,j; clrscr(); for(i=0;i<5;i++) { printf("enter the total bill amount:"); scanf("%d",&s[i].amount); printf("enter the name:"); scanf("%s",&s[i].name); printf("entre the bill number:"); scanf("%d",&s[i].rno); } clrscr(); for(j=0;j<5;j++) { printf("name:%s\n",s[j].name); printf("number:%d\n",s[j].rno); printf("total bill amount:%d\n",s[j].amount); } getch(); } /*----------------------**--------\\ /* name:pooja number:11 total bill amount:1250 name:alpa number:8 total bill amount:1326 name:vishal number:21 total bill amount:2450 name:dharti number:16 total bill amount:3650 name:neha number:19 total bill amount:1000 */ ============================================================================================================================= Q3] WAP to read student details and display their detail and percentage along with grade using nested structure. ANS: #include <stdio.h> #include <string.h> struct student_college_detail { int college_id; char college_name[50]; }; struct student_detail { int id; char name[20]; float percentage; // structure within structure struct student_college_detail clg_data; }stu_data, *stu_data_ptr; int main() { struct student_detail stu_data = {1, "Raju", 90.5, 71145, "Anna University"}; stu_data_ptr = &stu_data; printf(" Id is: %d \n", stu_data_ptr->id); printf(" Name is: %s \n", stu_data_ptr->name); printf(" Percentage is: %f \n\n", stu_data_ptr->percentage); printf(" College Id is: %d \n", stu_data_ptr->clg_data.college_id); printf(" College Name is: %s \n", stu_data_ptr->clg_data.college_name); return 0; } Output: Id is: 1 Name is: Raju Percentage is: 90.500000 College Id is: 71145 College Name is: Anna University ============================================================================================================================== [-------------Dynamic Memory Allocation-----------] Q1] WAP to create dynamic array of numbers of user defined elements and display the numbers in reverse order. ANS: #include <stdio.h> #include <stdlib.h> int main(){ int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: "); for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr); getch(); } /*******************output******************* Enter number of elements: 5 Enter elements of array: 4 5 6 6 6 Sum=27 ************************************/ ================================================================================================================================ Q3]WAP to sort the words read by user. ANS: #include <stdio.h> #include <conio.h> #include <string.h> void main(){ int i,n; char name[15][20]; void namesort(char name[15][20], int); clrscr(); printf("How many names u want to enter : "); scanf("%d",&n); printf("\n\n****Enter Input Data****\n"); for(i=0;i<n;i++){ flushall(); printf("Enter a name : "); gets(name[i]); } namesort(name,n); printf("\n\n\n******After Sorting******\n"); for(i=0;i<n;i++) printf("%s\n",name[i]); getch(); } void namesort(char name[15][20],int n){ int i,j; char *temp; for(i=0;i<n;i++){ for(j=0;j<n-1;j++){ if(strcmp(name[j],name[j+1])>0){ strcpy(temp,name[j]); strcpy(name[j],name[j+1]); strcpy(name[j+1],temp); } } } } /*********output************** How many names u want to enter : 3 ****Enter Input Data**** Enter a name : vishal Enter a name : parth Enter a name : dhruv ******After Sorting****** dhruv parth vishal */ ================================================================================================================================== [-------------Files---------------] [1] WAP to count the total numbers of characters and lines in file. ANS: #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; char a[20]; int nol=0,not=0,nob=0,noc=0; char c; clrscr(); printf("Enter the name of File:\n"); gets(a); if((fp=fopen(a,"r"))==NULL) { printf("File dosen't exist."); } else { while(1) { c=fgetc(fp); if(c==EOF) break; noc++; if(c==' ') nob++; if(c=='\n') nol++; if(c=='\t') not++; } } fclose(fp); printf("Number of characters = %d\n",noc); printf("Number of blanks = %d\n",nob); printf("Number of tabs = %d\n",not); printf("Number of lines = %d\n",nol); getch(); } Output Enter the name of file: cb.txt Number of characters = 31 Number of blanks = 2 Number of tabs = 0 Number of lines = 1 ********************************************************************************************************* 2)WAP to compare the contents of two files with display message whether same or not. ANS: #include<stdio.h> #include<conio.h> int main() { FILE *fp1, *fp2; int ch1, ch2; char fname1[40], fname2[40]; printf("Enter name of first file :"); gets(fname1); printf("Enter name of second file:"); gets(fname2); fp1 = fopen(fname1, "r"); fp2 = fopen(fname2, "r"); if (fp1 == NULL) { printf("Cannot open %s for reading ", fname1); exit(1); } else if (fp2 == NULL) { printf("Cannot open %s for reading ", fname2); exit(1); } else { ch1 = getc(fp1); ch2 = getc(fp2); while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) { ch1 = getc(fp1); ch2 = getc(fp2); } if (ch1 == ch2) printf("Files are identical n"); else if (ch1 != ch2) printf("Files are Not identical n"); fclose(fp1); fclose(fp2); } return (0); } Output They are same. *************************************************************************************************************** [3] WAP to copy first 10 lines into another file. ANS: #include<stdio.h> #include<process.h> void main() { FILE *fp1, *fp2; char a; clrscr(); fp1 = fopen("test.txt", "r"); if (fp1 == NULL) { puts("cannot open this file"); exit(1); } fp2 = fopen("test1.txt", "w"); if (fp2 == NULL) { puts("Not able to open this file"); fclose(fp1); exit(1); } do { a = fgetc(fp1); fputc(a, fp2); } while (a != EOF); fcloseall(); getch(); } Output : Content will be written successfully to file ********************************************************************************************************************* [5] WAP to display a number from the file from the location given by user. ANS: #include <stdio.h> #include <stdlib.h> int main() { char ch, file_name[25]; FILE *fp; printf("Enter the name of file you wish to see\n"); gets(file_name); fp = fopen(file_name,"r"); // read mode if( fp == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } printf("The contents of %s file are :\n", file_name); while( ( ch = fgetc(fp) ) != EOF ) printf("%c",ch); fclose(fp); return 0; } output Enter the name of file you wish to see computer-programming.txt The content of computer-programming.txt file are : Computer Programming is too much fun. =================================================================================================================================1]WAP to find the largest element from the array of size 10. ANS:- #include<stdio.h> #include<conio.h> void main() { int a[10],i,max; for(i=0;i<10;i++) { printf("enter the max number:"); scanf("%d",&a[i]); } max=a[0]; for(i=1;i<10;i++) { if(a[i]>max) max=a[i]; } printf("maximum=%d",max); getch(); } /*----------------------- enter the max number:58 enter the max number:25 enter the max number:65 enter the max number:89 enter the max number:35 enter the max number:56 enter the max number:87 enter the max number:483 enter the max number:59 enter the max number:125 maximum=483 */ ============================================================================ Q2]WAP to merge two arrays of five elements and display the final array. ANS: #include<stdio.h> #include<conio.h> int main() { int arr1[30], arr2[30], res[60]; int i, j, k, n1, n2; clrscr(); printf("\nEnter no of elements in 1st array :"); scanf("%d", &n1); for (i = 0; i < n1; i++) { scanf("%d", &arr1[i]); } printf("\nEnter no of elements in 2nd array :"); scanf("%d", &n2); for (i = 0; i < n2; i++) { scanf("%d", &arr2[i]); } i = 0; j = 0; k = 0; // Merging starts while (i < n1 && j < n2) { if (arr1[i] <= arr2[j]) { res[k] = arr1[i]; i++; k++; } else { res[k] = arr2[j]; k++; j++; } } /* Some elements in array 'arr1' are still remaining where as the array 'arr2' is exhausted */ while (i < n1) { res[k] = arr1[i]; i++; k++; } /* Some elements in array 'arr2' are still remaining where as the array 'arr1' is exhausted */ while (j < n2) { res[k] = arr2[j]; k++; j++; } //Displaying elements of array 'res' printf("\nMerged array is :"); for (i = 0; i < n1 + n2; i++) printf("%d ", res[i]); return (0); } /***************************************************** Output: Enter no of elements in 1st array :4 5 6 8 5 Enter no of elements in 2nd array :2 5 6 Merged array is :5 5 6 6 8 5 Enter no of elements in 1st array : ********************************************/ ============================================================================= q3]WAP to find the sum of odd and even elements and display the sum of both. ans: #include<stdio.h> #include<conio.h> void main() { int num[20]; int i , esum , osum , N; clrscr(); esum = 0; osum = 0; printf("Enter the size of Array\n"); scanf("%d" , &N); printf(" Enter Array elements\n"); for(i=0 ; i<N ; i++) { scanf("%d" , &num[i]); } for(i=0 ; i<N ; i++) { if((num[i] %2) ==0) { esum+ = num[i]; } else { osum+ = num[i]; } } printf(" Sum of even Numbers = %d\n" , esum); printf(" Sum of odd Numbers = %d\n" , osum); } Output: Enter the size of Array 5 Enter the Array elements 55 21 36 10 14 Sum of even Numbers = 60 Sum of odd Numbers = 76 =========================================================================== Q4]WAP to transpose the square matrix of 3x3. ans: #include <stdio.h> #include<conio.h> int main() { int a[10][10], trans[10][10], r, c, i, j; printf("Enter rows and column of matrix: "); scanf("%d %d", &r, &c); /* Storing element of matrix entered by user in array a[][]. */ printf("\nEnter elements of matrix:\n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("Enter elements a%d%d: ",i+1,j+1); scanf("%d",&a[i][j]); } /* Displaying the matrix a[][] */ printf("\nEntered Matrix: \n"); for(i=0; i<r; ++i) for(j=0; j<c; ++j) { printf("%d ",a[i][j]); if(j==c-1) printf("\n\n"); } /* Finding transpose of matrix a[][] and storing it in array trans[][]. */ for(i=0; i<r; ++i) for(j=0; j<c; ++j) { trans[j][i]=a[i][j]; } /* Displaying the transpose,i.e, Displaying array trans[][]. */ printf("\nTranspose of Matrix:\n"); for(i=0; i<c; ++i) for(j=0; j<r; ++j) { printf("%d ",trans[i][j]); if(j==r-1) printf("\n\n"); } getch(); } /****************output************** Enter rows and column of matrix: 3 3 Enter elements of matrix: Enter elements a11: 1 Enter elements a12: 2 Enter elements a13: 3 Enter elements a21: 4 Enter elements a22: 5 Enter elements a23: 6 Enter elements a31: 7 Enter elements a32: 8 Enter elements a33: 9 Entered Matrix: 1 2 3 4 5 6 7 8 9 Transpose of Matrix: 1 4 7 2 5 8 3 6 9 *************************/ ===================================================================== Q5]WAP to multiply two matrices and display it. ANS: #include <stdio.h> #include <conio.h> void main() { int m1[2][2],m2[2][2]; int i,j; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n enter value [%d][%d] element:",i,j); scanf("%d",&m1[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n enter value [%d][%d] element:",i,j); scanf("%d",&m2[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\n multiply of [%d][%d] indice:%d",i,j,m1[i][j]*m2[i][j]); } } getch(); } //*****************output****************************// //enter value [0][0] element:12 // enter value [0][1] element:13 //enter value [1][0] element:15 // enter value [1][1] element:16 //enter value [0][0] element:21 // enter value [0][1] element:14 //enter value [1][0] element:10 //enter value [1][1] element:16 // multiply of [0][0] indice:252 //multiply of [0][1] indice:182 // multiply of [1][0] indice:150 // multiply of [1][1] indice:256 ======================================================================================================== [-------------Functions---------------] 1)WAP to sort the elements of an array of size 5 using Function. ANS: void sort(int m, int x[ ]); main() { int i; int marks[5] = {40, 90, 73, 81, 35}; printf("Marks before sorting\n"); for(i = 0; i < 5; i++) printf("%d ", marks[i]); printf("\n\n"); sort (5, marks); printf("Marks after sorting\n"); for(i = 0; i < 5; i++) printf("%4d", marks[i]); printf("\n"); } void sort(int m, int x[ ]) { int i, j, t; for(i = 1; i <= m-1; i++) for(j = 1; j <= m-i; j++) if(x[j-1] >= x[j]) { t = x[j-1]; x[j-1] = x[j]; x[j] = t; } } Output Marks before sorting 40 90 73 81 35 Marks after sorting 35 40 73 81 90 ********************************************************************************************************* Q]WAP to display the Fibonacci series of read length using concept of recursion. ANS: #include<stdio.h> #include<conio.h> int size; int fibonacci(int prev_number, int number); void main() { static int prev_number = 0, number = 1; clrscr(); printf("Enter the Size of Series (< 20) : "); scanf("%d", &size); printf("1 "); fibonacci(prev_number, number); getch(); } int fibonacci(int prev_number, int number) { static int i = 1; int next_num; if (i == size) return (0); else { next_num = prev_number + number; prev_number = number; number = next_num; printf("%d ", next_num); i++; fibonacci(prev_number, number); //recursion } return (0); } Output : Enter the Size of Series (< 20) : 6 1 1 2 3 5 8 13 ******************************************************************************************************* Q]WAP to insert an element in a sorted array using functions. ANS: #include <stdio.h> int insert(int array[],int val); int main (void) { int arr[5],j; for (j = 0; j<5; j++) { scanf("%d",&arr[j]); } insert(arr,2); for(j = 0;j<6;j++) printf("%d",arr[j]); return(0); } int insert(int array[],int val) { int k,i; for (k = 0;k<5;k++) if(val<array[k]) break; for (i = 4; i>=k;i--) { array[i+1] = array[i]; } array[k] = val; return(1); } *************************************************************************************************** Q]WAP to encrypt and decrypt the string given by user. #include<stdio.h> #include<string.h> int main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); if( strcmp(a,b) == 0 ) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); return 0; } Output Enter the first string abcd Enter the second string abcd Entered strings are equal. ***************************************************************************************************
0 comments:
Post a Comment