To Find the Greatest among 3 Number Using if else Statement
Syntax:- #include <stdio.h> #include <conio.h> void main() { clrscr(); int a,b,c; printf(Enter a number); scanf(%d%d%d ,&a,&b,&c); if (a>b & a>c) { printf(a is greater then b and c:%d ,a); } else if (b>a & b>c) { printf(b is greater then a and c:%d ,b); } else printf(c is greater then a and b:%d ,c); } getch(); }
To Find the Greatest among 3 Number Using if else Statement Syntax:- #include<stdio.h> #include<conio.h> void main() { int ch,a,b,c; clrscr(); printf("enter two numbers:"); scanf("%d%d",&b,&c); printf("\n Now enter 1 for Add,2 for Sub, 3 for multiply,4 for division"); scanf("%d",&ch); switch (ch) { case 1: a=b+c; printf("sum of two no is:%d",a); break; case 2: a=b-c; printf("sub of two no is:%d",a); break; case 3: a=b*c; CHANDIGARH UNIVERSITY, GHARUAN 5 Name: Puneet Goyal UID: 13BCS1193 printf("multipication of two no is:%d",a); break; case 4: a=b/c; printf("division of two no is:%d",a); break; default: printf("wrong value"); } getch(); }
To Find Factorial Of A Number #include<stdio.h> #include<conio.h> void main() { int i,f=1,r; clrscr(); printf("Enter a number whose factorial u want to find= "); scanf("%d"&i); for(r=i;r>=1;r--) { f=f*r; } printf("%d",f); getch(); }
Syntax:- #include<stdio.h> #include<math.h> double area_of_triangle(double, double, double); main() { double a, b, c, area; clrscr(); printf("Enter the sides of triangle\n"); scanf("%lf%lf%lf",&a,&b,&c); area = area_of_triangle(a, b, c); printf("Area of triangle = %.2lf\n", area); getch(); } double area_of_triangle( double a, double b, double c ) { double s, area; s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)); return area; } CHANDIGARH UNIVERSITY, GHARUAN 15 Name: Puneet Goyal UID: 13BCS1193
Program-11
Program Of Swapping No. By Call By Value And Call By Reference Syntax:-a) Call By Value #include<stdio.h> #include<conio.h> void swap(int x, int y) { int temp; clrscr(); temp = x; x = y; y = temp; printf("Swapped values are a = %d and b = %d", x, y); } void main() { int a = 7, b = 4; swap(a, b); printf("\nOriginal values are a = %d and b = %d", a, b); printf("\nThe values after swap are a = %d and b = %d", a, b); getch(); CHANDIGARH UNIVERSITY, GHARUAN 16 Name: Puneet Goyal UID: 13BCS1193 }
Syntax:-b)Call By Reference #include<stdio.h> #include<conio.h> void swap(int *x, int *y) { int temp; clrscr(); temp = *x; *x = *y; *y = temp; printf("Swapped values are a = %d and b = %d", *x, *y); } void main() { int a = 7, b = 4; swap(&a, &b); printf("\nOriginal values are a = %d and b = %d",a,b); printf("\nThe values after swap are a = %d and b = %d",a,b); getch(); } CHANDIGARH UNIVERSITY, GHARUAN 17 Name: Puneet Goyal UID: 13BCS1193
Program Of Print Prime No Up To n No. Syntax:- #include<stdio.h> #include<conio.h> void main() { int n,i,j; printf("enter the range:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=2;j<=(n-1);j++) { if(i%j==0) { break; } } if(i==j) { printf("%d\n",i); } } getch(); }
Program Of Area Of Circle By Call By Value And Call By Reference Syntax:-a) Call By Value
#include<stdio.h> #include<conio.h> void area(int); void main() { int r; float a; clrscr(); printf("enter the radius:"); scanf("%d",&r); area(r); getch(); } void area (int x) { float a; a=3.14*x*x; printf("area of the circle is %f",a); }
Syntax:-b)Call By Reference #include<stdio.h> #include<conio.h> void area(int*); CHANDIGARH UNIVERSITY, GHARUAN 21 Name: Puneet Goyal UID: 13BCS1193 void main() { int r; clrscr(); printf("enter the radius:"); scanf("%d",&r); area(&r); getch(); } void area(int *x) { float a; a=(3.14*(*x)*(*x)); printf("area of the circle is %f",a); }
Syntax:-a) By Using Function #include<stdio.h> #include<conio.h> int main() { int n, first=0,second=1,next,c; clrscr(); printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for (c=0;c<n;c++) { if (c<=1) next = c; else { next = first + second; first = second; second = next; } printf("%d\n",next); }
Syntax:-a) By Using Recursion #include<stdio.h> #include<conio.h> int Fibonacci(int); int main() { int n, i=0, c; clrscr(); scanf("%d",&n); printf("Fibonacci series\n"); for ( c=1;c<=n;c++) { printf("%d\n", Fibonacci(i)); i++; } getch(); } int Fibonacci(int n) { if (n==0) return 0; else if (n==1) return 1; CHANDIGARH UNIVERSITY, GHARUAN 24 Name: Puneet Goyal UID: 13BCS1193 else return (Fibonacci(n-1) + Fibonacci(n-2)); }
CHANDIGARH UNIVERSITY, GHARUAN 25 Name: Puneet Goyal UID: 13BCS1193 Program-16 Write a program to display the biggest value in an integer array. Syntax:- #include<stdio.h> #include<conio.h> void main () { int big,a[20],n,i,j; clrscr(); printf("enter the number of elements"); scanf("%d",&n); for(i=1;i<=n;i++) {printf("enter the elements"); scanf("%d",&a[i]);} big=a[1]; for(j=1;j<n;j++) { if(big<a[j]) { big=a[j]; }} printf("\nbiggest element is %d",big); getch(); }
CHANDIGARH UNIVERSITY, GHARUAN 26 Name: Puneet Goyal UID: 13BCS1193 Program-17 Write a program to sort the array using bubble sort. Syntax:- #include<stdio.h> #include<conio.h> void main() { int n,a[20],temp,m,i,l,k; clrscr(); printf("enter nUMBER OF ELEMENTS"); scanf("%d",&n); for(i=1;i<=n;i++) {printf("enter the elements"); scanf("%d",&a[i]); } for(k=1;k<=n-1;k++) { for(l=1;l<=n-k;l++) { if(a[l]>a[l+1]) { temp=a[l]; a[l]=a[l+1]; a[l+1]=temp; }}} for(m=1;m<=n;m++) {printf("%d\t",a[m]);} getch();}
CHANDIGARH UNIVERSITY, GHARUAN 27 Name: Puneet Goyal UID: 13BCS1193 Program-18 Write a program to multiply two matrices using arrays. Syntax:- #include<stdio.h> #include<conio.h> void main() { int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2; clrscr(); printf("Enter number of rows and columns of first matrix \n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of second matrix \n"); scanf("%d%d",&r2,&c2); if(r2==c1) {printf("Enter rows and columns of First matrix \n"); printf("Row wise\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&m1[i][j]);} printf("You have entered the first matrix as follows:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",m1[i][j]); printf("\n");} printf("Enter rows and columns of Second matrix \n"); printf("Again row wise\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); } printf("You have entered the second matrix as follows:\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",m2[i][j]); printf("\n"); }
printf("Now we multiply both the above matrix \n"); printf("The result of the multiplication is as follows:\n");
CHANDIGARH UNIVERSITY, GHARUAN 29 Name: Puneet Goyal UID: 13BCS1193 Program-19 Write a program to concatenate two strings & store the result in third string. Syntax:- #include<conio.h> #include<stdio.h> #include<string.h> void main() { char a[20]; char b[20]; char c[20]; clrscr(); printf("enter the first string\n "); scanf("%s",a); printf("enter the second string\n "); scanf("%s",b); strcpy(c,strcat(a,b)); printf("\n %s",c); getch();}
CHANDIGARH UNIVERSITY, GHARUAN 30 Name: Puneet Goyal UID: 13BCS1193 Program-20 Write a program to check whether the string is palindrome.
a) with using string inbuilt functions b) without using string inbuilt functions.
a)with using string inbuilt functions b)without using string inbuilt functions
Syntax:- Syntax:- #include<conio.h> #include<conio.h> #include<stdio.h> #include<stdio.h> #include<string.h> #include<string.h> void main() void main() { { char a[20]; char a[20]; char b[20]; char b[20]; clrscr(); clrscr(); printf("enter the string\n"); printf("enter the string\n"); scanf("%s",a); gets(a); strcpy(b,a); for(i=0;a[i]='\0'; i++) strrev(a); for(j=0;i=0;i--) printf("reverse of string is=%s",a); { b[j++]=a[i];} if((strcmp(b,a)==0)) b[j]='\0'; {printf("\n string is pallindrome");} for(i=0;a[i]='\0';i++) else { if(a[i]=b[i]) {printf("\n string is not a pallindrome"); } { f=0; break;}} getch();} if(f==0) printf("string is not pallindrome"); else printf("string is pallindrome"); getch(); }
CHANDIGARH UNIVERSITY, GHARUAN 32 Name: Puneet Goyal UID: 13BCS1193 Program-21 Write a program to read & print the record of 15 students using structures . Syntax:- #include<conio.h> #include<stdio.h> struct student { int roll_no; char name [20]; char course [20]; }; void main( ) { struct student stud[15]; int i,j; clrscr(); printf ("Enter the data for all the students:\n"); for (i=0;i<=14;i++) { printf ("Enter the roll number of %d student=",i+1); scanf ("%d",&stud[i].roll_no); printf("Enter the name of %d student=",i+1); scanf ("%s",stud[i].name); printf ("Enter the course of %d student=",i+1); scanf ("%s",stud[i].course); } printf ("The data you have entered is as follows:\n"); for (i=0;i<=14;i++) { printf ("The %d the student's detail is %d %s %s\n",i+1,stud[i].roll_no,stud[i].name,stud[i].course); } getch(); }