diff --git a/1915302.md b/1915302.md new file mode 100644 index 0000000..dabd51f --- /dev/null +++ b/1915302.md @@ -0,0 +1,1399 @@ +![College Logo](https://www.gndec.ac.in/logo.png) + +# **Programming for Problem Solving** +## **Name:- Amanpreet Singh Matharoo** +## **CRN:-1915302** +## **Branch:- CSE-C1** +## **Submitted To:- Ms Goldendeep Kaur** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Amanpreet singh"); +puts("**************"); + +return 0; +} +``` +![1.png](https://i.imgur.com/NWeOz8h.jpg) +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +![2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/2.png) +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +![3.png](https://github.com/devanshdhiman77/PPS/blob/master/images/3.png) +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +![4.png](https://github.com/devanshdhiman77/PPS/blob/master/images/4.png) +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +![5.png](https://github.com/devanshdhiman77/PPS/blob/master/images/5.png) +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +![6.png](https://github.com/devanshdhiman77/PPS/blob/master/images/6.png) +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +![7.png](https://github.com/devanshdhiman77/PPS/blob/master/images/7.png) +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +![8.png](https://github.com/devanshdhiman77/PPS/blob/master/images/8.png) +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +![9.png](https://github.com/devanshdhiman77/PPS/blob/master/images/9.png) +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +![10.png](https://github.com/devanshdhiman77/PPS/blob/master/images/10.png) +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +![11.png](https://github.com/devanshdhiman77/PPS/blob/master/images/11.png) +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +![12.png](https://github.com/devanshdhiman77/PPS/blob/master/images/12.png) +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +![13.png](https://github.com/devanshdhiman77/PPS/blob/master/images/13.png) +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +![14.png](https://github.com/devanshdhiman77/PPS/blob/master/images/14.png) +### 15) To check whether number is palindrome or not. +```C + /* Palindrome of a number */ + +#include +int main() { + + int n, reversedInteger = 0, remainder, originalInteger; + printf("Enter an integer: "); + scanf("%d", &n); + originalInteger = n; + // reversed integer is stored in variable + while( n!=0 ) + { + remainder = n%10; + reversedInteger = reversedInteger*10 + remainder; + n /= 10; + } + // palindrome if orignalInteger and reversedInteger are equal + if (originalInteger == reversedInteger) + printf("%d is a palindrome.", originalInteger); + else + printf("%d is not a palindrome.", originalInteger); + + return 0; +} +``` +![15.png](https://github.com/devanshdhiman77/PPS/blob/master/images/15.png) +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +![16.png](https://github.com/devanshdhiman77/PPS/blob/master/images/16.png) +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` + ![17.png](https://github.com/devanshdhiman77/PPS/blob/master/images/17.png) +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +![18.png](https://github.com/devanshdhiman77/PPS/blob/master/images/18.png) +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +![19_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/19_1.png) +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +![19_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/19_2.png) +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +![20.png](https://github.com/devanshdhiman77/PPS/blob/master/images/20.png) + +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +![21.png](https://github.com/devanshdhiman77/PPS/blob/master/images/21.png) + +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +![22.png](https://github.com/devanshdhiman77/PPS/blob/master/images/22.png) +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +![25.png](https://github.com/devanshdhiman77/PPS/blob/master/images/25.png) +### 26) Programs to swap two numbers using call by value and call by refernce. + +### Call by reference +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +![26_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/26_1.png) +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +![26_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/26_2.png) + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +![27_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/27_1.png) + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +![27_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/27_2.png) + +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +![28_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/28_1.png) +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +![28_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/28_2.png) + +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +![29.png](https://github.com/devanshdhiman77/PPS/blob/master/images/29.png) +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +![30.png](https://github.com/devanshdhiman77/PPS/blob/master/images/30.png) + +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +![31.png](https://github.com/devanshdhiman77/PPS/blob/master/images/31.png) +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} +``` +![34.png](https://github.com/devanshdhiman77/PPS/blob/master/images/34.png) +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +![35.png](https://github.com/devanshdhiman77/PPS/blob/master/images/35.png) diff --git a/1915307.md b/1915307.md new file mode 100644 index 0000000..bf8103c --- /dev/null +++ b/1915307.md @@ -0,0 +1,1399 @@ +![College Logo](https://www.gndec.ac.in/logo.png) + +# **Programming for Problem Solving** +## **Name:- Devansh Kumar** +## **CRN:-1915307** +## **Branch:- CSE-C1** +## **Submitted To:- Ms Goldendeep Kaur Mam** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Devansh Dhiman"); +puts("**************"); + +return 0; +} +``` +![1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/1.png) +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +![2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/2.png) +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +![3.png](https://github.com/devanshdhiman77/PPS/blob/master/images/3.png) +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +![4.png](https://github.com/devanshdhiman77/PPS/blob/master/images/4.png) +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +![5.png](https://github.com/devanshdhiman77/PPS/blob/master/images/5.png) +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +![6.png](https://github.com/devanshdhiman77/PPS/blob/master/images/6.png) +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +![7.png](https://github.com/devanshdhiman77/PPS/blob/master/images/7.png) +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +![8.png](https://github.com/devanshdhiman77/PPS/blob/master/images/8.png) +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +![9.png](https://github.com/devanshdhiman77/PPS/blob/master/images/9.png) +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +![10.png](https://github.com/devanshdhiman77/PPS/blob/master/images/10.png) +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +![11.png](https://github.com/devanshdhiman77/PPS/blob/master/images/11.png) +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +![12.png](https://github.com/devanshdhiman77/PPS/blob/master/images/12.png) +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +![13.png](https://github.com/devanshdhiman77/PPS/blob/master/images/13.png) +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +![14.png](https://github.com/devanshdhiman77/PPS/blob/master/images/14.png) +### 15) To check whether number is palindrome or not. +```C + /* Palindrome of a number */ + +#include +int main() { + + int n, reversedInteger = 0, remainder, originalInteger; + printf("Enter an integer: "); + scanf("%d", &n); + originalInteger = n; + // reversed integer is stored in variable + while( n!=0 ) + { + remainder = n%10; + reversedInteger = reversedInteger*10 + remainder; + n /= 10; + } + // palindrome if orignalInteger and reversedInteger are equal + if (originalInteger == reversedInteger) + printf("%d is a palindrome.", originalInteger); + else + printf("%d is not a palindrome.", originalInteger); + + return 0; +} +``` +![15.png](https://github.com/devanshdhiman77/PPS/blob/master/images/15.png) +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +![16.png](https://github.com/devanshdhiman77/PPS/blob/master/images/16.png) +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` + ![17.png](https://github.com/devanshdhiman77/PPS/blob/master/images/17.png) +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +![18.png](https://github.com/devanshdhiman77/PPS/blob/master/images/18.png) +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +![19_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/19_1.png) +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +![19_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/19_2.png) +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +![20.png](https://github.com/devanshdhiman77/PPS/blob/master/images/20.png) + +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +![21.png](https://github.com/devanshdhiman77/PPS/blob/master/images/21.png) + +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +![22.png](https://github.com/devanshdhiman77/PPS/blob/master/images/22.png) +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +![25.png](https://github.com/devanshdhiman77/PPS/blob/master/images/25.png) +### 26) Programs to swap two numbers using call by value and call by refernce. + +### Call by reference +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +![26_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/26_1.png) +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +![26_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/26_2.png) + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +![27_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/27_1.png) + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +![27_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/27_2.png) + +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +![28_1.png](https://github.com/devanshdhiman77/PPS/blob/master/images/28_1.png) +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +![28_2.png](https://github.com/devanshdhiman77/PPS/blob/master/images/28_2.png) + +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +![29.png](https://github.com/devanshdhiman77/PPS/blob/master/images/29.png) +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +![30.png](https://github.com/devanshdhiman77/PPS/blob/master/images/30.png) + +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +![31.png](https://github.com/devanshdhiman77/PPS/blob/master/images/31.png) +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} +``` +![34.png](https://github.com/devanshdhiman77/PPS/blob/master/images/34.png) +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +![35.png](https://github.com/devanshdhiman77/PPS/blob/master/images/35.png) diff --git a/1915313/image/1.jpg b/1915313/image/1.jpg new file mode 100644 index 0000000..7fa22ff Binary files /dev/null and b/1915313/image/1.jpg differ diff --git a/1915313/image/11.jpg b/1915313/image/11.jpg new file mode 100644 index 0000000..e469e3b Binary files /dev/null and b/1915313/image/11.jpg differ diff --git a/1915313/image/12.jpg b/1915313/image/12.jpg new file mode 100644 index 0000000..8665373 Binary files /dev/null and b/1915313/image/12.jpg differ diff --git a/1915313/image/13.jpg b/1915313/image/13.jpg new file mode 100644 index 0000000..4c0fc3c Binary files /dev/null and b/1915313/image/13.jpg differ diff --git a/1915313/image/14.jpg b/1915313/image/14.jpg new file mode 100644 index 0000000..a57a6fb Binary files /dev/null and b/1915313/image/14.jpg differ diff --git a/1915313/image/15.jpg b/1915313/image/15.jpg new file mode 100644 index 0000000..ab24dd2 Binary files /dev/null and b/1915313/image/15.jpg differ diff --git a/1915313/image/16.jpg b/1915313/image/16.jpg new file mode 100644 index 0000000..051a117 Binary files /dev/null and b/1915313/image/16.jpg differ diff --git a/1915313/image/17.jpg b/1915313/image/17.jpg new file mode 100644 index 0000000..275bab1 Binary files /dev/null and b/1915313/image/17.jpg differ diff --git a/1915313/image/18.jpg b/1915313/image/18.jpg new file mode 100644 index 0000000..a402b98 Binary files /dev/null and b/1915313/image/18.jpg differ diff --git a/1915313/image/19i.jpg b/1915313/image/19i.jpg new file mode 100644 index 0000000..be0808a Binary files /dev/null and b/1915313/image/19i.jpg differ diff --git a/1915313/image/19ii.jpg b/1915313/image/19ii.jpg new file mode 100644 index 0000000..2b0a922 Binary files /dev/null and b/1915313/image/19ii.jpg differ diff --git a/1915313/image/2.jpg b/1915313/image/2.jpg new file mode 100644 index 0000000..807e235 Binary files /dev/null and b/1915313/image/2.jpg differ diff --git a/1915313/image/20.jpg b/1915313/image/20.jpg new file mode 100644 index 0000000..6534218 Binary files /dev/null and b/1915313/image/20.jpg differ diff --git a/1915313/image/21.jpg b/1915313/image/21.jpg new file mode 100644 index 0000000..c4c084f Binary files /dev/null and b/1915313/image/21.jpg differ diff --git a/1915313/image/22.jpg b/1915313/image/22.jpg new file mode 100644 index 0000000..7bdbfa3 Binary files /dev/null and b/1915313/image/22.jpg differ diff --git a/1915313/image/23.jpg b/1915313/image/23.jpg new file mode 100644 index 0000000..4877114 Binary files /dev/null and b/1915313/image/23.jpg differ diff --git a/1915313/image/24.jpg b/1915313/image/24.jpg new file mode 100644 index 0000000..55bfe51 Binary files /dev/null and b/1915313/image/24.jpg differ diff --git a/1915313/image/25.jpg b/1915313/image/25.jpg new file mode 100644 index 0000000..14e72f5 Binary files /dev/null and b/1915313/image/25.jpg differ diff --git a/1915313/image/26i.jpg b/1915313/image/26i.jpg new file mode 100644 index 0000000..59bcaa4 Binary files /dev/null and b/1915313/image/26i.jpg differ diff --git a/1915313/image/26ii.jpg b/1915313/image/26ii.jpg new file mode 100644 index 0000000..a68d201 Binary files /dev/null and b/1915313/image/26ii.jpg differ diff --git a/1915313/image/27i.jpg b/1915313/image/27i.jpg new file mode 100644 index 0000000..6c71639 Binary files /dev/null and b/1915313/image/27i.jpg differ diff --git a/1915313/image/27ii.jpg b/1915313/image/27ii.jpg new file mode 100644 index 0000000..456a033 Binary files /dev/null and b/1915313/image/27ii.jpg differ diff --git a/1915313/image/28.jpg b/1915313/image/28.jpg new file mode 100644 index 0000000..09fbef7 Binary files /dev/null and b/1915313/image/28.jpg differ diff --git a/1915313/image/29.jpg b/1915313/image/29.jpg new file mode 100644 index 0000000..fe1803d Binary files /dev/null and b/1915313/image/29.jpg differ diff --git a/1915313/image/3.jpg b/1915313/image/3.jpg new file mode 100644 index 0000000..c304ee8 Binary files /dev/null and b/1915313/image/3.jpg differ diff --git a/1915313/image/30i.jpg b/1915313/image/30i.jpg new file mode 100644 index 0000000..0aedb89 Binary files /dev/null and b/1915313/image/30i.jpg differ diff --git a/1915313/image/30ii.jpg b/1915313/image/30ii.jpg new file mode 100644 index 0000000..b954226 Binary files /dev/null and b/1915313/image/30ii.jpg differ diff --git a/1915313/image/31.jpg b/1915313/image/31.jpg new file mode 100644 index 0000000..61ae0b0 Binary files /dev/null and b/1915313/image/31.jpg differ diff --git a/1915313/image/32.jpg b/1915313/image/32.jpg new file mode 100644 index 0000000..5771c6c Binary files /dev/null and b/1915313/image/32.jpg differ diff --git a/1915313/image/33.jpg b/1915313/image/33.jpg new file mode 100644 index 0000000..22f6387 Binary files /dev/null and b/1915313/image/33.jpg differ diff --git a/1915313/image/35i.jpg b/1915313/image/35i.jpg new file mode 100644 index 0000000..02cee84 Binary files /dev/null and b/1915313/image/35i.jpg differ diff --git a/1915313/image/35ii.jpg b/1915313/image/35ii.jpg new file mode 100644 index 0000000..3c03c61 Binary files /dev/null and b/1915313/image/35ii.jpg differ diff --git a/1915313/image/4.jpg b/1915313/image/4.jpg new file mode 100644 index 0000000..72ae62e Binary files /dev/null and b/1915313/image/4.jpg differ diff --git a/1915313/image/5.jpg b/1915313/image/5.jpg new file mode 100644 index 0000000..29465f0 Binary files /dev/null and b/1915313/image/5.jpg differ diff --git a/1915313/image/6.jpg b/1915313/image/6.jpg new file mode 100644 index 0000000..59347c2 Binary files /dev/null and b/1915313/image/6.jpg differ diff --git a/1915313/image/7.jpg b/1915313/image/7.jpg new file mode 100644 index 0000000..85e5de5 Binary files /dev/null and b/1915313/image/7.jpg differ diff --git a/1915313/image/8.jpg b/1915313/image/8.jpg new file mode 100644 index 0000000..2ae5251 Binary files /dev/null and b/1915313/image/8.jpg differ diff --git a/1915313/image/9.jpg b/1915313/image/9.jpg new file mode 100644 index 0000000..f116ed2 Binary files /dev/null and b/1915313/image/9.jpg differ diff --git a/1915315.md b/1915315.md new file mode 100644 index 0000000..b70316a --- /dev/null +++ b/1915315.md @@ -0,0 +1,1237 @@ +![](https://ibb.co/W29pwFz) +--- +# Programming for Problem Solving +# Name:- Gurleen Kaur Birdi +# CRN:-1915315 +# Branch:- CSE-C1 +# submitted to :- Goldendeep Kaur Mam +*** +## 1) To print name. + +```C +#include +void main() +{ +puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); +puts("My name is Guleen Kaur Birdi"); +puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); +} +``` +Output :- + +![](/image/1.jpg) +*** +## 2) To print college address. +```C + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +Output :- +![](/image/2.jpg) +*** +## 3) Program to add two numbers. +```C +#include +void main() +{ +int a,b,c; +printf("enter the numbers"); +scanf("%d%d",&a,&b); +c=a+b; +printf("%d=%d+%d \n",c,a,b); +} +``` +Output:- +![](/image/3.jpg) +*** +## 4) Program to find quotient and remainder. +```C +#include +int main() + { +int a, b, quo,rem; +printf("Enter a"); +scanf("%d", &a); +printf("Enter b"); +scanf("%d", &b); +quo = a/ b; +rem = a % b; +printf("Quo = %d\n", quo); +printf("Rem = %d", rem); + return 0; +} +``` +Output:- +![](/image/4.jpg) +*** +## 5) Program to swap two no.s without 3rd variable. +```C +#include +int main() +{ + int a, b; + printf("enter two numbers \n"); + scanf("%d%d", &a, &b); + a = a + b; + b = a - b; + a = a - b; + printf("a = %d\nb = %d\n",a,b); return 0; +} +``` +Output:- +![](/image/5.jpg) +*** +## 6) To check even or odd number. +```C +#include +void main() +{ +int a; +printf("Enter a number:"); +scanf("%d",&a); +if (a%2 ==0) +{ +printf("The number is even"); +} +else +{ +printf("The number is odd"); +} +} +``` +Output:- +![](/image/6.jpg) +*** +## 7) Finding greatest of two numbers. +```C +#include +int main() +{ + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +Output:- +![](/image/7.jpg) + +*** +## 8) To find greatest of 3 numbers. +```C +#include + +void main() +{ + int num1, num2, num3; + + printf("Enter the values of num1, num2 and num3\n"); + scanf("%d %d %d", &num1, &num2, &num3); + printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); + if (num1 > num2) + { + if (num1 > num3) + { + printf("num1 is the greatest among three \n"); + } + else + { + printf("num3 is the greatest among three \n"); + } + } + else if (num2 > num3) + printf("num2 is the greatest among three \n"); + else + printf("num3 is the greatest among three \n"); +} +``` +Output:- +![](/image/8.jpg) + +*** +## 9) Program to grade to student according to percentage. +```C +#include +void main() +{ int marks; + printf("Enter marks:"); +scanf("%d",&marks); + if(marks<=100 && marks>90) +{ printf("Your grade is: A1\n"); +} +else if(marks<=90 && marks>80) + { printf("Your grade is A2\n"); +} +else if(marks<=80 && marks>70) +{ printf("Your grade is B1\n"); +} +else if(marks<=70 && marks>60) +{ printf("Your grade is B2\n"); + } + else + { printf("FAIL\n"); +} +} +``` +Output:- +![](/image/9.jpg) + +*** +## 10) Program to print roots of quadric equation. +```C +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +Output:- +![](/image/10.jpg) + +*** +## 11) Program to check year is leap or not. +```C +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +Output:- +![](/image/11.jpg) + +*** +## 12) To print table of 5. +```C +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +Output:- +![](/image/12.jpg) +*** +## 13) To make simple calculator using switch case. +```C +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +Output:- +![](/image/13.jpg) +*** +## 14) To calculate reverse of a number. +```C +#include +void main() +int i,n,rev=0 +i=0; +{ +printf("Enter a number"); +scanf("%d",&n); +while(i<=n) +{ +rev=rev*10; +rev=rev+n%1; +n=n/10; i++ +} +printf("The number is reversed"); +scanf("%d",&n); + } +``` +![](/image/14.jpg) +*** +## 15) To check whether a no. is palindrome or not. +```C +#include +int main() +{ + int n, rev= 0, rem, a; + printf("Enter an integer: "); + scanf("%d", &n); + a= n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if (a == rev) + printf("%d is a palindrome.", a); + else + printf("%d is not a palindrome.", a); + + return 0; +} +``` +Output:- +![](/image/15.jpg) +*** +## 16) To check whether a number is prime or not. +```C +#include +int main() +{ + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + for(i = 2; i <= n/2; ++i) + { + + if(n%i == 0) + { + flag = 1; + break; + } + } + if (n == 1) + { + printf("1 is neither a prime nor a composite number."); + } + else + { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} +``` +Output:- +![](/image/16.jpg) +*** +## 17) Program to print prime no. to 100. +```C +#include + + int main(){ + +int num,i,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(num=2;num<=100;++num) + + { + + remark=0; + + for(i=2;i<=numbr/2;i++){ + + if((num % i) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",num); + + } + + return 0; + + } +``` +Output:- +![](/image/17.jpg) +*** +## 18) Program to check whether a no. is amstrong or not. +```C +#include +void main() +{ int a,b,rem,x=0; + printf("Enter a number: "); +scanf("%d",&a); + b=a; + while(a!=0) + { +rem=a%10; +x= x+rem*rem*rem; + a=a/10; + } + if(x==b) +printf("The number is armstrong\n"); + else +printf("The number is not armstrong\n"); +} +``` +Output:- +![](/image/18.jpg) +*** +## 19) Print different patterns. + - pattern 1 +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} +``` +Output:- +![](/image/19i.jpg) +- pattern 2 + +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +Output:- +![](/image/19ii.jpg) +*** +## 20) Program to find largest from 1-D array. +```C +#include + +int main() +{ + + int array[50], size, i, largest; + + printf("\n Enter the size of the array: "); + scanf("%d", &size); + + printf("\n Enter %d elements of the array: ", size); + + for (i = 0; i < size; i++) + scanf("%d", &array[i]); + + largest = array[0]; + + for (i = 1; i < size; i++) + { + if (largest < array[i]) + largest = array[i]; + } + + printf("\n largest element present in the given array is : %d", largest); + + return 0; + +} +``` +Output:- +![](/image/20.jpg) +*** +## 21) To find sum of N natural numbers in an array. +```C +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +Output:- +![](/image/21.jpg) +*** +## 22) Program to add two matrices. +```C +#include +int main() +{ +int m,n,c,d,first[10][10],second[10][10], +sum[10][10]; + printf("Enter the number of rows and columns of m$scanf("%d%d", &m, &n); + printf("Enter the elements of first matrix\n"); + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + +printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +Output:- +![](/image/22.jpg) +*** +## 23) Multiplication of matrices. +```C +#include +void main() +{ +int a[3][3],b[3][3],c[3][3],i,j,k; +int sum=0; +printf("Enter the first matrix:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) + { +scanf("%d",&a[i][j]); +} +} +printf("Enter the sceond matrix:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +scanf("%d",&b[i][j]); +} + } +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +sum=0; +for(k=0;k<3;k++) +{ +sum=sum+a[i][j]*b[i][j]; +c[i][j]=sum; +} +} + +} +printf("The multiplication of two matrices is:\n"$ +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{printf("\t%d",c[i][j]); +} +printf("\n"); +} +} +``` +Output:- +![](/image/23.jpg) +*** +## 24) Program to check whether a string is palindrome or not . +```C +#include +#include + +int main(){ + char string1[20]; + int i, length; + int flag = 0; + + printf("Enter a string:"); + scanf("%s", string1); + + length = strlen(string1); + + for(i=0;i < length ;i++){ + if(string1[i] != string1[length-i-1]){ + flag = 1; + break; + } +} + + if (flag) { + printf("%s is not a palindrome", string1); + } + else { + printf("%s is a palindrome", string1); + } + return 0; +} +``` +Output:- +![](/image/24.jpg) +*** +## 25) Program to perform basic operations like lenghth of string ,string concat, string copy ,string compare and string reverse. +```C +#include +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +Output:- +![](/image/25.jpg) +*** +## 26) Programs to swap two numbers using call by value and call by refernce. +- Call by reference +```C +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +Output:- +![](/image/26ii.jpg) +- call by value:- +```C +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +Output:- +![](/image/26i.jpg) +*** +## 27) Program to calculate factorial of a number with and without recursion both. +### with recursion +```C +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +Output:- +![](/image/27ii.jpg) +## without recursion +```C +#include +int main() +{ + int n, i; + long fact=1; + + printf(" Enter any number: "); + scanf("%d", &n); + + for (i=1; i<=n; i++) + fact = fact*i; + printf(" Factorial = %ld", fact); + + return 0; +} +``` +Output:- +![](/image/27i.jpg) +*** +## 28) Program to print fibonacci series with and without recursion both. +### with recursion +```C +#include +void series(int); + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); + printf("\n\n\n"); + + return 0; +} + +void series(int n) + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +Output:- +![](/image/28.jpg) +## without recursion +```C +#include + +int fibo(int n); +int main(){ +int n,i=0,c; +printf("Enter a number: "); + +scanf("%d",&n); +printf("Fibonacci series:\n"); +for(c=1;c<=n;c++) + { + printf("%d\n",fibo(i)); + i=i+1; + } +return 0; + + +} +int fibo(int n){ +if(n==0) +return 0; +else if(n==1) +return 1; +else +return (fibo(n-1)+fibo(n-2)); + +} +``` +Output:- +*** +## 29) Program to calculate average of 5 numbers using function. +```C +#include +int average(); +void main(){ +int avg; +avg=average(); +printf("The average of five numbers is: %d\n",avg$ +} +int average() +{ +int a,b,c,d,e,result; +printf("Enter the five numbers: "); +scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); +result=(a+b+c+d+e)/5; +return result; +} +``` +Output:- +![](/image/29.jpg) +*** +## 30) Program to implement bubble sort. +```C +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +Output:- +![](/image/31.jpg) +*** +## 31) Program to implement binary and linear search. + - binary search:- +```C +#include + +int main() +{ + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +}``` + +- linear search:- +```C +#include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) /* If required element is found */ + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} +``` +Output:- +![](/image/30i.jpg) +![](/image/30ii.jpg) +*** + +## 32) Program to store information of 10 students using array of structures. +```C +#include +struct student{ + char name[50]; + int roll; + float marks; +}; + int main(){ + struct student s[10]; + int i; + printf("Enter information of students:\n"); + for(i=0;i<10;++i) + { + s[i].roll=i+1; + printf("\nFor roll number %d\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying information of students:\n\n"); + for(i=0;i<10;++i) + { + printf("\nInformation for roll number %d:\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + } + return 0; +} +``` +Output:- +![](/image/32.jpg) +*** +## 33) Program for Transpose of a matrix. +```C +#include +int main() +{ +int a[3][3]; +printf("Enter a 3X3 matrix: "); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +scanf("%d",&a[i][j]); +} +} +printf("The matrix you entered is:\n"); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +printf("%d\t",a[i][j]); +} +printf("\n"); +} +printf("The transpose of the matrix is:\n "); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +printf("%d\t",a[j][i]); +} +printf("\n"); +} + +} +``` +Output:- +![](/image/33.jpg) +*** +## 34) Program to print the address of variable using pointer. +```C +#include +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + return 0; +} +``` +Output:- +![](/image/35i.jpg) +*** +## 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +Output:- +![](/image/35ii.jpg) diff --git a/1915316.md b/1915316.md new file mode 100644 index 0000000..df9f07d --- /dev/null +++ b/1915316.md @@ -0,0 +1,1085 @@ +#PPS +##personal details +**Name-Hardev** + +**CRN-1915316** + +**URN-1905790** + +**Branch-CSE-C1** + +###1.To print name using puts + +'#include +int main() { + +puts("~~~~~~~~~~"); +puts(" Hardev "); +puts("~~~~~~~~~~"); +return 0; +}' +![output]() + +###2.to print college address +'#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +}' +![output](https://imgur.com/4CoNJAC) + +###3.program to add two integers +'#include +int main() +{ +int a,b,c; +printf("enter th integers..."); +scanf("%d%d",&a,&b); +c=a+b; +printf("sum is.....%d",c); + +return 0; +}' +![output](https://imgur.com/yezf6ji) + +###4.program to compute quotient and remainder +'#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +}' +![output](https://imgur.com/40kGeuj) + +###5.program to swap two variables without using third variable +'#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +}' +![output](https://imgur.com/pNmXF9y) + +###6.program to check whether a number is even or odd +'#include +void main() +{ +int number; +printf("enter an integer"); +scanf("%d", &number); +if(number % 2==0) + printf("%d is even.",number); + else + printf("%d is odd.",number); +} ' +![output](https://imgur.com/vCcBNQg) + +###7.program to find greatest of two numbers +'#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} ' +![output](https://imgur.com/6cvi8oO) + +###8.program to find greatest of three numbers +'#include +void main() +{ +int a,b,c,big; +printf("enter three numbers"); +scanf("%d %d %d,",&a,&b,&c,big); +if(a>b) +if(a>c) +big=a; +else big=c; +else if(b>c) +big=b; +else big=c; +printf("largest of %d,%d&%d=%d",a,b,c,big); +}' +![output](https://imgur.com/cabQ9Gr) + +###9.program to assign grade to student according to percentage +'#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +}' +![output](https://imgur.com/g5p7mvH) + +###10.program to print roots of quadratic equation +' if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} ' +![output](https://imgur.com/40kGeuj) + +###11.program to check year is leap or not +'#include +int main() { + + int year,temp; + + printf("Enter the year.."); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +}' +![output](https://imgur.com/KPtJdg7) + +###12.program to find table of 5 +'#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +}' +![output](https://imgur.com/Af4g5Wi) + +###13.program too make simple calculator using switch case +' break; + + case '-': + result=num1-num2; + break; + + case '*': + result=num1*num2; + break; + + case '/': + result=(float)num1/(float)num2; + break; + + case '%': + result=num1%num2; + break; + default: + printf("Invalid operation.\n"); + } + + printf("Result: %d %c %d = %f\n",num1,ch,num2,result); + return 0; +} ' +![output](https://imgur.com/h1hNv6J) + +###14.program to clculate reverse of a number +'#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +}' +![output](https://imgur.com/ruQjxJZ) + +###15.program to check whether a number is palindrome or not +'#include +int main() +{ +int n,reversedinteger=0,remainder,originalinteger; +printf("enter an integer:"); +scanf("%d",&n); +originalinteger=n; +while(n!=0) +{ +remainder=n%10; +reversedinteger=reversedinteger*10+remainder; +n/=10; +} +if(originalinteger==reversedinteger) +printf("%d is a palindrome.",originalinteger); +else +printf("%d is not a palindrome.",reversedinteger); +return 0; +} ' +![output](https://imgur.com/eVR0eY1) + +###16.program to check whether a number is prime or not +'#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} ' +![output](https://imgur.com/enJFP9m) + +###17.program to print prime numbers from 1 to 100 +'#include + +int main() +{ + int i, Number, count; + + printf(" Prime Number from 1 to 100 are: \n"); + for(Number = 1; Number <= 100; Number++) + { + count = 0; + for (i = 2; i <= Number/2; i++) + { + if(Number%i == 0) + { + count++; + break; + } + } + if(count == 0 && Number != 1 ) + { + printf(" %d ", Number); + } + } + return 0; +}' +![output](https://imgur.com/YXrgQWk) + +###18.program to check whether a number is armstrong or not +'#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} ' +![output](https://imgur.com/N4npkgj) + +###19.program to print different patterns +####A. +'#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +}' +![output](https://imgur.com/Llii7Vs) +####B. +'#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +}' +![output](https://imgur.com/MCOfStk) +####C. +'#include +int main() +{ + int rows, coef = 1, space, i, j; + printf("Enter number of rows: "); + scanf("%d",&rows); + for(i=0; i +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +}' +![output](https://imgur.com/WmhcSkw) + +###21.program to find the sum of N natural numbers in an array +'#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the no. of natural numbers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d natural numbers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +}' +![output](https://imgur.com/UhHOrdh) + +###22.program to add two matrices +'#include + +int main() +{ + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +}' +![output](https://imgur.com/S8QoZHt) + +###23.program to multiply two matrices +' { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +}' +![output](https://imgur.com/Vkwue8j) +####B.call by reference +'#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +}' +![output](https://imgur.com/YBwljkZ) + +###27.program to find factorial of a number with or without recursion +####A.with recursion +'#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} ' +![output](https://imgur.com/igSfJ6v) +####B.without recursion +'#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +}' +![output](https://imgur.com/H2ISalh) + +###28.program to write fibonacci series with or withou recursion +####A.with recursion +'#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +}' +![output](https://imgur.com/oMD5svL) +####B.without recursion +'#include +int main() +{ +int i, n,first=0,second=1,next; +printf("enter the no. of terms\n"); +scanf("%d",&n); +printf("\n terms of fibonacci series are:"); + +for(i=0;i +int avg(int,int,int,int,int); +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); + printf("Average of the numbers %d",res); + + return 0; + } + int avg(int a1,int a2,int a3,int a4,int a5) + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } ' + ![output](https://imgur.com/bFXZDfj) + +###30.program to implement linear and binary search +####A.linear +'#include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) /* If required element is found */ + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +}' +![output](https://imgur.com/eMKtA70) +####B.binary +'#include + +int main() +{ + int a[10], first,i,last, middle, n, search; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter integers\n", n); + + for(i = 0; i < n; i++) + scanf("%d",&a[i]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while(first <= last) + { + if(a[middle] < search) + first = middle + 1; + + else if(a[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if(first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +}' +![output](https://imgur.com/ugj9hCz) + +###31.program to implement bubble sort +'#include + +int main() +{ + int array[100], n, i, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (i = 0; i < n; i++) + scanf("%d", &array[i]); + + for (i = 0 ; i < n - 1; i++) + { + for (d = 0 ; d < n - i - 1; d++) + { + if (array[d] > array[d+1]) + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (i = 0; i < n; i++) + printf("%d\n", array[i]); + + return 0; +} ' +![output](https://imgur.com/JBLmgLZ) + +###32.program to store iformation of 10 students using array of structures +' for(i=0; i +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +}' +![output](https://imgur.com/FLjIyvb) + +###35.program to access array elements usin pointer +'#include +int main() +{ +int data[5],i; +printf("enter elements:"); +for(i=0;i<5;++i) +scanf("%d",data+i); +printf("you entered:\n"); +for(i=0;i<5;++i) +printf("%d\n",*(data+i)); +return 0; +}' +![output](https://imgur.com/JRtNOcR) +-------*----*----*-----*------*------*------*----- \ No newline at end of file diff --git a/1915320 pps.md b/1915320 pps.md new file mode 100644 index 0000000..693999c --- /dev/null +++ b/1915320 pps.md @@ -0,0 +1,5440 @@ +![](https://jaspreetsarao.files.wordpress.com/2012/05/gne.png) +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105)** +## NAME : GURKIRAT SINGH +### ROLL NO : 1915314 +### BRANCH : COMPUTER SCIENCE +### SECTION : C '1' +____ +### 1. *PROGRAM TO FIND A NUMBER IS EVEN OR ODD* +```C +#include + +int main() +{ + int n; + + printf("Enter an integer\n"); + scanf("%d", &n); + + if (n%2 == 0) + printf("Even\n"); + else + printf("Odd\n"); + + return 0; +} +``` +____ + +#2.PROGRAM OF SWAP TWO NUMBERS WITHOUT USING THIRD VAULE +```C +#include + int main() +{ +int a=10, b=20; +printf("Before swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a= (30-10) +printf("\nAfter swap a=%d b=%d",a,b); +return 0; +} +``` + + + +___ +```C +## 3.To print our name using puts +#include + +int main(){ + + char name[]="Gurkirat Singh"; + + printf("My name is: "); + puts(name); + + return 0; +} +``` +___ + + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + + + + + + diff --git a/1915322.md b/1915322.md new file mode 100644 index 0000000..e040f44 --- /dev/null +++ b/1915322.md @@ -0,0 +1,1403 @@ +![College Logo](https://www.gndec.ac.in/logo.png) + +# **Programming for Problem Solving** +## **Name:- Jahnavi Singh** +## **CRN:-1915322** +## **Branch:- CSE-C1** +## **Submitted To:- Ms Goldendeep Kaur Mam** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Jahnavi Singh"); +puts("**************"); + +return 0; +} +``` +![1.png](https://github.com/1915322/PPS/blob/master/outputs/1.png) +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +![2.png](https://github.com/1915322/PPS/blob/master/outputs/2.png) +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +![3.png](https://github.com/1915322/PPS/blob/master/outputs/3.png) +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +![4.png](https://github.com/1915322/PPS/blob/master/outputs/4.png) +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +![5.png](https://github.com/1915322/PPS/blob/master/outputs/5.png) +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +![6.png](https://github.com/1915322/PPS/blob/master/outputs/6.png) +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +![7.png](https://github.com/1915322/PPS/blob/master/outputs/7.png) +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +![8.png](https://github.com/1915322/PPS/blob/master/outputs/8.png) +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +![9.png](https://github.com/1915322/PPS/blob/master/outputs/9.png) +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +![10.png](https://github.com/1915322/PPS/blob/master/outputs/10.png) +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +![11.png](https://github.com/1915322/PPS/blob/master/outputs/11.png) +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +![12.png](https://github.com/1915322/PPS/blob/master/outputs/12.png) +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +![13.png](https://github.com/1915322/PPS/blob/master/outputs/13.png) +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +![14.png](https://github.com/1915322/PPS/blob/master/outputs/14.png) +### 15) To check whether number is palindrome or not. +```C + /* Palindrome of a number */ + +#include +int main() { + + int n, reversedInteger = 0, remainder, originalInteger; + printf("Enter an integer: "); + scanf("%d", &n); + originalInteger = n; + // reversed integer is stored in variable + while( n!=0 ) + { + remainder = n%10; + reversedInteger = reversedInteger*10 + remainder; + n /= 10; + } + // palindrome if orignalInteger and reversedInteger are equal + if (originalInteger == reversedInteger) + printf("%d is a palindrome.", originalInteger); + else + printf("%d is not a palindrome.", originalInteger); + + return 0; +} +``` +![15.png](https://github.com/1915322/PPS/blob/master/outputs/15.png) +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +![16.png](https://github.com/1915322/PPS/blob/master/outputs/16.png) +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` + ![17.png](https://github.com/1915322/PPS/blob/master/outputs/17.png) +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +![18.png](https://github.com/1915322/PPS/blob/master/outputs/18.png) +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +![19a.png](https://github.com/1915322/PPS/blob/master/outputs/19a.png) +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +![19b.png](https://github.com/1915322/PPS/blob/master/outputs/19b.png) +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +![20.png](https://github.com/1915322/PPS/blob/master/outputs/20.png) + +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +![21.png](https://github.com/1915322/PPS/blob/master/outputs/21.png) + +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +![22.png](https://github.com/1915322/PPS/blob/master/outputs/22.png) +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +![25a.png](https://github.com/1915322/PPS/blob/master/outputs/25a.png) +![25b.png](https://github.com/1915322/PPS/blob/master/outputs/25b.png) +### 26) Programs to swap two numbers using call by value and call by refernce. + +### Call by reference +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +![26.png](https://github.com/1915322/PPS/blob/master/outputs/26.png) +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +![26a.png](https://github.com/1915322/PPS/blob/master/outputs/26a.png) + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +![27.png](https://github.com/1915322/PPS/blob/master/outputs/27.png) + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +![27a.png](https://github.com/1915322/PPS/blob/master/outputs/27a.png) + +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +![28.png](https://github.com/1915322/PPS/blob/master/outputs/28.png) +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +![28a.png](https://github.com/1915322/PPS/blob/master/outputs/28a.png) + +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +![29.png](https://github.com/1915322/PPS/blob/master/outputs/29.png) +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +![30a.png](https://github.com/1915322/PPS/blob/master/outputs/30a.png) +![30b.png](https://github.com/1915322/PPS/blob/master/outputs/30b.png) + +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` + +![31.png](https://github.com/1915322/PPS/blob/master/outputs/31.png) +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +![32.png](https://github.com/1915322/PPS/blob/master/outputs/32.png) +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} +``` +![34.png](https://github.com/1915322/PPS/blob/master/outputs/34.png) +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +![35.png](https://github.com/1915322/PPS/blob/master/outputs/35.png) diff --git a/1915323.md b/1915323.md new file mode 100644 index 0000000..de61c3e --- /dev/null +++ b/1915323.md @@ -0,0 +1,1331 @@ + ![](https://i.imgur.com/2u9mSMe.jpg) + +---- +# ESC-18104/18105 PROGRAMMING FOR PROGRAM SOLVING + +---- +**NAME**-JAPNEET SINGH +**BRANCH**-CSE-C1 +**ROLL NO.**-1915323 + +---- +## 1-To print Name using puts. + +``` +#include + int main() + { + puts("My Name Is Japneet Singh"); + return 0; + } + ``` +**_Output:_** +![](https://i.imgur.com/b4JNnEy.png) + +---- +## 2-To print college address +``` +#include +int main() +{ +printf("Guru Nanak Dev Engineering College,\n"); +printf("Gill Road,\n"); +printf("Ludhiana,Punjab.\n"); +return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/pF0ThtP.png) + +---- +## 3-Program to add two integers +``` +#include +int main() +{ + int firstNumber, secondNumber, sumOfTwoNumbers; + + printf("Enter two integers: "); + scanf("%d %d", &firstNumber, &secondNumber); + sumOfTwoNumbers = firstNumber + secondNumber; + printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers); + return 0; +} +``` +**_Output_:** +![](https://i.imgur.com/VIxYzPh.png) + +---- +## 4- Program To find Quotient and Remainder +``` +#include +int main() +{ + int dividend, divisor, quotient, remainder; + printf("Enter dividend: "); + scanf("%d", ÷nd); + printf("Enter divisor: "); + scanf("%d", &divisor); + quotient = dividend / divisor; + remainder = dividend % divisor; + printf("Quotient = %d\n", quotient); + printf("Remainder = %d", remainder); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/L2dTJCx.png) + +---- +## 5-Program To Swap Two Variables Without Third Variable +``` +#include + int main() +{ +int a=10, b=20; +printf("Before swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a=20 (30-10) +printf("\nAfter swap a=%d b=%d",a,b); +return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/CfnBzO4.png) + +---- +## 6-Program To Check Even Odd Number +``` +#include +int main() +{ + int number; + printf("Enter an integer: "); + scanf("%d", &number); + if(number % 2 == 0) + printf("%d is even.", number); + else + printf("%d is odd.", number); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/7vSR8p8.png) + +---- +## 7-Program to Find Greatest Of Two Numbers +``` +#include +int main() +{ + int a, b, big; + printf("Enter any two number: "); + scanf("%d%d", &a, &b); + if(a>b) + big=a; + else + big=b; + printf("\nBiggest of the two number is: %d", big); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/e3fifMI.png) + +---- +## 8-Program to Find Greatest Of Three Numbers +``` +#include + +void main() +{ + int num1, num2, num3; + + printf("Enter the values of num1, num2 and num3\n"); + scanf("%d %d %d", &num1, &num2, &num3); + printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); + if (num1 > num2) + { + if (num1 > num3) + { + printf("num1 is the greatest among three \n"); + } + else + { + printf("num3 is the greatest among three \n"); + } + } + else if (num2 > num3) + printf("num2 is the greatest among three \n"); + else + printf("num3 is the greatest among three \n"); +} +``` +**_Output:_** +![](https://i.imgur.com/d6DTT6a.png) + +---- +## 9-Program to Print Grade Of Students According to Marks Entered +``` +#include +void main() +{ + int marks; + printf("Enter your marks "); + scanf("%d",&marks); + if(marks<0 || marks>100) + { + printf("Wrong Entry"); + } + else if(marks<50) + { + printf("Grade F"); + } + else if(marks>=50 && marks<60) + { + printf("Grade D"); + } + else if(marks>=60 && marks<70) + { + printf("Grade C"); + } + else if(marks>=70 && marks<80) + { + printf("Grade B"); + } + else if(marks>=80 && marks<90) + { + printf("Grade A"); + } + else + { + printf("Grade A+"); + } +} +``` +**_Output:_** +![](https://i.imgur.com/zQJULx7.png) + +---- +## 10-Program to Find The Roots Of Quadratic Equation +``` +#include +#include + +int main() +{ + int a, b, c, d; + double root1, root2; + + printf("Enter a, b and c where a*x*x + b*x + c = 0\n"); + scanf("%d%d%d", &a, &b, &c); + + d = b*b - 4*a*c; + + if (d < 0) + { //complex roots + printf("First root = %.2lf + i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a)); + printf("Second root = %.2lf - i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a)); + } + else + { //real roots + root1 = (-b + sqrt(d))/(2*a); + root2 = (-b - sqrt(d))/(2*a); + + printf("First root = %.2lf\n", root1); + printf("Second root = %.2lf\n", root2); + } + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/id6x1Xo.png) + +---- +## 11-Program To Check The Year Is Leap Or Not +``` +#include +int main() +{ + int year; + printf("Enter a year: "); + scanf("%d",&year); + if(year%4 == 0) + { + if( year%100 == 0) + { + // year is divisible by 400, hence the year is a leap year + if ( year%400 == 0) + printf("%d is a leap year.", year); + else + printf("%d is not a leap year.", year); + } + else + printf("%d is a leap year.", year ); + } + else + printf("%d is not a leap year.", year); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/Ym34Ap3.png) + +---- +## 12- Program To Print Multiplication Table of 5 +``` #include + +int main() +{ + int num=5, i = 1; + + printf("Multiplication table of 5:\n "); + + while (i <= 10) +{ + printf(" %d x %d = %d", num, i, num * i); + i++; + } + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/hDqF4Uk.png) + +---- +## 13-Program To Make Simple Calculator Using Switch Case +``` +# include +int main() +{ + char operator; + double firstNumber,secondNumber; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf",&firstNumber, &secondNumber); + switch(operator) + { + case '+': + printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber); + break; + // operator doesn't match any case constant (+, -, *, /) + default: + printf("Error! operator is not correct"); + } + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/laVVSJH.png) + +---- +## 14-Program To Calculate Reverse Of A Number + +``` +#include + +int main() +{ + int n, reverse = 0; + + printf("Enter a number to reverse\n"); + scanf("%d", &n); + + while (n != 0) + { + reverse = reverse * 10; + reverse = reverse + n%10; + n = n/10; + } + + printf("Reverse of entered number is = %d\n", reverse); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/2O0J0OB.png) + +---- +## 15-Program To Check Whether A Number Is Palindrome Or Not +``` +#include +int main() +{ + int n, reversedInteger = 0, remainder, originalInteger; + printf("Enter an integer: "); + scanf("%d", &n); + originalInteger=n; + while( n!=0 ) + { + remainder = n%10; + reversedInteger = reversedInteger*10 + remainder; + n /= 10; + } + if (originalInteger == reversedInteger) + printf("%d is a palindrome.", originalInteger); + else + printf("%d is not a palindrome.", originalInteger); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/SyCPDkt.png) + +---- +## 16-Program To Check Whether A Number Is Prime Or Not +``` +#include +int main() +{ + int i, num, p=0; + printf("Enter a number"); + scanf("%d", &num); + for(i=1; i<=num; i++) + { + if(num%i==0) + { + p++; + } +} +if(p==2) +{ + printf("Entered number is %d" \ + " and it is a prime number.",num); +} +else +{ + printf("Entered Number is %d" \ + "and it is not a prime number.",num); + } +} +``` +**_Output:_** +![](https://i.imgur.com/MBJYjY6.png) + +---- +## 17-Program To Print Prime Numbers Between 1 To 100 Using For Loop +``` +#include +int main() +{ + int i,a,count; + printf("Prime numbers between 0 and 100 are : \n"); + for (i=0;i<100;i++) + { + count=0; + for (a=1;a<=i;a++) + { + if (i%a==0) + count++; + } + if (count==2) + printf("%d\t",i); + } + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/1yH6F1F.png) + +---- +## 18- Program To Check Whether A Number Is Amstrong Or Not +``` +#include + int main() +{ +int n,r,sum=0,temp; +printf("enter any number"); +scanf("%d",&n); +temp=n; +if(n>0) +{ + r=n%10; +sum=sum+(r*r*r); +n=n/10; +printf("It is a armstrong number\n"); +} + else + printf("It is not armstrong number\n"); +return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/JaT9Xol.png) + +---- +## 19-Print Different Patterns:- + + +### a) Pattern 1- +``` +#include +int main() +{ + int i, j, rows; + printf("Enter Number of rows"); + scanf("%d" , &rows); + for(i=1; i<=rows; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d",j); + } + printf("\n"); + } + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/zxe5IRv.png) + +---- +### b) Pattern 2- +``` +#include +int main() +{ + int n, i, c, a = 1; + + printf("Enter The Number Of Rows "); + scanf("%d" , &n); + + for (i = 1; i<=n; i++) + { + for(c = 1; c<=i; c++) + { + printf("%d",a); + a++; + } + printf("\n"); + } + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/gkMXLAW.png) + +---- +### c)Pattern 3- +``` +#include +int main() +{ + int i, space, rows, k=0, count = 0 , count1 = 0; + printf("Enter 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"); + } + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/vMPOUhX.png) + +---- +## 20- Program To Find Largest From 1 Dimensional Array +``` +#include +int main() +{ + int i, n; + float arr[100]; + printf("Enter total number of elements of array: "); + scanf("%d", &n); + printf("\n"); + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + for(i = 1; i < n; ++i) + { + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/Ainy64n.png) + +---- +## 21-Program To Find The Sum of N Natural Numbers Of An Array +``` +#include +int main() +{ + int n, sum = 0, c, array[100]; + + printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d natural numbers \n\n", n); + + for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; + } + + printf("\n\nSum of the natural numbers = %d\n\n", sum); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/M0MNM9E.png) + +---- +## 22-Program To Add Two Matrices +``` +#include + int main() +{ + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/w3MfhyJ.png) + +---- +## 23-Program To Multiply To Two Matrices +``` +#include + int main() +{ + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + else + { + printf("Enter elements of second matrix\n"); + + for (c = 0; c < p; c++) + for (d = 0; d < q; d++) + scanf("%d", &second[c][d]); + + for (c = 0; c < m; c++) { + for (d = 0; d < q; d++) { + for (k = 0; k < p; k++) { + sum = sum + first[c][k]*second[k][d]; + } + + multiply[c][d] = sum; + sum = 0; + } + } + + printf("Product of the matrices:\n"); + + for (c = 0; c < m; c++) { + for (d = 0; d < q; d++) + printf("%d\t", multiply[c][d]); + + printf("\n"); + } + } + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/StvDMep.png) + +---- +## 24-Program To Check Whether A String Is Palindrome Or Not +``` +#include +#include +int main() +{ + char text[20], reverse_text[20]; + int i,n, length = 0; + + printf("Enter text: "); + gets(text); + + for (i = 0; text[i] != '\0'; i++) + { + length++; //this will calculate the length of given text + } + for (i = length - 1; i >= 0; i--) + { + reverse_text[length - i - 1] = text[i]; + } + for (n = 1, i = 0; i < length; i++) + { + if (reverse_text[i] != text[i]) + n = 0; + } + + if (n == 1) + printf("%s is a palindrome.", text); + else + printf("%s is not a palindrome", text); + + return 0; + } +``` +**_Output:_** +![](https://i.imgur.com/I0VBFL0.png) + +---- +## 25- Program To Perform Basic Operations Like Length Of String, String Concat,, String copy, String Compare and String Reverse +``` +#include +#include + +int find_length(char string[]) +{ + int len = 0, i; + for(i = 0; string[i] != '\0' ; i++) { + len ++; +} + return len; +} + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 +len2; i++) { + string1[i] = string2[i - len1]; +} +string1[i] = '\0'; +} + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for(i = 0; i< len1;i++) { + if(string1[i] == string2[i]) + count++; +} +if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; +} + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; + int choice; +while (1) +{ + printf("\n1. Find The Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your Choice"); + scanf("%d", & choice ); + switch (choice) +{ + case 1: + printf("Enter the string"); + scanf("%s" , string1); + printf("The Length of String is %d", find_length(string1)); + break; + case 2: + printf("Enter Two Strings"); + scanf("%s%s", string1 , string2); + join_strings(string1, string2); + printf("The Concatenated String Is %s", string1); + break; + case 3: + printf("Enter two Strings"); + scanf("%s%s" , string1, string2); + if (compare_strings(string1, string2) == 0) + printf("They Are Equal"); + else + printf("They are not equal"); + break; + case 4: + printf("Enter a string"); + scanf("%s", string1); + printf("string1 = %s\n"); + printf("After Copying strting1 to string2\n"); + copy_string(string2, string1); + printf("string2 = %s" , string2); + break; + case 5: + exit(0); + } + } +return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/kj6Owoj.png) + +---- +## 26-Programs To Swap Two Numbers By:- + +---- +### a) Call By Value +``` +#include + +void swap(int,int); + +void main( ) +{ + int n1,n2; + printf("Enter the two numbers to be swapped\n"); + scanf("%d%d",&n1,&n2); + printf("\n The Values Of n1 and n2 Before Swapping: n1=%d n2=%d",n1,n2); + swap(n1,n2); + printf("\n The Values Of n1 and n2 After Swapping: n1=%d n2=%d",n1,n2);} + +void swap(int n1,int n2) +{ + int temp; + temp=n1; + n1=n2; + n2=temp; + printf("\n After Swapping n1=%d n2=%d",n1,n2); +} +``` +**_Output:_** +![](https://i.imgur.com/A7jL0BT.png) + +---- +### b) Call By Reference +``` +#include + +void swap(int*, int*); + +int main() +{ + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +**_Output:_** +![](https://i.imgur.com/YZ91gVX.png) + +---- +## 27-Program To Calculate Factorial Of A Number:- + +---- +### a) With Recursion +``` +#include +long int multiplyNumbers(int n); +int main() +{ + int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +**_Output:_** +![](https://i.imgur.com/NelRCE9.png) +### b) Without Recursion +``` +#include +#include + +int main() +{ + int n, i; + unsigned long long factorial = 1; + + printf("Enter a number to find factorial: "); + scanf("%d",&n); + + if (n < 0) + printf("Error! Please enter any positive integer number"); + + else + { + for(i=1; i<=n; ++i) + { + factorial *= i; + } + printf("Factorial of Number %d = %llu", n, factorial); + } +} +``` +**_Output:_** +![](https://i.imgur.com/7LzzHO6.png) + +---- +## 28- Program To Print Fibonacci series:- + +---- +### a) With Recursion +``` +#include + +int f(int); + +int main() +{ + int n, i = 0, c; + + scanf("%d", &n); + + printf("Fibonacci series terms are:\n"); + + for (c = 1; c <= n; c++) + { + printf("%d\n", f(i)); + i++; + } + + return 0; +} + +int f(int n) +{ + if (n == 0 || n == 1) + return n; + else + return (f(n-1) + f(n-2)); +} +``` +**_Output:_** +![](https://i.imgur.com/dVQhRRA.png) + +---- +### b) Without Recursion +``` +#include +int main() +{ + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("\n%d %d",n1,n2); + for(i=2;i +float average(int a, int b, int c, int d, int e) { + return (float)(a+b+c+d+e)/5; +} +int main() +{ + int num1, num2, num3, num4, num5; + float avg; + + printf("Enter first number:"); + scanf("%d",&num1); + printf("Enter second number:"); + scanf("%d",&num2); + printf("Enter third number:"); + scanf("%d",&num3); + printf("Enter fourth number:"); + scanf("%d",&num4); + printf("Enter fifth number:"); + scanf("%d",&num5); + + avg = average(num1, num2, num3, num4, num5); + + + printf("Average of five numbers is %.2f\n",avg); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/B82CzWj.png) + +---- +## 30-Programs To Implement:- + +---- +### a) Linear Search + +``` +#include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/z8RU6gS.png) + +---- +### b) Binary Search +``` +#include + +int main() +{ + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/4flmm68.png) + +---- +## 31-Program To Implement Bubble Sort +``` +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/brKECE5.png) + +---- +## 32-Program For Students Information Using Array Structure +``` +#include +struct student +{ + char name[50]; + int roll; + float marks; +} s[10]; +int main() +{ + int i; + printf("Enter information of students:\n"); + // storing information + for(i=0; i<10; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying Information:\n\n"); + // displaying information + for(i=0; i<10; ++i) + { + printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } + return 0; +} + ``` +**_Output:_** +![](https://i.imgur.com/1k4tqSX.png) + +---- +## 33- Program To Compute Transpose Of A Matrix + +``` +#include + +int main() +{ + int m, n, c, d, matrix[10][10], transpose[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of the matrix\n"); + + for (c = 0; c < m; c++) + for(d = 0; d < n; d++) + scanf("%d", &matrix[c][d]); + + for (c = 0; c < m; c++) + for( d = 0 ; d < n ; d++ ) + transpose[d][c] = matrix[c][d]; + + printf("Transpose of the matrix:\n"); + + for (c = 0; c < n; c++) { + for (d = 0; d < m; d++) + printf("%d\t", transpose[c][d]); + printf("\n"); + } + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/TUouKFr.png) + +---- +## 34-Program To Print Address Using Pointer + +``` +#include +int main(void) +{ + int a; + float b; + char c; + + int *ptr_a = &a; + float *ptr_b = &b; + char *ptr_c = &c; + + printf("Address of a: %p\n", ptr_a); + printf("Address of b: %p\n", ptr_b); + printf("Address of c: %p\n", ptr_c); + + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/CcofQyj.png) + +---- +## 35- Program To Access Array Using Pointer +``` +#include +int main() +{ + int data[5], i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +**_Output:_** +![](https://i.imgur.com/Hi334C1.png) + +---- + + + + + + + + + + + + + + + + + + + + + + diff --git a/1915326.md b/1915326.md new file mode 100644 index 0000000..3f3d31c --- /dev/null +++ b/1915326.md @@ -0,0 +1,1451 @@ +![](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQjw1RzMBU-HK5zPmWDnq9Npi_Pbk10fgq_AI7U6LvKOcy9PW5J) +--- + +| Column 1 | Column 2 | Column 3 | +| -------- | -------- | -------- | +| Text | Text | Text | + + +| Column 1 | Column 2 | Column 3 | +| -------- | -------- | -------- | +| Text | Text | Text | + + +--- +# **Programming for Problem Solving** +## **Name** - KOMAL KUMARI +## **CRN -1915326** +## **Branch - CSE-C1** +## **Submitted to:- Ms Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("KOMAL KUMARI"); +puts("**************"); + +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/3c7BpLId1kQibDk8ZIJmkC8zJNpMlHW0-P1LL-aRC91I9U48IUZeaKa6AEHqRTJsEFIzhn7fpFgvZTwSWdEITAKMHCKNG1dy06wUaqFPwAGIVFRt1GaczL3QcfbRDj7uYrLpi4CXpYizBZEtcqBhpmbKQSOC_UPhma45ScwU8OIOsrPJvYyf2-2IFm_D66OIoqCIBlj7yl24ALAvebwpy_lhT1Qf3kiVyMqkFWsmp0SbmQbYv2uCd7i3-UJPUcniEAJ0AA5Ymp-WrqdPzfcDNMIxAQzv_jL6k9GqIHBS2muXkGPvtky59W1h7oWTQa30L70XoqtDeJr7x895Sg4YoF5h02oA4qflCpYkCKKBjaGvi2tfc9aIepTKd4lh6TFdvISySkoiAgefoZHQqeaMp3xgRXXwO4hrllVVh6WJo6ifXKTgFgbruZXEAjEHMhkmOLp0GDDPzcVImvBz1KnypSJ5ujdsABwGWuvMZGMJvYQab5iSF3ohjQGWwuNOfofsXM_jq-tyd4Dz3HsS-i_pJZVxABvs_IKU76K9vnfPDDLo3VC5_reacRT2zuXm80LpUe9dYgoiTgaSmMZ28UrQgrvl1uRKHIhIb7UQniVxuBYy8U5RwYyorIpvDLmeFxsVZ_IMSz2iRRp1ozV38YURdpcOq_dUiDrrZaYLJo5OVLERXTRedC0opGc=w1082-h608-no) +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/NDKK92tKV4BikhQB1YzXcqMC-yMO9dDUtC4wKgK6Z22lzn8es_YZtkIdGeBwmiGWkEFHFEEhLS6VvoaMeM0zy4J8SszaNnoQJprrEhszboUtoMZ2vsNWKpqCzCeJ-DqNVUQfqlO0fcovdeW7BRWwyfWUau3btvIPyoXhgTDYIzfFBv3Ln5VmElits42nCeEPwT03eoes58V1KGVIlhD720esGk194RajqFAV3n7uPEW6bfO8v65xPaS8lZNfTPiVp1jlRF0A32ZgJM4qL5s9jYNxi9JXljkkR3Ghj5n4tl8AYMYHfmzcaJBrba8hgb5K4NfrKga1cLn2ULxFo9G3130MlxnUhZoOFQLMB15LyMXPkru0p698KIdbVot01aqw4tdYZWK1GpqKIB04W6cnhvNRSs8hIejaLzAw-zYBLfiQXoJ7essnw7VoLbzgum7pzwcFamVVjYG8ZnLq3Ol6yeZSdslyTMQhWRfRc69PkBJUpZJYHhA9BQcrdtRXcj8Gg_hEqkzbONyewpnyVxvOdcrfh5KIsFG7T3br3Plk7IhN_3QaONsuFM7af__SHgzrYyFynaUPp4RHxcDrMnb_SiV6D_IpHW5k4_s47k4b8gAKp01eet0mnYgFKZcY_wgWmqfcLfPg8zX7VP3svrvAp763mp2XIaQML5RxKlA_0R_ee_r7Xne5BC8=w1169-h657-no) +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/p_0gJXTXHqjdeoN-1cSfHqEVnt6ElMcyPWvSQy_3lbnYu7xYko2C_ARTWLV5ms8vmtZnS4kSs2ilFOViI9N0SIfpuH1y0ylJQTfHoW8Oe2c7FikGsY68LudVKpDU1lwfz4dYWDawwCQassSl9EsqjiOmc9_dehnYREJrLSl7_VZZeJxg_2payua8Ui10YLLQTTrHkD8nbUqbxYJDhRNXyNFxiKkoOTNvcdPCSZDyPTbPHO1LVbndJ6auNWpXtkPfZA36t1xlOx2l4t5HvlWKYwmca3wtvk9YS0jy_O3q7o8COMAyLufAXBEzxnY0NSJ8tNtzDSIW6u-5-1tY4HkMM07m45Z8-X0klDQ8QJMUAA0nFrcYAiXhYYphyNeHqphpRd0UZhc_HYC_ymt0GL2HxA1vMN-mOci3RSvGA73dtB3fe0LnqGH0XoOlTDGYYxWxCZMSARwOPY0gYtTcLmMagJKzzqUplDELUFvHWSqd8CPJsx-5ZUpba22Mt_JKS3WKAQ5vXiL4bg8PCOHmXa9ygyszGZsEJwDT3Mh2_29mEIYEWjPLBNBj1dJb5BRVTI7RiLQD3AfRXfK9jNxuuAPV6oid5fGP2xEGFZEvmSjqdZab4u-TVy9lBA50tWEGeNkLe-LyDLOBXw-dTZ8S2Fj2nlN13eEkYdJmZfRCnCgOWxHFjzileoqF6SA=w1169-h657-no) + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/p_0gJXTXHqjdeoN-1cSfHqEVnt6ElMcyPWvSQy_3lbnYu7xYko2C_ARTWLV5ms8vmtZnS4kSs2ilFOViI9N0SIfpuH1y0ylJQTfHoW8Oe2c7FikGsY68LudVKpDU1lwfz4dYWDawwCQassSl9EsqjiOmc9_dehnYREJrLSl7_VZZeJxg_2payua8Ui10YLLQTTrHkD8nbUqbxYJDhRNXyNFxiKkoOTNvcdPCSZDyPTbPHO1LVbndJ6auNWpXtkPfZA36t1xlOx2l4t5HvlWKYwmca3wtvk9YS0jy_O3q7o8COMAyLufAXBEzxnY0NSJ8tNtzDSIW6u-5-1tY4HkMM07m45Z8-X0klDQ8QJMUAA0nFrcYAiXhYYphyNeHqphpRd0UZhc_HYC_ymt0GL2HxA1vMN-mOci3RSvGA73dtB3fe0LnqGH0XoOlTDGYYxWxCZMSARwOPY0gYtTcLmMagJKzzqUplDELUFvHWSqd8CPJsx-5ZUpba22Mt_JKS3WKAQ5vXiL4bg8PCOHmXa9ygyszGZsEJwDT3Mh2_29mEIYEWjPLBNBj1dJb5BRVTI7RiLQD3AfRXfK9jNxuuAPV6oid5fGP2xEGFZEvmSjqdZab4u-TVy9lBA50tWEGeNkLe-LyDLOBXw-dTZ8S2Fj2nlN13eEkYdJmZfRCnCgOWxHFjzileoqF6SA=w1169-h657-no) +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/OBYjm4u6MEvZ85dfrvTv8xioE0A4ty6gAYXHf9LzC61HKBdn6k7BCKG0bsLAhSgA5hag8klSQpLSDnijzNg_FDrmmKZG29AWwlrrQoefka-o1uo2j-E4A0rpaX2wsLUFpGcQKvNgvo8GGte9wtkJ7WJ1FUmxThq1-KoP5R1LcmQAnVR72XqVnMM1LKn9xyEB8JXEJIfmqGhlMVKp0hkYqIFc7VrQ7TKGsIrWMkQTZ32RfazsIlcFSHP0RqLMA5ldWGUSFGmSlw4ASJjVA6oR08aL2IkKJQuIhoQTAfjBHCh3tR647T-I8_O4yd87nTwEbv2vssFUPc6DVwO3qlHtoWFam7yQd4eCJhy3VrBzhiewDS0cWj5ysS9ozDVL1qMtUen6sPEHSpAB7CvLaf8RNnctL9s0Ox4-Nok-buFb7AqBEuqTYlpzOuC1a0mBwFuQHjtlb-Y7hwXL41P4O1crW2WvH2txmOrdvCfq0rKMXhKi-ufAIXqE5t6w5R4KOpJ9Uib7BOZOcDC4oZiJ7dqzs8ZaKYFZNpV7WKPG7x1ztQpxyy3q6dIo5aDGjo4VpmsYVfVN3_XignKW1cHmKmrSFwaZUFupaanfUIvhzK-hVXhPvBPpxq072ATpMVvHu0iPqb1-V1GgEZ7EhVuYChLNtYyOtaNzzlM_ouCDL5wZkAD2O5kxenjrW3E=w1169-h657-no) +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/pOVAyP5YdBJy4Qrlo-NqKjTnBm8JZljDe1eE5ieusTAckFpu1RRCi6MZuU4B8s4GyMvl_Pb0QWV8khfcdNjnug11HuHgsCQounoW5D-kx_H-33s0rWcrBUtBBNwLixKA2B8Wu-AKr_PBTtPiBucn_a_8lM-Y0x7ZYjNRR8cXcWDb3Fcbp1W071IdPTd1aJ7k1hGhGKUCDkfg3P0eIEu4BVmldnGmR7pOXY0MZ1ilXXrsLXprIgN5MoqO0YtvKi-Brd9PxfXcnZD6Lmw3-f_jcVJb_p_fDQXgsLFJUu7-jAf5LwO8OkTCL3OFI-h_KlHodlduOuXI08vztfh5mhjU9gQSxH_F-ihH4R4uUpM_sXn1Vinmw5DKWiq78leLYzvGxlSElJrgw9JYhtdYs_NzrTFumosQxMg-slyDG3tPx7Cyaw_Gh1s2wIBdvnEUKRzikBcZY99qOW0teM9w-ZkeOlW41Wzx78MDiv6_I42m8GONTCMYQhqpNUxAUjAtECJS-OgQ3qIXpNpz13HiUganD0fUZ75skwwYmFnVvmLS5AKCXdwAMhcjjH_QW6i-4JhJG8Af5TPf1shYTrwnDopkwzh8WIQR10wsiZ0-NikX9_dOrMmvHv6E1B83oSSglVXMAOpjupJJ1b_f7bLXUbA1hgXcyP2Kes3eO9_05tWbqjWomhmBvSgImaQ=w1169-h657-no) +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/kPmvXlkvIW0niwZNTup7eGTvOcPeMeKYRAITdzsVbTWVUS5TYukBpHC2SruI7qDZYvElMNb7ZFrev93fOwSndGLKoKR8cSszWY2iCJxEVgWlOLducmOEqW-JItSPUb5okqljYK5IsHaZ5bD-SgwuETk7SEqO0bO8VBmpbhaMqOrFyjUYVj4xaBlWYNmXyzFnRbS8xC-L4u7g24-qm_czv_3TFs7Gc68DXewJnR-qeuY2CSuQOfZje-UbOu7g9N9LKHu_JniKYusN4yi5oW3gy5iTVwRStP0-ZDcWVdlWJWJcxvlzGLAVNFbRG8YQ9xrgWT-POO58bI7rMMAz34Kmy4gPQD_R45kBajiP-9YEyUqIuBYDNyOaMNYS4j8_VHoRhez-8fq9TxQkwFJX1dQGsZtqdKgo8fsw-jGFsYHJ2Y8dZ4P6Y49chLgDz5ZFERCp2VfSuPFwlEkqv8E1HJ5bz41rkApfih6MgYPskhraM8UYd4WWAcjLysVFs7CK2IvW_DPcpEOAjqMU-r7tMHSZupw5VTZfmeGJzVGiN0rpMcoqyLD7RV5fi0-XqwrhWuhQnXbUC7C4ZscDyfflIQjI1IC7RXjHI_Aeqt_5LqZsNS43nH7XKcoTg7Iw4XY_kfKb7z3525UlI3y02fHnU4dEFpy8o5r_2SJErgpm74loPyNA34_KDHRHQYM=w1169-h657-no) +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/kPmvXlkvIW0niwZNTup7eGTvOcPeMeKYRAITdzsVbTWVUS5TYukBpHC2SruI7qDZYvElMNb7ZFrev93fOwSndGLKoKR8cSszWY2iCJxEVgWlOLducmOEqW-JItSPUb5okqljYK5IsHaZ5bD-SgwuETk7SEqO0bO8VBmpbhaMqOrFyjUYVj4xaBlWYNmXyzFnRbS8xC-L4u7g24-qm_czv_3TFs7Gc68DXewJnR-qeuY2CSuQOfZje-UbOu7g9N9LKHu_JniKYusN4yi5oW3gy5iTVwRStP0-ZDcWVdlWJWJcxvlzGLAVNFbRG8YQ9xrgWT-POO58bI7rMMAz34Kmy4gPQD_R45kBajiP-9YEyUqIuBYDNyOaMNYS4j8_VHoRhez-8fq9TxQkwFJX1dQGsZtqdKgo8fsw-jGFsYHJ2Y8dZ4P6Y49chLgDz5ZFERCp2VfSuPFwlEkqv8E1HJ5bz41rkApfih6MgYPskhraM8UYd4WWAcjLysVFs7CK2IvW_DPcpEOAjqMU-r7tMHSZupw5VTZfmeGJzVGiN0rpMcoqyLD7RV5fi0-XqwrhWuhQnXbUC7C4ZscDyfflIQjI1IC7RXjHI_Aeqt_5LqZsNS43nH7XKcoTg7Iw4XY_kfKb7z3525UlI3y02fHnU4dEFpy8o5r_2SJErgpm74loPyNA34_KDHRHQYM=w1169-h657-no) +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/kPmvXlkvIW0niwZNTup7eGTvOcPeMeKYRAITdzsVbTWVUS5TYukBpHC2SruI7qDZYvElMNb7ZFrev93fOwSndGLKoKR8cSszWY2iCJxEVgWlOLducmOEqW-JItSPUb5okqljYK5IsHaZ5bD-SgwuETk7SEqO0bO8VBmpbhaMqOrFyjUYVj4xaBlWYNmXyzFnRbS8xC-L4u7g24-qm_czv_3TFs7Gc68DXewJnR-qeuY2CSuQOfZje-UbOu7g9N9LKHu_JniKYusN4yi5oW3gy5iTVwRStP0-ZDcWVdlWJWJcxvlzGLAVNFbRG8YQ9xrgWT-POO58bI7rMMAz34Kmy4gPQD_R45kBajiP-9YEyUqIuBYDNyOaMNYS4j8_VHoRhez-8fq9TxQkwFJX1dQGsZtqdKgo8fsw-jGFsYHJ2Y8dZ4P6Y49chLgDz5ZFERCp2VfSuPFwlEkqv8E1HJ5bz41rkApfih6MgYPskhraM8UYd4WWAcjLysVFs7CK2IvW_DPcpEOAjqMU-r7tMHSZupw5VTZfmeGJzVGiN0rpMcoqyLD7RV5fi0-XqwrhWuhQnXbUC7C4ZscDyfflIQjI1IC7RXjHI_Aeqt_5LqZsNS43nH7XKcoTg7Iw4XY_kfKb7z3525UlI3y02fHnU4dEFpy8o5r_2SJErgpm74loPyNA34_KDHRHQYM=w1169-h657-no) +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/jk7cC_H8t4jJobvUgHmxUkXY1C1wQEuIj3CyzYojLnAwOUE1Dia6fktP2doTbDMRcNwH3Z_3p5-J-l2sKwtA1iVinPGapx0GxLSx5UB_WYDYmoDXpN19nTmdCcCvFKkWpYYLjgjCmEjseIalytfhBFNDrVF96VIDekDKDacinrWKj7W9GR-bydvNAsukyrVvmI9iTDfhM62RN8gNikrvj_DCA4jIGaxB1Xhnc0LIvoflnqZeWM6t5CxvGSa0OzQalEUWtpl-k6jf9jAZL921WEJJlZ83n5mFne2jezbxFhOe0QHW433ErrsZXwjgz8wI98DXwijnEFYSA7UcMA8RGHtwn0o7oO9CNKcj3ygTwXA9l8N43qAL2IH0AucapZ1XM6zWcL0bmd5HHpGI39U4qeotJDoUkCCYP4tuJy5JZMHzjYlh09e-HMn8KnDvcLfLxr0_XFUOy6c--ZV4X5wzkVoEDvjsdNszGtKqYNspkYV0uLeG4sSG5L0qEdSIOs0NCvjuA6JmxMJ45_AcnTKVG36QPKQZntOlulKwKHtx5991yEq9-LkEm9t7AtW3pCMZmwwX85YHayjjaReWRE2uzI4I3aSnfaov0xPziWQf8IrFiLCK10C1wqBIwbNGc-gfz_jsWX3i4NFnxEPGRHRgPnn1_OHPv6M2y4B17HQedfG4o6YjNuFbiXI=w1169-h657-no) +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/GrvOGnGOPl461pzlPNiJ4k_tvnh6yzGQkV631Tg8KHPV34JJX3FTbghA0e3dK_CVBRmV2Jh4rVIScjoGrPpmf1oHT3_PB1NFJroI3gzfByoJ5ve-vGF50HLpYxH_nIcNhnv2gQooPOP8wBF7guo50sFQvarvFMY3lCKCCKu0ppB7-4n1mDi8CLqH_SMPGfx28zs7N85C_jtSdundMM5R-Fm6lofLXsyKzgaDAHD9TcPqmEvC_xFfRPeuUFcxupqkaO8GayQ5T57zLiNiFbcfLy-thgPlcNWVZ9TmfEc6a6gI8naFnbt_gQCIFE-AFacyejNpbfKAKvLnEHrklmbJCnkOJWh0aw0n8xV4pUdKb2FqUgTGXzSv8A696xyJY3Q82nnHKwa75O9tfdt_sKj09IqosuWVRbqabg-tqwORZH-BX1NFtFy1rbeBWgbC8qVMFan1LvNktlQj4HNMdrBlMc86yfG9cOZyOZ8dE9AMMSuwybZPj8NB3-cCd24X_p6L0plFNqr3NGwTYwybAyiEXHZ008ThKbZeJ0PMR5CoCWYcokDjiu6M5W2WivXDfQBaRkdQpm8S5B1tD9VoHIcUrog71H5RhIuTGcVJgueNMddiDcA2SFZhCgqzO43KvIsnzhwdnsOVhr0ckPuyu9pjuhus_x2YqKA5T0iomaANhYMYIkcsbfIuOzA=w1169-h657-no) +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/gAqfwra_HPXSe2x8aF4vUHP88JAoGyXcC212A_yC-WLXmtCSKhaa4atdArHqzFJ4sokjRUDNYayNImwxouLe5WZn20IjdJ9YiQiQWieqk-bpGL_5XmStUCFg_rUsEYkqPqGJENBSRH8ge0aF_VRB_BZJ7CGtkEQe5KUn8p4yH4WEIKmo5DEWdfDAJVBatQOWtQBn8QPeno8b34ohJg8XvHMB_LEYKRr2mggzp4T-sclqxXIJ8uP8AuAFaDykL987zCCQ5iAgy79IjocC161XA8afRUk1yTNEjhyPNtS6NX02L6m88tNz7R5SDYYxJHv_JE0bpufz-8YVORhta-frZXj_S0KNKbXdbtlRA_lCaAQjEqqI679asUSztBMtDKyry-3wd7p5hIIG61ZjAP4OphA8YG5wgtqVsfgaRr81uBDWKcOS6otBh9ubzWn7bY7HRFUoOuHNHAHsJXK7SF4XtFDa0tPcdPcxZejWFSNRZlcxkYMPsR5LGiAmAS2TWrW13A3FYqNEYvFmo-34Kx01lD3Btc8GSXkoyjwdnBKsEjFWP1Hj6UIx-DY7hurwyBEEZybZlZPUQZS8WYqxv0CaJUG2qYUO2fTjjRjRWPOQd7MzmGDjHMrDgTCApPgQSO2zwkPvI6FMj7veQZDD8-DCm_Gq5rORqzTio1_FH3Y4hXvJarvjwv6Uoxw=w1169-h657-no) +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/g7-IrMXRd--D6Sm_2AUVhY8z7X1X77hU7u9qtoG7w2U8JyO7JMSRacHXjoYwqm9det1cMJbHXpw6OzFTXnlBC5GDOASvcueSBX5eJhuqBDHsL0sRIaEWRO4Mn2vYG_SRFINDROLv1R1CfPrzt9nnnPa-JQAsB1A28x5qSl4KKSWXOCAtM2NYrELzCXJwP586C4LZp8MydYx-giehSUWpFw2PiTeqO_1noildRE0utDc5xirgdUJiQi72o-Ds2vXddgZeC8594aToFilFRfp_StkZO6ROIjwKc9JXDfmfgvccsEHDavkN1GygyxT-DIzaMX_WZqwncb_SKRhXOZ2rOR6zPmBgUfag6M8c_AUq8WZpTU5YSrZpSKoGOrHXkWIIAcfNoNXVN87pvM04U7a9hVKNHPw_Sjnxrcv1FQUw9ed8siXo1rAX5Mgu7avdfV3iXQ9p_OG8MJEKooQKGJSnjkxKcCcTGeFPYe0ms5EcmSfksEwoLICjJ0iLf04JoJHFT-KS443LOLCXdmxcn5uWXo-X_OuorJsDsE-gRzJzJj_BHQ__kk3XJCky1nLBkbmv4hupi8khm-cRcfZuC16K1LVzqn-9p0UgoiTZudWw2HScsgjr1wTIGtTRRNMWjs0r2adyvJbT4VGamnMC8R1rYFrBxlApizjDxuMsz52id4xBoTBOBbRjSLY=w1169-h657-no) +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/DTDkm9_X6tAiddrr9CdOQ2wLBMUukI7ACX9hn0lVE8VufCFfeDlbyyzthGREqCwzqr5HDjn-D8kG8MoAETegPJgQakBwC4DARAbSQ-DHtA8KwhMkTe1FmeQbZmfzQWjjWrOpP6eVWagl2xIiXzxVimKvolOUn0MaYK1m4NIffhmCouYgjsZ1qf-nS6OFmnVumeEGy0B0DnuHTGKZjr46LnBdA1zOWc0c-EkGRRk1juB-i5lnjxi4V2waxLPUMdttLGTPlrtUmsYwPVhEoZlnIxTHFiyPp_7s4nwQAQSsSnE_SThzpQAxOwL4KofjMQZUbbNrvlQKFuTWGfKhEr4_AiJFDsta8gU5PYlCHrQTXKM269Z6_AWgSjS4ELLSgM1k8GoxwtHic5oz9IlCyxzftsg5Ey2UnVsnw2a9ZJmXYTjJmraUZwQKHHnMAR7exvHlPUSqkPmqXwKsy_h94VLq3swa1peNwCkJBlNT7jnRqyYG9tFp4MPFOxQPFS8LWCFdBxLsMIehlSMbsfFuRysVqhvJujxq4JGoeEEXU-TNMrmdrZby749A83M6mSU8yuh8HuQdtGDrH4qmpQDcEj2GkGBmHE2W21vXq8S52VqdsZgIWs0IxJ-OeEIxhigo4rysZzKc8ru_uwlk7UMeinJ7UPwHoQeWmAdmnUrBUj5t6Upgodi9Cqr-yEw=w1169-h657-no) +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/oPeBqb4lQ26GKtkNLSkKm23NtEfvxsuAmVlBkbyV2KuxtWg_v4WA_nBIqBC6F50ccddpEqU6Om0JLZVxpPADlv_-j0MtBpemHxwXzorvxrGGx6-O5SpT1wMkLs2fOU9TAc8YY7Fb0REgUMVuNVUthKJgNRvPVWekDQ2kTanvy-gY9BFPMclo5A5ilh-4TnnrLDq16QeBQEoDTttTKreM58SobqX-bGVhn07DOJxDbX9ann_uLOk-Jjdx6QJtN6JUGJTRyt6J6dOQg7Iv52gSQ-ld33zF15kMOvQ1ee4CyX7T7SQWJxt_3R8zv2XoO4ncIbxfEB8ciBUoZuAQmWPGVIRB1C_LXr9jBcYcGgfim13BFrnMeSKDeR3OMfSN3dNAiXr7CecA7Fq_39sq9-8GXd6BWxWn_f2gwAZisYGSuyHU92f9F7btIC44X1UbeCMpUhL_pEl4lSEmO4N_fDguMosLJ_ktbDbxWfGAxEbt3HBG4IQ5gg7CJDKvnbs-9IhNR2l4_0kOxvLOWpriOGTCYyH1X_TDEu-4YwbJGWZjgk82qbCF2E7x5Wb1xqjDN36F7FOF-K8UDjkfYwtWlK2BsoFOFRhqw7JNEqcR5CSvkpUm1ldjINBHFMNdg8KpjyYsTnx4K7pLOU7j6PTsD1nYIvceiwjwX-6Fjrm4m-wVv3Y8cPURaAs-yZs=w1169-h657-no) +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/Hfg3i6OgLqfGWe9DUOTE8xoxz9zQr4G_lMrl4g0Bqs-yLalNhEQVx-R2nrqCUNfiDx2B8zduqVNE_uduhmcQwwM_CnkkxZh_V6ilzVmTDJeVKQ_Jh0s5-z0rOnbPpbXvlMe3k0-uRBYra0t1fcSDFKj18u1diX5gS2GX_h_mZuBlZCkRkLoQqQdJYNVwbpDwB854Kk7i4ChYY_qTaMi7v8kGlUIcIiBNffSmY9-GvJ5gg8k3Kgj7NLkO6RKDxKU6yp24XGQUl3F-5-rsKBS1QnVTCpCj_H9BXwrKqdYeQC_BQHKl9S50xkNxF-4acNe8y8QU5x_h7AGGIgODjgIu_p9oqhTKZY75OyAOcKQCh_7SgIcGKJz9pvnTu5Ljd-hxqcXyC7IRSMbSpiGviqNV0RwnhVr4gb1GVCZKdhLsKdFyxvlLfk7yXFgynviXzX29e6gmTdp2V2_U1nKGjXjM4xDIBgZ0Yx0L1hTwiIYLrZs8rKOo0cg7HsMoRTaLTHcRvUB-XSpVi58XnfWT9W71AiDISMrT39-fQPIpH_R_JI3Z-9oEtJgCQ-eCThIQZbtcCDoFUTEhrDKkkqs1yPKoXvQeOt0tgIuikUmT2uHym2gva-uom86o8wFmF-pOaLlLZ32VZAv_fRw0XMbJyTddqNvdlAEf2RWC98-BOhkcPalJh4Epb68AY5c=w1169-h657-no) +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` + ## OUTPUT + ![](https://lh3.googleusercontent.com/Oj5ONGQBnfB7GMfgb5vLrR1Z_hMy1PecYghDDsIFBA_4UGDz94P8QlEZ_BtYmPd12MTvvI8b1cvrJwiV8ZVbkMcj5xCn8qweB-4obRZyXm18TUqa-JPjBAdluwK_Q_H5R35e5aavyVr7hGeA9A0ZtRfmkhuY68ZWLHLsP1S-suRbCec3PDt4juLKyr72KzmpW0ccmVsLtik2Uekq5dXZqmBewRMHLHuuMsBClMaeMEq3kfQDTE4h-Lek3XajZkLpc1KeGVSXncAiNYoLkMdL64iG_2xXi7Og7OWQvAN_AkFIFbcoU4ZvZTNr-i5uTxQO4AqaXW400kirVH_K_2-RMN6iS08RUW1mXXwVDDjRoQq79Xsr8JV0An3kf-ZX43PXNlsP-h9SPJqQCTTuR1KHH-Cwz7ksmCs23NJUg4EBlW0US2h9zk9lY9-K_hFhjYqvOODNRxZ7TJgmnsm9Ol3zBkAftrDuKgXDg5HcDTRy1Ae7bmoEnzawK9QDnbLvBO-yAgZWGh8a9nZXLYC67gz6LqrDhIcVJIQ8LLHIhtiqy3KTk44gdB07S3KPKsvnfEskMNbFfPVG6EcjrKGNuFRcjUzkkWwm1xXZzLBgWbjUBt679XD6B0DSGrkWVPHOl23DUktxLEhtbqH6URCk0C_BK_Gn07K0NbIs9LwIzhCOuBF2TEOe3tEeUbk=w1169-h657-no) +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +## OUTPUT +![](https://lh3.googleusercontent.com/DqWX9-G8PD11w88-o2zTluVPYToTZcO7q_LbijqU_h_bF_BSD_pRKz8_HQUFG2iHzlQz-zQx1fP0SdN-58SxHgM4TL-N4iy7JIXFYm-SkdEDJ6iL4JDDYSYtcxsbhI3tWNiKFgZ1wZu_fKyIhS-Zku-_T1QykOerngAtyzIAwYIdIIYgyywCtDlrnUOP_T3NmodzaG3JIUoXpvWypLqFBY2JDvMcVbQlli91yr8CtqWQcENLLAjnBHUIJBp4BSehFAhT-4BZVcwODHz0SrhwQb0WcxpdHkiFUBeFPwkOUuqejBTXJRhUEU-kRT31oPq64Z-tER3sO6pqbHFX1hdNqEbis_ecc52dOVqRhvmAXm492UyzKhZdZo-e7h-oJsVsHLqmAKV22ti9cgftJrY_lOoj_pzHZr1EPcUoQR_LiRNVNwSScRIKlmLwtra1NxIKMWTte1-CYGq1Az6RLYrwk0_L2JBzOj3NMMyIUM3xNelfY3C2GYAbzKo7qlRmDj3N06PwAQUDVZPXDpyj8hQn2C9Mf_RyLB0r_15m_TTeIilVoXs2TAZZbrMFOMajciVFb6nvurg9lkFJjBPt288Irey3YMnEf1WuhZboyid-qCVVrrVbXZmRFSIn-bMQh3rJs9Jn6oYAHraQChN1vCEQUVqVslT7n3_U12pbyvI0e_AwRfJuzloOSh8=w1169-h657-no) +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +## OUTPUT +![](https://lh3.googleusercontent.com/6tc__7L69zKSSR36BGXwuybe5Rz1T577iJuPRg_8Z2r8wDJWgs7HMz3X33UV_Xy_8vO1pNp0wYBbqkAWYUg87lrbxtebYABRB_IjvzNL87TsvZeC2BZQGg1BYQT-J4tu_m_chd2uxRiZU9_NHpM6ZR4havvlU64kszI8yuTYaKiljA0pRcTMcAyxaDj8wSLPIr-XMRW_MMVNRTjfWsprOd9xjG-jmA1DAZDd_c1bOUu5Jz9_pPJs1cYBHzpJMoxY6nHqcyDJtHYnoOSWGMIoRoTkkF13kFZmGLfz8fAutx6K25EL2kaUyvkC5odT5I7haz2cx1uJXagG26unKzuXUFS5zUYWmHZoKzTpyFA4UEMJrZdS0BP9k_tO9zZXefaA13dsC-ow24cb7ZFleyaaMaTqMkfHunNDIxIC_RMWForj__ZO97zSqZVj5PhQXct2vJIpTjss3eTheyusQAWc3b1kNzjjVDf1zIkyWrSbqd87rC-RfL9A993E19CK6hsqTpTZ7lajQNwhCkf3r3xUsulBrK2j68fP3wwEjbFLlDVUeYwjErELC-fQIqquU67yCfaF4uj-FZKmeEb-GnsTKarE7m2sYy2cIisMt3lq1anwPfj96pxPOTPrwuB_WgSmE84QxtyBATWsn_jeL1pI8tHjdCMgOxMqZjvQDWefQynsQwr-JlsS6gs=w1169-h657-no) +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/LJNlSI1gISHOnRsZ302OlFRmFxPQVsDaaHOLjTqWIiM3G5iKLeRhYHRzMfwwM2LjHLlypHpgN7j75_K4qAucgy86LNwinYUGCdgwyhz-HtjUmpk82ZK_PZ4PjCuTvUjPabj0iOFZmFLQyHY8U-Z1GgdHYhyiK9-XeGySWyMvD7AomlFPwxF8a5AyxmBNQGBX913yTAtKMzcZzF0KthZKE17xmIuqt4fzOLWgToYm3nQO37wPopmVvhWlPBcdO4Wv-BS2VvT14V7knltl3DtsVpo9xc-ynbRrpN93iaCsrBoRo7NHek350tcJ-9D-UMIUy8R4B4WOaKkadLV89mu-a6mcqZP9H8k9MLGzwY8kchZUaW6U8S9UIw8Ku6JXPu5uZSMEAEKFIze4qiMQjLqx6WndSQgUWJreuyXZ6otVQfQtPfxMLprZu6wOfDDBfEgSFsxykHogMu5ATbV2qxbfbscNzGU8_hAsSnc0RS7nPhuI6vcal8iSMy8WDcgcuIp6CCAQB346NSuFTl4_FWsgavjmCjyzFz15QkenPKInyl2bResIcd7OF6aFrqlCu342Kk-QdlBr7iCPH6jqPwBybmIGi7l72pg26_AZYQmvvOC1UVGizDxvunMKyhycp0rXNKvkeCtBgfdFjTnA7-CixKPQuGVGoWwBhShcbllxo2XJDAd0DczlOjY=w1169-h657-no) +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/mgWe_sEqTyiloc_JJ4tmRSH8s_CGZtB169GGhFbwcdbZLr0gzBSJJc_GFZHP0_04CLojqaR6Z9rTEuFvnOaLK8-d3qs4wKX7RswMgeDPFYKqK7B5VermEf6l7mub9-SavXNaASiOQi6WnLFWyEgmTCGmTRGvzF0ea4vb7lS1SnzqSXITzCMIp4p5DQL2ODcBQ7fOg53vl6wv_RGE0J1pDmd6YdvoMlxBvGo_27j10j-ESeOIWCAAs7dqd8QXBMaS_sPagUs6Ck9AjCPtdtW0LV_qqgiX155rIIhCqJrwGNkrY3Nw23V7jz-UeckvG_prh-XQe00Hwlof1O05SYNqJLzVJ1W8J4TNEThWvUoDrh-3P2emFTVphjrfaOxLlfUmFvBQ8wcFz3l9vjIJYZpVb1A1gSx0BVLmtmCyWTbtWRBD1E3LyjQA4EsSTAX04RbdAACpJr2dW2nLuKVNN4SRvTA3NrNQBwao-9QVWPZCt6on5TZ68E9rqRzK_m03kCyvY0UtDvZxVGkbfGptLVJ6s1oxzkWXZqTQqRKJEYnXs3xfB2GAzWNhbgy4uUT7FA45jMxCzHFCUW_OVG8PB-1NTisXemmGMVnHHhARUeQUdmR-poc8mgYOO3YLWg74Jf5RhB9sLV9exuLXKgy4XjrqLX947vEQgCEY8yfKzgQEwyHhMxb_Db4UmMA=w1169-h657-no) +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/61Mq7PlHHsH16dtdBxgw-uhmZ6mIHeFTLH1VxiThMiehpwoYwyx9_VmtVbDH8ECN_CfhqcT9Mkg138EiK_X-mIZi44XgGnW441LhbTPthJmrdllzh3zeFt4SyQD9K8bjeNklTapdcgPyYAucOLGa2KJlEJYaaizZgk5frDN0hUjd9rcaEmLVfq8vQLSsq1kheDZ7IWbInJkn8Vpwgu-ZteG9ioqrvIZU2mXDdu3nVVo_Dpq4CmfXJ7fKHl6Y2MWyNEVpV2MJ8q85kQUA6VStTeIjATxWo75J7MlA1USpdbnAZwHiWspv1oQM2Bqsb694vUx29OTtesQzNI180JDjJgIGAnspgYvB4DGx2VhpnZEi17qbjBQ97eDdHvwJc2YsEYGNSb71d4KN8pxpTkcgS8D5eUYuG0mLP641OSvpJJ48NRMfaxb7_x-WkexjFXEv5lrPhXeGuvLnYi8TbHO7ETQNDc5dK6NM3lSO2w-SBvs4Im0uKr8Bos1PpsxsjLIaw8adASGx7NPYSegQcuJq5YC_Ol111X1-bKoM5JiqTtyE7js9bcb4GFLLW1twlOp4BhLAeXHPzAifUIwN54wiUGKbbo0Tt8n5t8IonHmPUHG5WrUfAjMxTExSn6dE-ZvZ3MufGrBPaqR8D2AS0XAX4dLzyzoPaDeGjJddFgCW8cwRYqmhL0MvYeM=w1169-h657-no) +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/zRNOx-KI1UlARZiVzFwC0JnT0s9n5V7mKIjMEXA12T8slnr-jugL35LDtu2a7sTIbs5dh1tqxLPcj6v24El3EqgoUbaa6CSZGQoMAYqXqS_RUxCQ_8V2AaMSUzNXCSe3rxbdSjrTqT2GnF4TfbOn7YaHiE76dykViQ6M8kecqeykjjKqfeFYJ3o-GWOUH3xVS-iY0EbkkDp2TQtmRJY_T837faZKzCRLJVhat71mzI-aWx_gu975e_Vup4dWvz7PkDpQaj5Db5oJ7k5dGwroL849A2DAnu_deMTcnVPvL4Zej--KHJOfjR8w1M6jtT6b3StdN6DV2xSlLKupycnXsMHmvWeHWAgilTCaLGGzjcY8oCq_jBuDDprMI9XfMBpTML_w7sD6h8twsfJPPLrB-TCn6CHFUu3uyO2Z7QTETKYgAqBp1STZyoy2O4ig7-DdGZ3kiWWOz_PWRxZzPC-vABY1NCYhG-LdvwtoC-mNy6Nb_Py6EXAo0yFRT3BDeQXAelkmGnrrg4pAKEPPhF4DsM-K2VTw02S7_7aT2vvDE2jrUZvi_xAMVKg3CvMPkNJ6tFd5bQanEpRGSvAJjMZ-WwVMhG8h8Kp37Tnow2v8cKoRrRThiA5bfvyAaJZbXSs8FJnEJVBKKEBOWxml3ruyhKfw6jnEiPeioQ4X1wCXr4gNoBhc14NUhFY=w1169-h657-no) +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/qTAcF6hDEQnrI-IuZVHq7FfMX9KzrF7UKjDBi-xJH7MPoNLkXxH_lSk74pKRpjRBc73rdtvX22FalTbC1buVKaASAwjcpUv4UySuAHcSLAYmLNJ0zPYOQsLM-Yl7T-n43hPp1LZqsk8c4BHijFb-6JMloMexpPyNqvfqa_Gy9W5SMEvoQBAOM2Uoh9nhNs_1QT5dqQ5bIIFEs3pdh0-U9QJHHPTxJMVtsj53E1V3Z07yp_6AKQcN5Mn-8m5kCtgSmF_hnwHqp-hIvwME2z8x9yd3L-WzrORN3l7T_FJr87vg6bHZqIhRqcnXhYQ6fMY4EWnEi3LJaMd8hbR-sHdvZ6h3xEk0t46Hxo7uFdy0n3uCXf1ckKL-eOgPLi9qob3IGewIvlqKnu73ZHuzaBW_SuuvOgAlCHNTprXGglbUCsnDq9gib6MeyZViD9n6pf2h6u8zwl1UGhoF4lKez7sqG484kkPHoUr6wIRzoGZdj0EHfLAMPbY5pghOSyd0RDDmeRiD6KnHCsjLQ-GFcUuR-EGHQMFGC4wio1VvkhKtmBfqYhg2dKmc0cMW0QCDpYC_iKd_KngjuHtioY_QNmIYJU8kPXypaYg61iUUo8NEfrM8bNkaBlji4-ArEJjh3lm_6RaolpzCegSTNDPCkX_h_8sC-YtaViQ2IZGRQl8Crsy77QAvVLmOvM4=w1169-h657-no) +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/nUWky94iELqXqlMlmhWXqSTxNlosgbd2QdPs9KD0LA0qtJQf_g3dokcuyCuhaF0f0d2z95XNz-V-UlCyR6kn_pzXZyyIDYK2WoGBZkUUgQlJRw9RDaSEJNXNA9yAr0A7qVd0PulElUS35ew-juRTTrZi_r88s-pqbz4FzIKPP3bCaZuT5cO0JvAWuJHJyZEjzfDIjTpOxtuTzImrTOLSSvNUWxF5nrCOv1gly37QUVv57Hq0w_rRy6S8w-ip8YGwRZa1jT_ZGIolzs15aYUsDBQ9q2wE9mcG5rE8MOrEawn7W8hFZ-8MZ8ho4Tnn-e2s8bys6jYzoAPv8iQZL65mNmf2_Jb_CsE7UmmKcV5KYQEWx5P8S2Z2T5fH6WnuIoTFpjNRDP4Kq14a-ilY_wtZt420bEGiMju0rqJw0UULyDEq8MtJNX4q4iglrQhvkm4npfP-dnyTL46NSzHsxlpE9Th-AQBTYVHBd-jPIEPmRySzx617TjoCaMbbIg_AejvT5VqwanOJ7n29qjVLNV9mPZ6TP3IZyw2knWwuJEs9E1OMtZX54azKe-LDW4GfuUcrO_97OkpA9Sv2BbTIxsTIUbYu2-U2rbMAlvVst4tOfb6H0XNydpsp-W3jwof596FuABqOj5bQHMd1cQBs4H16TuUYNFj4JtUKBMNZLs9b1ppxvlKvLPfNff8=w1169-h657-no) +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/4zcMBcRwU6mmPkcTPHpUGX2-Z2njh6p0pbCTPmldh4jCj09eKWkrE2Cwu1TDehZB5qi9s3tUoHZ5VFf8NCJ08EcDQtweHY7u53K2mdgAqmNNAjLIWFP0YvnBhfsUofi07DKxpCbkUW30WIuVQ9T5_SaqrClcVVriCMhAIU9ZhH4adpRnQuR2kKJ5SS1HIDA9k0DGnvswgavCdB90sy36np2W8Sb-tnv1dBewHk5OKBwtekSckn6DWGJhNJlOKje704rvZXvhY4IWLmVasEN8UjV3lS_o0wSCuSc_AfCWiS1Q8lK4LLqDAW04Cm9KzK2uiG5IKH3GN1KExd0iO0E-lKi2o0cCvYJo96N1fLtwSD9KKvRr7BZRIjxRhChoWxdTDVFYsczQVwB3VY_j-ZOVT_saSRIJUYPGzliAr_3hvHTRbuAKl43MKzaP12Nl9nr1iFN1Hb1_-O9DFP6hkNMAszj26iTgiJEVxKb_5t_slxHumjdn6qxw9rocJdlzrEJeVvb_xb-8wdqN46FqtYLUO1Ql2sw3qbfpXwxrDhUAeZodPnO4pS-2oBF3hDwB2pUfkOXI0ryedcOpSyPgCvVDm2PI2GaSgaxMVgFF4-LZdLDxaY_IW0Rq0ADp0ry83gaDHcT4djjIWg83hYIZ6Gt0zCYVe52NMJ8w5fBWXAvjoxd6iWds-C5MpKA=w1169-h657-no) + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/ZU2uve11grG0A0395GMSeDLW4eMrm5s1y1VGxbMsFf63VPeEzyp_sApjqDPX-Qbsv_ScFP8peLvfQLL1afMNsOxmjtn2JO3tNAqeB5HpyjIEa46rbW1OsJPnMON6neGxcLJkza_LH8W5AheJPqgwVHrEMj1lWT0nmJQSa5rw83LSmXVxi67lOnpFl8g0quG3Ty_aAoGlnV-d6XRYzeLWyCTAEzgjQe4-yiPDxrYuB-oGzk5BfAOdm9pZuelRfMsXcdLyH9jetHi4niJ7DXU2yqcYKKtiXqGjeexc65feUhA4ii5ceTtgzixxdP1UmwK39U7Q3oOb52XRMqnJqUHrj3bDUGDKdYUfWVHjQ9L_EA-Hcw-5J2xcvyCUKOfI3u6JhYSk4NsgvUfcIR-gl7d3SYqtzcwfoaakZUJbjn2XN3_8jObUCRzjtvCmGSKqFOWmqyGridVzeqfFdXGx4QTdcHE9XW3bdv5gAp0p86JBH_xxK12zGufMyZg8OJSOiYf8GAy6slLY8yGbSKOcDDiZoIcmCB_FfELIlYvqNeAqpFsorKu3l_mcO3yGQL-qEmBn5QY3Ro3rnTHyE0n8UZ50qX6xEcO1fDO8SrSxdBN5G2KAWtjB_n_1IYL4SUm6Wk0Bp7mOHyqxGw3wjbVxZ-w4S7mGKqv5wIMyRvh5ora_6G8yS45oW_P0gjs=w1169-h657-no) + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/kmz6eOhEsZ9lbdlopeUKT8gO26QIAsQMzRDDQoj1FPegxWCH5eewl-v4MKamDL0LFR2UFYJo-1HQwcF0bNCn6_XKnqylJdaQIY5qcbWnmkENd10EFC9IGCQSswnKcJaSZJYMdpa7gQrE3cIGLiPKZs4IpYi13I-zVtbZhv58htzy3ISBFWIy4Ab0JuznBZ3nf7eyDleTDcfyRbjeNcqAqWfWzGdKJWuXOoZ6i24R0oh5tYwWsKesofGuiMbovC5219dCDMvFpfOUrwAMp2Rzgt_lzBlsD7y1YwgiK3UczrSJPsL55SdJz4zMHl0Gh2zMsxmSWwatnFtH0JE6D2MeeGHdbZ-OQ21EZ2ciurL2VybTUuLojtMFyLTOzp_NsRx5CdqtDMmJH--ixDvwAuYfJO8zPKGVR1f_XtE1m3olZe25JOFR6LPLaBDQwVlZJKwFavvNQn7IiBv2cuc8HYWJRQrxylfY1m1Fs4rgvk-y3HD1qXDQc3uwhG0tr_Psb3sjOnZ3JeBVCo-UDgLb0U_fCormzcN2IKTefR7kINWLO1B6D2uHP-g1R4GYxCrLJpZw5WQijG-5V1y2N6Y52gdYjC2rSCv5viJ3VPVw7TuJ507snDrJXVNlvpV-k_ueXqAG7uF7ETS_1UlnF54Bf0wAOaF-qysFI6mI-zC8WgybYNH393QobUZT_98=w1169-h657-no) + +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/QvRgbp4chBE_n17yfkX2ux7I6jXT-4-MQIYj34YBwQKRsLWV93WR05419KvSUT0tsuvVk99b2Qove_ri2GwUpmyGc8b_MwY6XQRVyqp-KpQ3ruZObXLq3vbnBY5Gv53IXm3LwvFp7rrtfi24v3P-9IAhRVcaN4Ip23UyBY8BRofMoTO8Z3jVbofrGUmZburoRuf342SVitdST3Q5XXtd8oUwOOpSh7RkjID4bwbm0d2Ua4WqtK3yFZdkyWqAiOk06ahhwxzG9S_cOBNyV4DpSp-_kToB9q2VN3psRb4YJZrg_ecYMvcH3OYKfXF-ICNe2InGZTrdGEw3SL03YoGdTpcPcBNT_sazFVCLJJ4Jc2EyUTfA0EYjLE1HgFMcUzvpsEX28JCZMznsURux-H2vXyAw_9wGxnqHWhpGL6g5oGqjYhl2loSyi-iIBxL-LaW518E5U9KZYf1ASjD96xbyo7e6yW0HyoXXP6hp0EY9gjGCKsa6MWuRaN64WWKNxFTmY1BOFo3uMVLAk1MUetTC1XQyY-XHyYBLkZXg8IohTYRJr-rtwi1xcWbNOXBsr9m7F1jLmOX7urgK-zZ854tvE-hy23FFDbvTkuMMAKZ6jrjObJuoECwqRhdYXiprQQcmSqB0qDkI_OgOZDVAABYCI6y2gNygLgFDSX3GCXlhGP2gx3NUmTd01IA=w1169-h657-no) + +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/YTqzUWW4TO8pDSIg73Y3FeBd3gV1xiiuNrQ8ZFOLKi5VcH_GPCvAtAcpdxQ67ftZ-yl6zR30doBe8agJL2A1Ilevqo-F8-xwdDxL1Tfn1qpcDhgkharI2M8xW-VH_gJKzg-MZe_N-pz6xt4EFKbMjXcADCUYq5V17cUIcZiIiDXI-ox0-10iDLwdeTyqQ15nqfVNo9TK5j8uBZS_Un-Zig7yo_1b5jOS4C8G9PdQuYcbi64LHkJPHYQ1uSNokPi0ffuWd3MZNSVwOd9v_e9gwkZd9ciyXOfqHSTHHZ6Sym-1NLExzCrQSpzAmrR7Rd2cMiP8y-LxmbweZuPTUTH8rqE7ffyAl-0xty9EmiaV3n4t024xhECJwm65ohZScaoJ20WpJxX6aia8oS3H9Ij4v6iFmnUPY6ICRDu3AHfmu_Eo8QAB-p590jRBpYLkaSrSsQpaRiH9SLHZ2cf45MG6cjo4Sxc8w4liCAscXT5dkSClq5AvVg2YnMLzv1EEaKZ3m1L-QzdBHny1m0PRLqZXgNxLgB_UxNmLZCSqcvGQWj0UlX8yg_EOFmY5op-yQSFMHKDEGLdpKK8VPxshCpNHnd0gGz8e2ia_Eeba-PCO8EyxVWig_SRUcVATFns0JlbUcteBG9wyKDR6JBjaPENP6BDX4jOMBfnsteUNF5nFwVo35gDaDwdGDuw=w1169-h657-no) +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` + ## OUTPUT + ![](https://lh3.googleusercontent.com/KxP5t8UVbrFfvjQ9RsNgrdfXY0JG3geLF2It-U60fUBi7IPAKUIEPnB_9W4haccW7dYCGBRoTdfsyqIBc3VD2OdqjDVmBDL32TeMS9f_nR4sCAey6Uv2_FH-L1AdIlnea5Ma0hBmbhRIrvwRTRM-FNctBKsCSOq1AohgRoh0eJb2ZjbT76DtWExlw0VTUlSZSjRLtTk8PEwzA5TlRdVZlT5oez3tc479NFW9vB8mXAdOh04f-s-ZdEl4Miy3zb4Drl3-0RtjuyHeMQtechZfSPQ2g7MsztU4rSCAC9_CKz4X8BrCcK6OoP5b_4ZRfSUgGZ3_MmphWNHijBUwXk3md1cCiTTSwvTXXmMcmnX-4hkLEFcGQjsY7Lg-wHKNAqieH8S0QZREoydlNO4fgVaCidW0X70dvAL3emJW4D8tdOJ9ZCdPhztqyYGOJOGFiOoUXPNby3_PXSQkAMv_MvXYt2ewmKxY3BXhVOrqtE7us6Gu6fFwPfVK69db3Rw8AMXs80r5We5a1PDWU8JYkUWz3P1mgtkdd74ZQ_BKAfIoHLn3Q65gCo-m1lUDyW60I88UjzsXRhJgwq-_w7a0D1sklAyAbrDTkma6T6hNbHGebHHfBKeXPechHhgb71Sp72tSwRqdgHN1OKFWq4tVbMIlZE6MePE_sz5H27c6FzLJjmZM_Xu1OwYnLiE=w1169-h657-no) +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/wV9t2s2U5BVz-knupI7Leu5vRS8lm0g-4how0xdp6DV4PSXAT9mVp9IFdVtvl16M3n3ScAMl-yeE58SEiQG1vKVMX-N9L-ce9wdSWrnkCEhLxXAKGY3TtfCY4Nslf11WhrwsZFJnWXZnAFLxPtvu4dYtMze8NI9YYtuPp5dj25DmS0TV56jLIJbEc1mJsJvNhaRswztktBCWUK6EMn_2lNi-n8HTZEaNyXqVZ22EHHCUD5jYhfWQbAFYfRZ1jLHxmSFjaBk-1KejkT6NlZ35fcaUmLhSSrSQWzod7nCWPVddZaW8U6H92MNbamE0v8RJq7Z4luXbwrew_WQxnxYuaOxxAcql-sijbMy3XBXGeVyNyFapU3fADLWtOQUq_m3Y1cAzGvbk-lMk5MVHR9cCM2iqiin9bsrwlM4k23HBJZOCkPXNSA40R0RboEC8PioBi9-G0aE9cnox4q-Dii3trnXi7hGAI3IvqecsRrCQdmUe6kGVs-4zoRDtJei96pBOesDDCEOR8zfLOpIQ5kUHFjaagwB6tuXp2cro7GKzAJs_HVU2PTq3nKsh2YZlWWlRDzIdUbxOkVHqGbb4IJvMXRhlKienmir5gluwkH7PaSNQeh-5F23XQwQYmJtPnvPTS0JLRSBXsaRWB2AxBAoLt1kKoz-5gJqXGb_NIecwEEgkMi_Dmf2Loao=w1169-h657-no) +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/32jc357Nri6l90MFVOjGOMALFMn9VAyhGvBtuazccl7q6LQjhY0sQGyEAvXUXHd8LyQJb2mer7zxZwsppkBx06l9LWsWzV0AzXsxg5xvFFFGbz8APR0J8TOBPxDAdjiI1cqGOILojvY_Yw4ibK9G74D303IMCvgLBCsWqpjHwpcRc0PIslmH_dpvfH9FwPKvihmDh0DgQFYWCl-1Emrnjmi8lA3nWuo3y9O-EPwBq8eER-kZ75tBE0KvLsr6e3iB46VKTQS2mEfos-wIqUJNk4dKluzUeRaxTsWvsBaGJnrLm3FVFG8g8Uc2FtM8hYt9wNFjqFzll95XnO8u7i0DC2Wz5wMuTVy238GbYhNElZ1CTfB6kgWeQiFxtP5w4qTLdRCeuPrD66KNvaESvI_Uma9u2Le_B2rwk0bT7TTFPFCPDm6E3dG9XYfCf8MaWMXBnCQ8p-Y6ul1uFTP0Nma8Vgmlf1TAx3BM2I1rQD9SdyAmdCIb97dKdI_7B9tr7sDcOcD7rDNb8PEI9LUGdfSavoNpcQ_MqldTzypXHL3e-QBMdqefdV0iY1QDtX-css7oxFwKlJ-5smLsmaXeEILlt8oKI5LtwfzrO3vLj_Jn5_kWlkCloj9BaQc7_QwpRnmpBeBFSTfQbUrsBYo4Si3-FDWXhe3p43BMgg9GE6ODmjvYjMy-VPnVzs0=w1169-h657-no) +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/8RObSlD3wLN0SKWnXt6hAR-uOKomFIGDi0pN09zAmYQhQAd2X_B4GyCRqLZHqHyi0dgsdtCKV-19_Xk4OALw7WSQe1gBJ2HvvXSQRvZ5oAUX0Jsp80-M4_VIOR63rO24HBhPQYY3dJhyhYUVzYJdd9qLhwDxa2Z_atidacJYB3OPoMfQxS9uUtn41QkaJnb9KuYGGj6Tvzd0mhGER45Zp9SeqHZ0fAgtssxqSPUeeKfTsP_RFe_9qDzlyiiQMpmtBroENUeU6kt12IXaVgBu5Oy3cdQW37o2z41XVx_X_g7rPpd2EVeme13tUynDXQDFm2e0POMlcp3e19GGjjj1o_CcrzN5Zis9QOkOftlxl0ADYngH6WXfgjRR3OZy236IWyamLKhaKVml0jc-xlFtxV_AuEr-ir1IoL-tECQGmLGq7ParHL4-Aw1VBWA9cs12tC8qn_xAy6YhNMNAJep44-rLjGWqlYUH8pk1O5WJKw4Y90Q6FDRnisZPhgkEXr1QvqtjrKMLTtYX6KtrshPIbdZc4Bmub6ZIgaLg0-ZFAeNQxw3u0Jufg97cFtHAuWg9jk-im3LCmviefIJJ2WTFPlF9LvpAECjxn3G60sNfe_7izNGPZV26fHKPwU1yqcvM64x6Sx4qcR9BlKV7tbG6WO0t5_l-HQ99KORaN5eT8BotJ-oCm6y1ch0=w1169-h657-no) +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +## OUTPUT +![](https://lh3.googleusercontent.com/gfOhyR2xWGnpe_C2i2mZfebOC2ADnPqIQYMKe957MzpMFjUIc8r0PgHAIL4ZrYMNrGKOvAIfDgZX0UVwuhwAzYaEo417yuLF2Mt-HC5MsCCXkFDpBnvB3joSwKrTfIjXxu8jWDjvWqvtOrbsTludtQHdrI93z7BtN3mpWF42K9Lo9a3AbRymWHvkbQEuw2ASWXYydP8C9WdMWy-L8VxfReb-N0YBEPx49yUXN7I2zRrJf9EVmcTvxUyXiee9JHfxy6FrTfcoqI-vMmcRyKaIqtwTwnkdUaIaI6ZElFS-KJcZrrhox1QHZ2hFEUG94iig9GA5TUoKD98T3a1rxRDnNkUVQiFlUQOSwyOauGeIuuX_cgGY4xprl2d4NRmWZfvHRJl1hMxI46xUmtKdm21HOfpBt6pQGAZdhbEyNDuO1wnuA105b3m0hW5JZzxgejmqknNRL_jTW_j-jVwtdpaX6U-j71N9v7LCk_dr0_x7Pcat7csXNq2J9oQtWc_lq78EgRojX_uh9zUssED-iEY9lpfP_U3B4nvFRaLm83ozDENSIJit59wHOW0cPe5xDLoK5IHk4NqaWagix5iF5XU-VGpPqqxycupLu_etDIeaNbhR-IpPXq52aHGy5Us0EMgGIIMdVvQkR4zyB10ED78lfXWBwcptkyFTs-fuLzitfli_anNypDW5e_E=w1169-h657-no) +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +## OUTPUT +![](https://lh3.googleusercontent.com/CpQ2rL0CYbFGQla5h1hnguCsZrtFBJmNshbVypgzmTSSPdyaCtiNxdy7FYiQnDvVwfu3Lw3sdVmLaGQE3pJDo-u4f3mxJkYiJzEwfMAUt0URolj9nZpXnmi_IKp6o1jsffHkjiwv4eEg4hokZgZzirzCz-7Z9PN3RRcElxgk4_n08t7RDmwJ0X7GpReVUSGpOK3OTSgfbYgUYsyjoZwN-V90tqAIDRan4AO7DMeSgXrOM21qIsHm-gD8j_ILEvfDTf8xMG5dBa0VDMXmO6voLpdNAb-0ksnF9Og23v5TVaUb9XJzXb36UIzUc7Ra4a2hABT9v__ybQqJSI0fFFk5KrC1X-fQE9k-AQd42cJkTOcU8f7Eza4OjNsywyLtaYPqDc-S5HOqccmu-n3MB0yCUQQyco9e4JcdHG7WQ6uFt0iBLJ7UlNv9FQLVr4ChQwg_FwRESSTUCLuSE-mIN598-KLSRY1YU5w7g1qb3HELtVLeANl-Sv3JrlqrzAEK1frh-WJQ1iU5xDVviVx60-Jv6tAJ2ERvjRAhRe15pu9d-jmjWuJKEzD8NwtxD4q_Nj8vwYqmVWU9objSTaEVUq-onjAkOx4Vdpp3yzlfVxIWiAb3XUceWFc9FwR2TyxZP064tYrQ9ePfo-OmvP_flTFR7tlxgeVs97xAoFhppYa0iPWnFH9NWvspEzk=w1169-h657-no) diff --git a/1915327.md b/1915327.md new file mode 100644 index 0000000..2a2da4e --- /dev/null +++ b/1915327.md @@ -0,0 +1,793 @@ +# PROGRAMMING FOR PROBLEM SOLVING + +### NAME - KOMALPREET SINGH + +### ROLL NO - 1915327 + +### BRANCH - COMPUTER SCIENCE + +### SECTION - CSE (C1) + + + +# PROGRAM NO 1 : PROGRAM TO PRINT WELCOME MESSAGE + + ` #include ` + `void main ()` + ` {` + `puts ("Welcome Budding Engineers");` + `}` + + ## OUTPUT + + `Welcome Budding Engineers ` + + + + # PROGRAM NO : 2 PROGRAM TO PRINT THE ADDRESS + + ` include ` +`void main ()` +`{` +`puts ("House Number : 111");` +`puts ("Street Number : 2");` +`puts ("XYZ Nagar ");` +`puts ("Patiala");` +`}` + +## OUTPUT + +`House Number : 111` +`Street Number : 2` +`XYZ Nagar` +`Patiala` + +# PROGRAM NO 3 : PROGRAM TO FIND THE SUM OF TWO NUMBERS + +`#include ` +`int main ()` +`{` +`int a,b,c;` +`printf ("Enter first variable : ");` +`scanf ("%d",&a);` +`printf("Enter second variable : ");` +`scanf ("%d",&b);` +`c = a+b; ` +`printf("Sum of two variables is : %d\n",c);` +`return 0;` +`}` + +## OUTPUT + +`Enter first variable : 10` +`Enter second variable : 20` +`Sum of two variables is : 30` + +# PROGRAM NO 4 : PROGRAM TO CONVERT TEMPERATURE FROM CELCIUS TO FAHRENHEIT + +`#include ` +`void main ()` +`{` +`float num1,num2;` +`printf ("\nEnter value in centigrade:\n");` +`scanf ("%f",&num1);` +`num2 = (num1*9/5)+32;` +`printf ("\nValue in farenheit is :%.2f\n",num2 );` +`}` + +## OUTPUT + +`Enter value in centigrade:` +`100` + +`Value in farenheit is :212.00` + +# PROGRAM NO 5 : PROGRAM TO FIND AREA AND PERIMETER OF A CIRCLE + +`#include ` +`int main ()` +`{` +`float radius,area, peri,pi=3.14 ;` +`printf("Enter radius of circle :");` +`scanf ("%f",&radius);` +`area=pi*radius*radius;` +`peri=2*pi*radius;` +`printf("Area of circle : %f,",area);` +`printf("Perimeter of circle : %f",peri);` +`return 0;` +`}` + +## OUTPUT + +`Enter radius of circle :5` +`Area of circle : 78.500000,Perimeter of circle : 31.400002` + +# PROGRAM NO 6 : PROGRAM TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE + +`#include ` +`int main ()` +`{ ` +`int a,b;` +`printf ("\nEnter the value of a :");` +`scanf ("%d",&a);` +`printf ("\nEnter the value of b :");` +`scanf ("%d",&b);` +`a=a+b;` +`b=a-b;` +`a=a-b;` +`printf ("\nThe swaped values of a : %d\n",a);` +`printf ("\nThe swaped value of b : %d \n",b);` +`return 0;` +`}` + +## OUTPUT +`Enter the value of a :10` + +`Enter the value of b :20` + +`The swaped values of a : 20` + +`The swaped value of b : 10 ` + +# PROGRAM NO 7 : PROGRAM TO CHECK WHETHER THE NUMBER IS EVEN OR ODD + +`#include ` +`int oddeven(int num1);` +`int main ()` +`{` +`int s ;` +`printf ("Enter the integer:");` +`scanf ("%d",&s);` +`oddeven (s);` +`return 0;` +`}` +`int oddeven(int num1)` +`{` +`if (num1%2==0)` +`printf ("The number is even");` +`else` +`printf ("The number is odd:");` +`return 0;` +`}` + +## OUTPUT + +`Enter the integer:177` +`The number is odd` + +# PROGRAM NO 8 : PROGRAM TO FIND FACTORIAL OF A NUMBER + +`#include ` +`void main()` +`{` +`int a,n=1,i;` +`printf("enter i : ");` +`scanf("%d",&i);` +`for(a=1;a<=i;a++)` +`n=n*a;` +`printf("factorial of i is %d \n",n);` +`}` + +## OUTPUT + +`enter i : 5` +`factorial of i is 120` + +# PROGRAM NO 9 : PROGRAM TO REVERSE A NUMBER + +`#include ` +`int main ()` +`{` +`int n , reverse =0;` +`printf ("Enter the number to be reversed:\n");` +`scanf ("%d",&n);` + +`while(n!=0)` +`{` +`reverse = reverse*10;` +`reverse = reverse + n%10;` +`n=n/10;` +`}` +`printf ("Reverse number is:%d\n",reverse);` +`return 0;` +`}` + +## OUTPUT + +`Enter the number to be reversed:` +`1598` +`Reverse number is:8951` + +# PROGRAM NO 10 : PROGRAM TO PRINT FIZZBUZZ + +`#include ` +`void main()` +`{` +`int a,i;` +`printf("enter the number ");` +`scanf("%d",&a);` +`for(i=1;i<=a;i++)` +`{ if (i%15==0)` +`printf("fizzbuzz\n");` +`else if(i%5==0)` +`printf("buzz\n");` +`else if(i%3==0)` +`printf("fizz\n");` +`else` +`printf("%d\n",i);` +`}` +`return 0;` +`}` + +## OUTPUT + +`enter number 30` +`1` +`2` +`fizz` +`4` +`buzz` +`fizz` +`7` +`8` +`fizz` +`buzz` +`11` +`fizz` +`13` +`14` +`fizzbuzz` +`16` +`17` +`fizz` +`19` +`buzz` +`fizz` +`22` +`23` +`fizz` +`buzz` +`26` +`fizz` +`28` +`29` +`fizzbuzz` + +# PROGRAM NO 11 : PROGRAM TO PRINT DAYS OF A WEEK + +`#include ` +`int main ()` +`{` +`int day;` +`printf ("Enter the number of day (Consider Monday = 1 and Sunday = 7) : ");` +`scanf ("%d",&day);` +`switch (day)` +`{` +`case 1:` +`printf ("\nToday is Monday\n ");` +`break;` +`case 2:` +`printf ("\nToday is Tuesday\n");` +`break;` +`case 3:` +`printf ("\nToday is Wednesday\n");` +`break;` +`case 4:` +`printf("\nToday is Thursday\n");` +`break;` +`case 5:` +`printf ("\nToday is Friday\n");` +`break ;` +`case 6:` +`printf ("\nToday is Saturday\n");` +`break;` +`case 7:` +`printf("\nToday is Sunday\n");` +`break;` +`default :` +`printf ("Please Enter a valid number ");` +`}` +`return 0;` +`}` + +## OUTPUT + +`Enter the number of day (Consider Monday = 1 and Sunday = 7) : 3` + +`Today is Wednesday` + +# PROGRAM NO 12 : PROGRAM TO MAKE A CALCULATOR + +`#include ` +`int main ()` +`{` +`int num1,num2;` +`char ch;` +`float result;` +`printf ("Enter the first number:");` +`scanf ("%d",&num1);` +`printf ("Enter the second number:");` +`scanf ("%d",&num2);` +`printf ("Choose the operation to perform(+,-,*,/):");` +`scanf ("%c",&ch);` +`result=0;` +`switch(ch)` +`{` +`case '+':` +`result = num1+num2;` +`break;` +`case '-':` +`result = num1-num2;` +`break;` +`case'*':` +`result = num1*num2;` +`break;` +`case '/':` +`result = num1/num2;` +`break;` +`default:` +`printf ("Invalid operation.\n");` +`}` +`printf ("Result: %d %c %d = %f\n",num1,ch,num2,result);` +`return 0;` +`}` + +## OUTPUT + +`Enter the first number:106` +`Enter the second number:67-` +`Choose the operation to perform(+,-,*,/):Result: 106 - 67 = 39.000000` + +# PROGRAM NO 13 : PROGRAM TO CHECK WHETHER A YEAR IS LEAP YEAR OR NOT + +`#include ` + `int main ()` +`{` +`int year;` +`printf("Enter the year : ");` +`scanf ("%d",&year);` + +`if (year%4==0)` + `{` + if (year%100==0) + + { + if (year%400==0) + printf ("%d is a leap year"); + else + printf ("%d is not a leap year"); + } + + else + printf ("%d is not a leap year"); + } + else + printf ("%d is a leap year"); + return 0; +} + +## OUTPUT + +`Enter the year : 2019` +`2019 is not a leap year` + +# PROGRAM NO 14 : PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT + +`#include ` +`int main ()` +`{` +`int n,i,flag=0;` +`printf ("Enter the number : ");` +`scanf ("%d",&n);` +`for (i=2 ; i<=n/2 ; i++)` +`{` +`if (n%i == 0)` + `{` + ` flag = 1;` + `break;` + ` }` +`}` +`if (n==1)` +`{` + `printf ("1 is neither a prime nor a composite number ");` +`}` +`else ` +`{` + `if (flag ==0)` +`printf ("%d is a prime number.",n);` + `else` +`printf ("%d is not a prime number.",n);` +`}` +`return 0;` +`}` + +## OUTPUT + +`Enter the number : 235` +`235 is not a prime number` + +# PROGRAM NO 15 : PROGRAM TO CHECK WHETHER A NUMBER IS PALLINDROME OR NOT + +`#include ` +`int main ()` +`{` + ` int n, reverse=0,t;` + `printf("Enter a number to check if it is a palindrome or not\n");` + `scanf("%d",&n);` + ` t=n;` + `while (t!=0)` + `{` + `reverse = reverse*10;` + `reverse = reverse + t % 10;` + `t = t/10;` + `}` +`if (n==reverse)` +`printf ("%d is a palindrome number.\n",n);` +`else` +`printf ("%d is not a palindrome number.\n",n);` +`return 0;` +`}` + +## OUTPUT + +`Enter a number to check if it is a palindrome or not` +`1221` +`1221 is a palindrome number.` + +# PROGRAM NO 16 : PROGRAM TO PRINT FIBONACCI SERIES + +`#include ` +`int main ()` + +`{` +`int i,n,t1=0,t2=1,next;` +`printf("Enter the number of terms :");` +`scanf ("%d",&n);` +`printf ("Fibonacci Series ");` +`for (i=1;i<=n;i++)` +`{` +`printf ("\t%d\t",t1);` +`next = t1+t2;` +`t1=t2;` +`t2=next; ` +`}` +`return 0;` +`} ` + +## OUTPUT + +`Enter the number of terms :9` +`Fibonacci Series 0 1 1 2 3 5 8 13 21` + +# PROGRAM NO 17 : PROGRAM TO ENTER AND PRINT ELEMENTS USING 1-D ARRAY + +`#include ` +`int main ()` +`{` +`int values [10],i;` +`for (i=0;i<10;i++)` +`{` +`printf ("Enter [%d] :",i);` +`scanf ("%d",&values[i]);` +`}` +`printf ("\n Printing elements of the array : \n");` +`for (i=0;i<10;i++)` +`{` +`printf ("%d\n",values[i]);` +`}` +`return 0;` +`}` + +## OUTPUT + +`Enter [0] :12` +`Enter [1] :33` +`Enter [2] :54` +`Enter [3] :76` +`Enter [4] :123` +`Enter [5] :33` +`Enter [6] :98` +`Enter [7] :45` +`Enter [8] :66` +`Enter [9] :75` + +` Printing elements of the array :` +`12` +`33` +`54` +`76` +`123` +`33` +`98` +`45` +`66` +`75` + + +# PROGRAM NO 18:-PROGRAM TO PRINT A MATRIX + +`#include ` +`int main ()` +`{` +`int i,j,m,n;` +`int matrix[10][20];` +`printf ("Enter the number of rows:");` +`scanf ("%d",&m);` +`printf ("Enter the number of columns:");` +`scanf ("%d",&n);` + +`for (i=0;i` +`int main ()` +`{` +`int i,j,a[10][10],b[10][10],sum[10][10],m,n; ` +`printf ("Enter the number of Rows\n Enter the number of Columns:");` +`scanf ("%d %d",&m,&n);` +`for (i=0;i` + +`void main()` +`{` + ` static int array[10][10];` + `int i, j, m, n;` + + ` printf("Enter the order of the matrix \n");` + `scanf("%d %d", &m, &n);` + ` printf("Enter the coefiicients of the matrix\n");` + `for (i = 0; i < m; ++i)` + ` {` + ` for (j = 0; j < n; ++j)` + ` {` + ` scanf("%d", &array[i][j]);` + `}` + ` }` + ` printf("The given matrix is \n");` + ` for (i = 0; i < m; ++i)` + ` {` + `for (j = 0; j < n; ++j)` + ` {` + ` printf(" %d", array[i][j]);` + ` }` + `printf("\n");` + ` } + ` printf("Transpose of matrix is \n"); ` + `for (j = 0; j < n; ++j)` + `{` + ` for (i = 0; i < m; ++i)` + ` {` + ` printf(" %d", array[i][j]);` + ` }` + ` printf("\n");` + ` }` +`}` + +## OUTPUT + +`Enter the order of the matrix` +`3 3` +`Enter the coefiicients of the matrix` +`3 7 9` +`2 7 5` +`6 3 4` +`The given matrix is` +` 3 7 9` +` 2 7 5` +` 6 3 4` +`Transpose of matrix is` + `3 2 6` + `7 7 3` +` 9 5 4` + +# PROGRAM NO 21:-PROGRAM TO SUBTRACT TWO MATRIX + +`#include ` +`int main ()` +`{` +`int i,j,a[10][10],b[10][10],minus[10][10],m,n;` +`printf("Enter the number of Rows :");` +`scanf ("%d",&m);` +`printf ("Enter the number of columns :");` +`scanf ("%d",&n);` +`for (i=0 ; i` +`void main()` +`{` + ` int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;` + `int sum=0;` + ` printf("Enter number of rows and columns of first matrix (MAX 10)\n");` + ` scanf("%d%d",&r1,&c1);` + `printf("Enter number of rows and columns of second matrix MAX 10)\n");` + `scanf("%d%d",&r2,&c2);` + `if(r2==c1)`` + `{` + + `printf("\n Enter First Matrix:");` + `for(i=0; i +int main() { + +puts("**************"); +puts("Manraj Singh Sidhu"); +puts("**************"); + +return 0; +} +Output - [Imgur](https://i.imgur.com/qZEQBRX.png) + + +## 2) To print College address + +/* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +Output - [Imgur](https://i.imgur.com/waOuMoG.png) + +## 3) Program to add two integers . + + +/* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +Output - [Imgur](https://i.imgur.com/oJ4wa3d.png) + +## 4) Program to find quotient and remainder. + +/* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +Output - [Imgur](https://i.imgur.com/SrxIhz5.png) + +## 5) Program to swap two variables without 3rd variable. + + +/* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +Output - [Imgur](https://i.imgur.com/O53bIpB.png) + +## 6) Program to check even odd number. + +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +[Imgur](https://i.imgur.com/RFdvAIQ.png) + + +## 7) Finding greteast of two numbers. + + +/* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +[Imgur](https://i.imgur.com/wx3aFic.png) + +## 8) Find greatest of three number . + + +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +[Imgur](https://i.imgur.com/asdFYSZ.png) + +## 9) Program to assign grade to student according to percentage. + + +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +[Imgur](https://i.imgur.com/slsegPW.png) + +## 10) Program to print roots of quadratic equation. + +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +[Imgur](https://i.imgur.com/pFy85H6.png) + +## 11) Program to check year is leap or not. + + +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} + +[Imgur](https://i.imgur.com/znxvVdI.png) + + +## 12) Program to print table of 5. + +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +[Imgur](https://i.imgur.com/hCFa8x6.png) + +## 13) To make simple calculator using switch case. + + +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +[Imgur](https://i.imgur.com/ew5R3hg.png) +## 14) To calculate reverse of a number. + + +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} + +[Imgur](https://i.imgur.com/iwEyVUN.png) + +## 15) To check whether number is palindrome or not. + + +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +[Imgur](https://i.imgur.com/wRSFFHL.png) + +### 16) To check whether a number is prime or not. + + +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +[Imgur](https://i.imgur.com/RCz5NI1.png) + + +### 17) Program to print prime number to 100. + +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + [Imgur](https://i.imgur.com/BOl6lY4.png) + + +## 18) Program to check whether a number is armstrong or not. + +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} +[Imgur](https://i.imgur.com/RdK5PCD.png) + + +## 19) Print Different Patterns. + +#### i) + +/* Pattern 1*/ + +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} +[Imgur](https://i.imgur.com/MMfL4q6.png) + + +#### ii) + + /* Pattern 2*/ + +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} + + + +### 20) Program to find largest from 1 dimensional array. + + + /* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +[Imgur](https://i.imgur.com/bLdUOyC.png) + +### 21) To find sum of the N natural numbers in an array. + +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} + [Imgur](https://i.imgur.com/XJE4bBo.png) + +### 22) Program to add two matrices . + +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +[Imgur](https://i.imgur.com/vDqsRXG.png) + +## 23) Program to multiply two matrices . + + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +[Imgur](https://i.imgur.com/6oSstKT.png) + + +## 26) Programs to swap two numbers using call by value and call by refernce. + +#### call by refernce:- + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +[Imgur](https://i.imgur.com/5CbaPFO.png) +#### call by value:- + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} + [Imgur](https://i.imgur.com/5CbaPFO.png) + + +## 27) Program to calculate factorial of a number with and without recursion both. + + +#### Recursion:- + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +[Imgur](https://i.imgur.com/qnepEv2.png) +#### without recrsion:- + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} + +[Imgur](https://i.imgur.com/qnepEv2.png) + +## 28) Program to print fibonacci series with and without recursion both. + +#### Without recursion:- + +/* Program to print fibonaci series */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +[Imgur](https://i.imgur.com/w4d35Op.png) + +#### with recursion:- + + +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} + + + +## 29) Program to calculate average of 5 numbers using function. + + + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + + [Imgur](https://i.imgur.com/Wh32Ccp.png) + +## 30) Program to implement linear serach and binary. + + +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } +; else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +[Imgur](https://i.imgur.com/mpOdXxk.png) + + +## 31) Program to implement bubble sort. + + + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +[Imgur](https://i.imgur.com/aTnRlyN.png) + + +## 32) Program to store information of 10 students using array of structures. + + + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +[Imgur](https://i.imgur.com/C82LkLp.png) + + +## 33) Programs to compute the transpose of a matrix. + + +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} +[Imgur](https://i.imgur.com/d8kpNGx.png) + +## 35) Program to access array using pointer. + +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +[Imgur](https://i.imgur.com/cfxdXbE.png) diff --git a/1915332-master/Images/02.jpg b/1915332-master/Images/02.jpg new file mode 100644 index 0000000..f87ae11 Binary files /dev/null and b/1915332-master/Images/02.jpg differ diff --git a/1915332-master/Images/03.jpg b/1915332-master/Images/03.jpg new file mode 100644 index 0000000..31507c0 Binary files /dev/null and b/1915332-master/Images/03.jpg differ diff --git a/1915332-master/Images/04.jpg b/1915332-master/Images/04.jpg new file mode 100644 index 0000000..e5fb3c4 Binary files /dev/null and b/1915332-master/Images/04.jpg differ diff --git a/1915332-master/Images/05.jpg b/1915332-master/Images/05.jpg new file mode 100644 index 0000000..74047e5 Binary files /dev/null and b/1915332-master/Images/05.jpg differ diff --git a/1915332-master/Images/06.jpg b/1915332-master/Images/06.jpg new file mode 100644 index 0000000..b0ef35e Binary files /dev/null and b/1915332-master/Images/06.jpg differ diff --git a/1915332-master/Images/07.jpg b/1915332-master/Images/07.jpg new file mode 100644 index 0000000..586fb5b Binary files /dev/null and b/1915332-master/Images/07.jpg differ diff --git a/1915332-master/Images/08.jpg b/1915332-master/Images/08.jpg new file mode 100644 index 0000000..a814fa8 Binary files /dev/null and b/1915332-master/Images/08.jpg differ diff --git a/1915332-master/Images/09.jpg b/1915332-master/Images/09.jpg new file mode 100644 index 0000000..9d70be9 Binary files /dev/null and b/1915332-master/Images/09.jpg differ diff --git a/1915332-master/Images/1.png b/1915332-master/Images/1.png new file mode 100644 index 0000000..38028ef Binary files /dev/null and b/1915332-master/Images/1.png differ diff --git a/1915332-master/Images/10.jpg b/1915332-master/Images/10.jpg new file mode 100644 index 0000000..c40b5a7 Binary files /dev/null and b/1915332-master/Images/10.jpg differ diff --git a/1915332-master/Images/11.jpg b/1915332-master/Images/11.jpg new file mode 100644 index 0000000..4c1ad3a Binary files /dev/null and b/1915332-master/Images/11.jpg differ diff --git a/1915332-master/Images/12.jpg b/1915332-master/Images/12.jpg new file mode 100644 index 0000000..b2d4ca3 Binary files /dev/null and b/1915332-master/Images/12.jpg differ diff --git a/1915332-master/Images/13.jpg b/1915332-master/Images/13.jpg new file mode 100644 index 0000000..af5905e Binary files /dev/null and b/1915332-master/Images/13.jpg differ diff --git a/1915332-master/Images/14.jpg b/1915332-master/Images/14.jpg new file mode 100644 index 0000000..e6d9486 Binary files /dev/null and b/1915332-master/Images/14.jpg differ diff --git a/1915332-master/Images/15.jpg b/1915332-master/Images/15.jpg new file mode 100644 index 0000000..1beac98 Binary files /dev/null and b/1915332-master/Images/15.jpg differ diff --git a/1915332-master/Images/16.jpg b/1915332-master/Images/16.jpg new file mode 100644 index 0000000..d56e7e2 Binary files /dev/null and b/1915332-master/Images/16.jpg differ diff --git a/1915332-master/Images/17.jpg b/1915332-master/Images/17.jpg new file mode 100644 index 0000000..17134ed Binary files /dev/null and b/1915332-master/Images/17.jpg differ diff --git a/1915332-master/Images/18.jpg b/1915332-master/Images/18.jpg new file mode 100644 index 0000000..f2f6915 Binary files /dev/null and b/1915332-master/Images/18.jpg differ diff --git a/1915332-master/Images/19a.jpg b/1915332-master/Images/19a.jpg new file mode 100644 index 0000000..8a17aad Binary files /dev/null and b/1915332-master/Images/19a.jpg differ diff --git a/1915332-master/Images/19b.jpg b/1915332-master/Images/19b.jpg new file mode 100644 index 0000000..5d1a336 Binary files /dev/null and b/1915332-master/Images/19b.jpg differ diff --git a/1915332-master/Images/19c.jpg b/1915332-master/Images/19c.jpg new file mode 100644 index 0000000..a8030f6 Binary files /dev/null and b/1915332-master/Images/19c.jpg differ diff --git a/1915332-master/Images/20.jpg b/1915332-master/Images/20.jpg new file mode 100644 index 0000000..cca6724 Binary files /dev/null and b/1915332-master/Images/20.jpg differ diff --git a/1915332-master/Images/21.jpg b/1915332-master/Images/21.jpg new file mode 100644 index 0000000..ae6ee65 Binary files /dev/null and b/1915332-master/Images/21.jpg differ diff --git a/1915332-master/Images/22.jpg b/1915332-master/Images/22.jpg new file mode 100644 index 0000000..5d43419 Binary files /dev/null and b/1915332-master/Images/22.jpg differ diff --git a/1915332-master/Images/23.jpg b/1915332-master/Images/23.jpg new file mode 100644 index 0000000..77586a9 Binary files /dev/null and b/1915332-master/Images/23.jpg differ diff --git a/1915332-master/Images/24.jpg b/1915332-master/Images/24.jpg new file mode 100644 index 0000000..c7a14b2 Binary files /dev/null and b/1915332-master/Images/24.jpg differ diff --git a/1915332-master/Images/25.jpg b/1915332-master/Images/25.jpg new file mode 100644 index 0000000..a859102 Binary files /dev/null and b/1915332-master/Images/25.jpg differ diff --git a/1915332-master/Images/26.jpg b/1915332-master/Images/26.jpg new file mode 100644 index 0000000..5f4cc34 Binary files /dev/null and b/1915332-master/Images/26.jpg differ diff --git a/1915332-master/Images/26b.jpg b/1915332-master/Images/26b.jpg new file mode 100644 index 0000000..e68aae7 Binary files /dev/null and b/1915332-master/Images/26b.jpg differ diff --git a/1915332-master/Images/27a.jpg b/1915332-master/Images/27a.jpg new file mode 100644 index 0000000..52a7ad5 Binary files /dev/null and b/1915332-master/Images/27a.jpg differ diff --git a/1915332-master/Images/27b.jpg b/1915332-master/Images/27b.jpg new file mode 100644 index 0000000..9bcdcc0 Binary files /dev/null and b/1915332-master/Images/27b.jpg differ diff --git a/1915332-master/Images/28a.jpg b/1915332-master/Images/28a.jpg new file mode 100644 index 0000000..0eb4f17 Binary files /dev/null and b/1915332-master/Images/28a.jpg differ diff --git a/1915332-master/Images/28b.jpg b/1915332-master/Images/28b.jpg new file mode 100644 index 0000000..371fe18 Binary files /dev/null and b/1915332-master/Images/28b.jpg differ diff --git a/1915332-master/Images/29.jpg b/1915332-master/Images/29.jpg new file mode 100644 index 0000000..cbeb817 Binary files /dev/null and b/1915332-master/Images/29.jpg differ diff --git a/1915332-master/Images/30a.jpg b/1915332-master/Images/30a.jpg new file mode 100644 index 0000000..1e2d4d4 Binary files /dev/null and b/1915332-master/Images/30a.jpg differ diff --git a/1915332-master/Images/30b.jpg b/1915332-master/Images/30b.jpg new file mode 100644 index 0000000..4f57c70 Binary files /dev/null and b/1915332-master/Images/30b.jpg differ diff --git a/1915332-master/Images/31.jpg b/1915332-master/Images/31.jpg new file mode 100644 index 0000000..9a2273b Binary files /dev/null and b/1915332-master/Images/31.jpg differ diff --git a/1915332-master/Images/32.jpg b/1915332-master/Images/32.jpg new file mode 100644 index 0000000..271581d Binary files /dev/null and b/1915332-master/Images/32.jpg differ diff --git a/1915332-master/Images/32_cont.jpg b/1915332-master/Images/32_cont.jpg new file mode 100644 index 0000000..83fb6da Binary files /dev/null and b/1915332-master/Images/32_cont.jpg differ diff --git a/1915332-master/Images/33.jpg b/1915332-master/Images/33.jpg new file mode 100644 index 0000000..04e8362 Binary files /dev/null and b/1915332-master/Images/33.jpg differ diff --git a/1915332-master/Images/34.jpg b/1915332-master/Images/34.jpg new file mode 100644 index 0000000..33cc94e Binary files /dev/null and b/1915332-master/Images/34.jpg differ diff --git a/1915332-master/Images/35.jpg b/1915332-master/Images/35.jpg new file mode 100644 index 0000000..283141e Binary files /dev/null and b/1915332-master/Images/35.jpg differ diff --git a/1915328-konarklohat.md b/1915332-master/PPSmoksh1915332.md similarity index 82% rename from 1915328-konarklohat.md rename to 1915332-master/PPSmoksh1915332.md index d115764..bce6a8b 100644 --- a/1915328-konarklohat.md +++ b/1915332-master/PPSmoksh1915332.md @@ -1,8 +1,9 @@ + ![College Logo](https://www.gndec.ac.in/logo.png) # **Programming for Problem Solving** -## **Name:- Konark Lohat** -## **CRN:-1915328** +## **Name:- MOKSH SOOD** +## **CRN:-1915332** ## **Branch:- CSE-C1** ## **Submitted To:- Ms. Goldendeep Kaur** --- @@ -13,12 +14,12 @@ int main() { puts("~~~~~~~~~~~~~~~~~~~~~~~~~"); - puts("My name is Konark Lohat"); + puts("My name is MOKSH SOOD"); puts("~~~~~~~~~~~~~~~~~~~~~~~~~"); return 0; } ``` -![1.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/01.JPG) +![1.jpg](1.png) ### 2) To print College address. ```C #include @@ -28,7 +29,7 @@ int main() return 0; } ``` -![2.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/02.JPG) +![2.jpg](02.jpg) ### 3) Program to add two integers. ```C #include @@ -44,7 +45,7 @@ int main() return 0; } ``` -![3.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/03.JPG) +![3.jpg](03.jpg) ### 4) Program to find quotient and remainder. ```C #include @@ -62,7 +63,7 @@ int main() return 0; } ``` -![4.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/04.JPG) +![4.jpg](04.jpg) ### 5) Program to swap two variables without 3rd variable. ```C #include @@ -81,7 +82,7 @@ int main() return 0; } ``` -![5.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/05.JPG) +![5.jpg](05.jpg) ### 6) Program to check even odd number. ```C #include @@ -97,7 +98,7 @@ int main() return 0; } ``` -![6.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/06.JPG) +![6.png](06.jpg) ### 7) Finding greteast of two numbers. ```C #include @@ -115,7 +116,7 @@ int main() return 0; } ``` -![7.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/07.JPG) +![7.png](06.jpg) ### 8) Find greatest of three number . ```C #include @@ -147,7 +148,7 @@ int main() } ``` -![8.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/08.JPG) +![8.png](08.jpg) ### 9) Program to assign grade to student according to percentage. ```C #include @@ -189,7 +190,7 @@ int main() return 0; } ``` -![9.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/09.JPG) +![9.png](09.jpg) ### 10) Program to print roots of quadratic equation. ```C #include @@ -210,7 +211,7 @@ int main() return 0; } ``` -![10.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/10.JPG) +![10.png](10.jpg) ### 11) Program to check year is leap or not. ```C #include @@ -226,7 +227,7 @@ int main() return 0; } ``` -![11.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/11.JPG) +![11.png](11.jpg) ### 12) Program to print table of 5. ```C #include @@ -242,7 +243,7 @@ int main() return 0; } ``` -![12.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/12.JPG) +![12.png](12.jpg) ### 13) To make simple calculator using switch case. ```C #include @@ -275,7 +276,7 @@ int main() return 0; } ``` -![13.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/13.JPG) +![13.png](13.jpg) ### 14) To calculate reverse of a number. ```C #include @@ -294,7 +295,7 @@ int main() return 0; } ``` -![14.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/14.JPG) +![14.png](14.jpg) ### 15) To check whether number is palindrome or not. ```C #include @@ -317,7 +318,7 @@ int main() return 0; } ``` -![15.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/15.JPG) +![15.png](15.jpg) ### 16) To check whether a number is prime or not. ```C #include @@ -344,7 +345,7 @@ int main() } } ``` -![16.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/16.JPG) +![16.png](16.jpg) ### 17) Program to print prime numbers from 1 to 100. ```C #include @@ -373,7 +374,7 @@ int main() return 0; } ``` - ![17.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/17.JPG) + ![17.png](17.jpg) ### 18) Program to check whether a number is armstrong or not. ```C #include @@ -397,7 +398,7 @@ int main() } ``` -![18.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/18.JPG) +![18.png](18.jpg) ### 19) Print the following patterns: ### i) Pattern 1. ```C @@ -416,7 +417,7 @@ int main() return 0; } ``` -![19_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19a.JPG) +![19_1.png](19a.jpg) ### ii) Pattern 2. ```C @@ -435,7 +436,7 @@ int main() return 0; } ``` -![19_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19b.JPG) +![19_2.png](19b.jpg) ### iii) Pattern 3. ```C @@ -462,7 +463,7 @@ int main() return 0; } ``` -![19_3.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19c.JPG) +![19_3.png](19c.jpg) ### 20) Program to find largest from 1 dimensional array. ```C #include @@ -483,7 +484,7 @@ int main() return 0; } ``` -![20.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/20.JPG) +![20.png](20.jpg) ### 21) To find sumof the N natural numbers in an array. ```C @@ -506,7 +507,7 @@ int main() return 0; } ``` -![21.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/21.JPG) +![21.png](21.jpg) ### 22) Program to add two matrices . ```C @@ -540,7 +541,7 @@ int main() return 0; } ``` -![22.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/22.JPG) +![22.png](22.jpg) ### 23) Program to multiply two matrices . ```C #include @@ -576,7 +577,7 @@ int main() return 0; } ``` -![23.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/23.JPG) +![23.png](23.jpg) ### 24) Program to check whether a string is palindrome or not . ```C #include @@ -601,7 +602,7 @@ int main() return 0; } ``` -![24.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/24.JPG) +![24.png](24.jpg) ### 25) Programs to perform basic operations like length of string, string concatenation, sting copy, string compare and string reverse. ```C #include @@ -624,7 +625,7 @@ int main() return 0; } ``` -![25.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/25.JPG) +![25.jpg](25.jpg) ### 26) Programs to swap two numbers using call by value and call by refernce. ### Call by reference @@ -659,7 +660,7 @@ void swap(int *a, int *b) *a = temp; } ``` -![26_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/26.JPG) +![26_1.png](26.jpg) ### call by value:- ```C /* Call by value */ @@ -693,7 +694,7 @@ void swap(int a, int b) { printf("Values of a and b is %d %d\n",a,b); } ``` -![26_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/26b.JPG) +![26_2.png](20b.jpg) ### 27) Program to calculate factorial of a number with and without recursion both. ```C @@ -717,7 +718,7 @@ long long int factorial(long long int x) return 1; } ``` -![27_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/27a.JPG) +![27_1.png](27a.jpg) ```C #include @@ -734,7 +735,7 @@ int main() return 0; } ``` -![27_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/27b.JPG) +![27_2.png](27b.jpg) ### 28) Program to print fibonacci series with and without recursion both. ```C @@ -761,7 +762,7 @@ int fib(int j) return (fib(j-1) + fib(j-2)); } ``` -![28_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/28a.JPG) +![28_1.png](28a.jpg) ```C #include int main() @@ -776,7 +777,7 @@ int main() } } ``` -![28_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/28b.JPG) +![28_2.png](28b.jpg) ### 29) Program to calculate average of 5 numbers using function. ```C @@ -795,7 +796,7 @@ int average(int a, int b, int c, int d, int e) return ((a+b+c+d+e)/5); } ``` -![29.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/29.JPG) +![29.png](29.jpg) ### 30) Program to implement linear serach and binary. ### Linear Search Program @@ -825,7 +826,7 @@ int main() return 0; } ``` -![30_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/30a.JPG) +![30_1.png](30a.jpg) ### Binary Search Program ```C #include @@ -864,7 +865,7 @@ int main() } ``` -![30_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/30b.JPG) +![30_2.png](30b.jpg) ### 31) Program to implement bubble sort. ```C @@ -903,7 +904,7 @@ int main() return 0; } ``` -![31.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/31.JPG) +![31.png](31.jpg) ### 32) Program to store information of 10 students using array of structures. ```C #include @@ -946,8 +947,8 @@ int main() return 0; } ``` -![32_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/32.JPG) -![32_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/32_cont.JPG) +![32_1.png](32.jpg) +![32_2.png](32_cont.jpg) ### 33) Programs to compute the transpose of a matrix. ```C #include @@ -972,7 +973,7 @@ int main() return 0; } ``` -![33.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/33.JPG) +![33.png](33.jpg) ### 34) Program to print the address of variable using pointer. ```C #include @@ -995,7 +996,7 @@ int main() { return 0; } ``` -![34.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/34.JPG) +![34.png](34.jpg) ### 35) Program to access array using pointer. ```C int main() @@ -1010,4 +1011,4 @@ int main() return 0; } ``` -![35.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/35.JPG) +![35.png](35.jpg) diff --git a/1915344.md b/1915344.md new file mode 100644 index 0000000..5ba21de --- /dev/null +++ b/1915344.md @@ -0,0 +1,682 @@ +# Practical + + # **PROGRAMMING FOR PROBLEM SOLVING ESC-18105** +## NAME- *Rahul Kumar* +## ROLL NO- *1915344* +## BRANCH- *COMPUTER SCIENCE & ENGINEERING* +## SECTION- *CS(C)* + + + + #### 1.To add two numbers + +#include +int main() +{ +int a,b,sum=0; +printf("Enter two numbers \n"); +scanf("%d%d",&a,&b); +sum=a+b; +printf("Sum of two numbers is %d \n",sum); +return 0; +} + +### Output +Enter two numbers +5 +10 +Sum of two numbers is 15 + + + + #### 2.To find average of n numbers + +#include +int main() +{ +int n,i,a[10]; +int sum=0,avg; +printf("Enter value of n : \n"); +scanf("%d",&n); +printf("Enter %d numbers: \n ",n); +for(i=0;i +int main() +{ +int c; +printf(" Enter the value of day"); +scanf("%d",&c); +switch (c) +{ +case 1: +printf("Monday \n"); +break; +case 2: +printf("Tuesday \n"); +break; +case 3: +printf("Wednesday \n"); +break; +case 4: +printf("Thursday \n"); +break; +case 5: +printf("Friday \n"); +break; +case 6: +printf("Saturday \n"); +break; +case 7: +printf("Sunday \n"); +break; +default : +printf("Wrong choice \n"); +} +return 0; +} + +#### Output +Enter the value of day +5 +Friday + + #### 4. To check whether a number is odd or even + +#include +int main() +{ +int number; +printf("Enter any number"); +scanf("%d",&number); +if(number%2==0) +printf("Number is even"); +else +printf("Number is odd"); +return 0; +} + +#### Output +Enter any number +5 +Number is odd + + #### 5. To print the table of 2 + +#include +int main() +{ +int i,a=2,b=1; +printf("The table of 2 :"); +printf("\n ============ \n"); +for(i=1;i<=10;i++) +{ +b=2*i; +printf("%d * %d = %d \n",a,i,b); +} +return 0; +} + +#### Output + The table of 2 + ---------------- + ---------------- + 2 * 1 = 2 + 2 * 2 = 4 + 2 * 3 = 6 + 2 * 4 = 8 + 2 * 5 = 10 + 2 * 6 = 12 + 2 * 7 = 14 + 2 * 8 = 16 + 2 * 9 = 18 + 2 * 10 = 20 + +#### 6. To check whether a number is armstrong or not + +#include +int main() +{ +int number,temp; +int arm=0,digit; +printf("Enter any number"); +scanf("%d",&number); +temp=number; +while(temp>0) +{ +digit=temp%10; +arm=arm+(digit * digit * digit); +temp/=10; +} +if(arm==number) +printf("Armstrong\n"); +else +printf("Not armstrong\n"); +return 0; +} + +#### Output +Enter any number +153 +Armstrong + + + #### 7. To print calculator + + +#include +void main() +{ +puts(" _____________________ "); +puts("| _____________________ |\n"); +puts("| 1 | 2 | 3 |\t|\n"); +puts("| ___ | ___ | ___ |\t|\n"); +puts("| 4 | 5 | 6 | + |\n"); +puts("| ___ | ___ | ___ | ___ |\n"); +puts("| 7 | 8 | 9 | - |\n"); +puts("| ___ | ___ | ___ | ___ |\n"); +puts("|\t0\t | * |\n"); +puts("| _______________ | ___ |\n"); +} + + +#### 8.Bubble Sort + +#include +int main() +{ +int a[20],i,n,k,temp; +printf("Enter size of array\n"); +scanf("%d",&n); +printf("Enter %d elements of array:\n",n); +for(i=0;ia[i+1]) +{ +temp=a[i]; +a[i]=a[i+1]; +a[i+1]=temp; +} +} +} +printf("Array elements after sorting \n"); +for(i=0;i +int main() +{ +int i,a[10],n,lb=0,ub=9,mp; +printf("Enter 10 numbers in ascending order \n"); +for(i=0;i<10;i++) +{ +scanf("%d",&a[i]); +} +printf("Enter number to be searched \n "); +scanf("%d",&n); +mp=((lb+ub)/2); +while(lb<=ub) +{ +if(a[mp]==n) +{ +printf("Number is found\n"); +break; +} +else if(a[mp]>n) +{ +ub=mp-1; +} +else +{ +lb=mp+1; +} +mp=((lb+ub)/2); +} +if(lb>ub) +{ +printf("Number not found "); +} +return 0; +} + + + #### 10.To find factorial of a number + +#include +int main() +{ +int i,n,fact=1; +printf("Enter the number whose factorial is to be find :\n"); +scanf("%d",&n); +for(i=1;i<=n;i++) +{ +fact=fact*i; +} +printf("Factorial of given number is : %d \n",fact); +return 0; +} + + + #### 11.Fizz Buzz + +#include +int main() +{ +int n; +for(n=1;n<=30;n++) +{ +if(n%3==0 && n%5==0) +printf("FizzBuzz"); +else if(n%3==0) +printf("Fizz \n"); +else if(n%5==0) +printf("Buzz \n"); +else +printf(" \n %d \n ",n); +} +return 0; +} + + +#### 12.To find sum of first 100 numbers + +#include +int main() +{ +int i,sum=0; +for(i=1;i<=100;i++) +{ +sum=sum+i; +} +printf("Sum of first 100 numbers is %d \n",sum); +return 0; +} + + + #### 13. To find greater of two numbers + +#include +int main() +{ +int a,b; +printf("Enter the value of a and b"); +scanf("%d%d",&a,&b); +if(a>b) +{ +printf("a is grteater"); +} +else +{ +printf("b is greater"); +} +return 0; +} + + #### 14.To find greatest of 3 numbers + +#include +int main() +{ +int a,b,c; +printf("Enter the three numbers to check greatest \n"); +scanf("%d%d%d",&a,&b,&c); +if(a>b&&a>c) +{ +printf("1st number is greatest \n"); +} +else if(b>c&&b>a) +{ +printf("2nd number is greatest \n"); +} +else +{ +printf("3rd number is greatest \n"); +} +return 0; +} + + + #### 15. To find gcd of two numbers + +#include +int main() +{ +int m,n,r=1; +printf("Enter two numbers to find gcd \n"); +scanf("%d%d",&m,&n); + while(r!=0) +{ +r=m%n; +m=n; +n=r; +} +printf("\n GCD =%d\n ",m); +return 0; +} + + +#### 16.To check whether a year is leap or not + +#include +int main() +{ +int year; +printf("Enter the value of year"); +scanf("%d",&year); +if(year%4==0) +printf(" The year is Leap \n"); +else +printf(" \n The year is not leap \n"); +return 0; +} + + + #### 17. Linear search + +#include +int main() +{ +int a[5],i,s,f; +printf("Enter 5 numbers\n"); +for(i=0;i<5;i++) +{ +scanf("%d",&a[i]); +} +printf("Enter number to be searched\n"); +scanf("%d",&s); +for(i=0;i<5;i++) +{ +if(s==a[i]) +{ +f=1; +break; +} +} +if(f==1) +printf("Number is found \n"); +else +printf("Number is not found \n"); +return 0; +} + + +#### 18.To find sum of two matrices + +#include +int main() +{ +int m,n,a[5][5],b[5][5],c[5][5],i,j; +printf("Enter the size of matrix"); +scanf("%d%d",&m,&n); +printf("Enter the elements of matrix A\n"); +for(i=0;i +int main() +{ +int a[10][10],b[10][10],m,n,i,j; +printf("Enter the size of matrix A as mand n:\n"); +scanf("%d%d",&m,&n); +printf("Enter the elements of matrix A\n"); +for(i=0;i +int main() +{ +int sum=0,digit; +int n,temp; +printf("Enter the number to cal. the sum of digits"); +scanf("%d",&n); +temp=n; +while(temp>0) +{ +digit=temp%10; +temp/=10; +sum+=digit; +} +printf("\n Sum of digits of %d = %d \n",n,sum); +return 0; +} + + + #### 21. To check whether a number is palindrome or not + +#include +int main() +{ +int sum=0,digit; +int temp,n; +printf("Enter any number"); +scanf("%d",&n); +temp=n; +while(temp>0) +{ +digit=temp%10; +temp/=10; +sum=sum*10+digit; +} +if(n==sum) +printf("\n%d is a palindrome\n",n); +else +printf("\n%d is not a palindrome\n",n); +return 0; +} + + #### 22.To swap two numbers using call by value method + +#include +void swap(int a,int b); //Function declaration + +void main() +{ +int x,y; +printf("\n Enter value for x: "); +scanf("%d",&x); +printf("\n Enter value for y: "); +scanf("%d",&y); + +printf("\n Before calling swap function\n"); +printf("\n Value of x = %d, value of y = %d\n",x,y); + +swap(x,y); //Function call, with only values of x & y + +printf("\n After returning from swap function"); +printf("\n Value of x = %d, value of y = %d\n",x,y); +} + +void swap(int a,int b) +{ +int temp; +printf("\n Inside the function\n"); +printf("\n Value of a = %d, value of b = %d before swap\n",a,b); +temp = a; +a = b; +b = temp; +printf("\n Value of a = %d, value of b = %d after swap\n",a,b); +} + + + + #### 23. To swap two numbers using call by reference + +#include +void swap( int * , int * ); //Function declaration + +void main() +{ +int x,y; +printf("\nEnter value for x: "); +scanf("%d",&x); +printf("\nEnter value for y: "); +scanf("%d",&y); +printf("\nBefore calling swap function\n"); +printf("\nValue of x=%d, value of y=%d\n",x,y); + +swap(&x,&y); //Function call, with addresses of x & y + +printf("\nAfter returning from swap function\n"); +printf("\nValue of x=%d, value of y=%d\n",x,y); +} +void swap( int * a , int * b ) +{ //Function definition +int temp; +printf("\nInside the function\n"); +printf("\nValue of *a=%d, value of *b=%d before swap\n",*a,*b); +temp = *a; +*a = *b; +*b = temp; +printf("\nValue of *a=%d, value of *b=%d after swap\n",*a,*b); +} + + +#### 24. To enter details of employee using structure + +#include +#include +struct employee +{ +int code; +char name[25],department[15]; +float salary; +}; +void main() +{ +struct employee aemployee; +printf("Enter employee code \n"); +scanf("%d",&aemployee.code); +printf("Enter employee's name \n"); +scanf("%s",&aemployee.name); +printf("Enter employee's department \n"); +scanf("%s",&aemployee.department); +printf("Enter employee salary \n"); +scanf("%f",&aemployee.salary); +printf("Particulars of employee are :\n"); +printf("\n Employee's code %d",aemployee.code); +printf("\n Employee's name %s",aemployee.name); +printf("\n Employee's department %s",aemployee.department); +printf("\n Employee salary %2f \n ",aemployee.salary); +} + + + +#### 25.To print product of two fraction using structures + +#include + +struct fr +{ +float num; +float denom; +}; + +int main() +{ +struct fr f1,f2,res; + +printf("Enter numerator,denominator of 1st fraction: "); +scanf("%f/%f",&f1.num,&f1.denom); +printf("Enter numerator,denominator of 2nd fraction: "); +scanf("%f/%f",&f2.num,&f2.denom); + +res.num=f1.num * f2.num; +res.denom=f1.denom * f2.denom; + +printf("The resultant product fraction is %.2f/%.2f.\n",res.num,res.denom); + +return 0; +} + + + + +#### By Rahul Kumar diff --git a/1915359.md b/1915359.md new file mode 100644 index 0000000..d23a727 --- /dev/null +++ b/1915359.md @@ -0,0 +1,1359 @@ +![](https://ce.gndec.ac.in/sites/default/files/logo.jpg) + +____ +# ***PROGRAMMING FOR PROBLEM SOLVING*** +# **NAME** :*ASHISH RANJAN SINGH* +### **ROLL NO** : *1915359* +### **BRANCH** : *COMPUTER SCIENCE* + ### **SECTION** : *C2* +____ +### 1) To print your name using puts. +```C +/* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Ashish Ranjan singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + diff --git a/1915366.md b/1915366.md new file mode 100644 index 0000000..32d37ca --- /dev/null +++ b/1915366.md @@ -0,0 +1,1360 @@ +![](https://ce.gndec.ac.in/sites/default/files/logo.jpg) + +____ +# ***PROGRAMMING FOR PROBLEM SOLVING*** +# **NAME** :*YOGITA SHARMA* +### **ROLL NO** : *1915366* +### **BRANCH** : *COMPUTER SCIENCE* + ### **SECTION** : *C2* +____ +### 1)To print your name using puts. +```c + + /* Program to print your name */ + +#include +int main() +{ +puts("*********** "); +puts("YOGITA SHARMA"); +puts("***********"); +return o; + +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + diff --git a/Hemangi Jain/PPS.md b/Hemangi Jain/PPS.md new file mode 100644 index 0000000..2d5444b --- /dev/null +++ b/Hemangi Jain/PPS.md @@ -0,0 +1,1150 @@ + +# PROGRAMMING FOR PROBLEM SOLVING. + +## Name :- Hemangi Jain +## Branch & Class-group :- B-TECH CSE C-C1 +## College Roll number :- 1915319 +## University Roll number :- 1905792 + +## Submitted to: +### - Ms Goldendeep Kaur + +## 1. To Print your name using puts(): + +```C +#include +int main(){ + puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); + puts("My Name is Hemangi Jain\n"); + puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); + return 0; +} +``` +![](/images/img1.JPG) +--- +## 2. Program to Print College Address in muliple lines: + +```C +#include +int main(){ + printf("Guru Nanak Dev Engineering College,\n"); + printf("Gill Road,\n"); + printf("Ludhiana, Punjab\n"); + return 0; +} +``` +![](/images/2.jpg) +--- +## 3. Addition of two numbers input by user: + +```C +#include +int main(){ + int n1,n2,sum; + printf("Enter 1st number :- "); + scanf("%d",&n1); + printf("Enter 2nd number :- "); + scanf("%d",&n2); + sum=n1+n2; + printf("Their Sum is %d\n",sum); + return 0; +} +``` +![](/images/3.jpg) +--- +## 4. Program to compute quotient and remainder: + +```C +#include +int main(){ + int d1,d2,q1,r1; + printf("enter dividend :- "); + scanf("%d",&d1); + printf("enter divisor :- "); + scanf("%d",&d2); + r1=d1%d2; + q1=d1/d2; + printf("Quotient is %d\n",q1); + printf("Remainder is %d",r1); + return 0; +} +``` +![](/images/4.jpg) +--- +## 5. Program to swap two numbers without third variable: + +```C +#include +int main(){ + int a,b; + printf("Enter 1st number ('a') :- "); + scanf("%d",&a); + printf("Enter 2nd number ('b') :- "); + scanf("%d",&b); + a=a+b; + b=a-b; + a=a-b; + printf("a = %d,b = %d",a,b); + return 0; +} +``` +![](/images/5.jpg) +--- +## 6. Program to check if a input number is even or odd: + +```C +#include +int main(){ + int n1; + printf("Enter number :- "); + scanf("%d",&n1); + if(n1%2==0){ + printf("Number is even"); + } + else{ + printf("Number is odd"); + } + return 0; +} +``` +![](/images/6.jpg) +--- +## 7. Program to find greatest of two numbers: + +```C +#include +int main(){ + int a,b; + printf("Enter 1st number ('a') :- "); + scanf("%d",&a); + printf("Enter 2nd number ('b') :- "); + scanf("%d",&b); + if(a>b){ + printf("A is greater"); + } + else if(b>a){ + printf("B is greater"); + } + else{ + printf("A and B are equal"); + } + return 0; +} +``` +![](/images/7.jpg) +--- +## 8. Program to find greatest of three entered numbers: + +```C +#include +int main(){ + int a,b,c; + printf("Enter 1st number ('a') :- "); + scanf("%d",&a); + printf("Enter 2nd number ('b') :- "); + scanf("%d",&b); + printf("Enter 3rd number ('c') :- "); + scanf("%d",&c); + if(a>b && a>c){ + printf("A is greatest"); + } + else if(b>c){ + printf("B is greatest"); + } + else{ + printf("C is greatest"); + } + return 0; +} +``` +![](/images/8.jpg) +--- +## 9. To print grade of a student by entering marks: + +```C +#include +int main(){ + int marks; + printf("Enter Marks of Student out of 100 :- "); + scanf("%d",&marks); + if(marks>=95){ + printf("O grade"); + } + else if(marks<95 && marks>=90){ + printf("A Grade"); + } + else if(marks<90 && marks>=80){ + printf("B Grade"); + } + else if(marks<80 && marks>=70){ + printf("C Grade"); + } + else if(marks<70 && marks>=60){ + printf("D Grade"); + } + else if(marks<60 && marks>=50){ + printf("E Grade"); + } + else if(marks<50 && marks>=40){ + printf("E- Grade"); + } + else{ + printf("F Grade"); + } + return 0; +} +``` +![](/images/9.jpg) +--- +## 10. Program To find roots of entered quadratic equation: +```C +#include +#include +int main(){ + int a,b,c,d,z; + float r1,r2; + printf("Quadratic equation is in the form of [ ax^2 + bx + c ]\n"); + printf("Enter value of a :- "); + scanf("%d",&a); + printf("Enter value of b :- "); + scanf("%d",&b); + printf("Enter value of c :- "); + scanf("%d",&c); + d = (b * b) - 4*a*c; + z = pow(d,0.5); + if(a==0){ + printf("equation is not quadratic equation"); + } + else if(d>=0){ + r1 = (-b+z)/2*a; + r2 = (-b-z)/2*a; + printf("Value of roots are %f and %f",r1,r2); + } + else{ + printf("roots are imaginary"); + } + return 0; +} +``` +![](/images/10.jpg) +--- +## 11. Program to check whether entered year is leap year: +```C +#include +int main() +{ + int year; + printf("Enter a year: "); + scanf("%d",&year); + if(year%4 == 0) + { + if( year%100 == 0) + { + // year is divisible by 400, hence the year is a leap year + if ( year%400 == 0) + printf("%d is a leap year.", year); + else + printf("%d is not a leap year.", year); + } + else + printf("%d is a leap year.", year ); + } + else + printf("%d is not a leap year.", year); + return 0; +} + +``` +![](/images/11.JPEG) +--- +## 12. Program to generate table of 5:- +```C +#include +int main(){ + int x; + for(x=0;x<=10;x++){ + printf("5 X %d = %d \n",x,5*x); + } + return 0; +} +``` +![](/images/12.jpg) +--- +## 13. To make simple calculator using switch statements: +```C +#include +int main() { + char Operator[1]; + float num1, num2, result; + printf("Enter the Values of first number:- "); + scanf("%f", &num1); + printf("Enter an Operator:- "); + scanf("%s", Operator); + printf("Enter the Values of second number:- "); + scanf("%f", &num2); + switch(Operator[0]){ + case '+': + result = num1 + num2; + printf("Result is %.1f",result); + break; + case '-': + result = num1 - num2; + printf("Result is %.1f",result); + break; + case '*': + result = num1 * num2; + printf("Result is %.1f",result); + break; + case '/': + result = num1 / num2; + printf("Result is %.1f",result); + break; + default: + printf("You have entered an Invalid Operator "); + } +return 0; +} +``` +![](/images/13.jpg) +--- +## 14. Program to calculate reverse of a number: +```C +#include +int main(){ + int n, rn, rem,n1; + rn=0; + printf("Enter a number:- "); + scanf("%d", &n); + n1=n; + while(n != 0){ + rem = n%10; + rn = rn*10 + rem; + n /= 10; + } + printf("Reversed Number = %d",rn); + return 0; +} +``` +![](/images/14.jpg) +--- +## 15. To check whether a number is palindrome or not: +```C +#include +int main(){ + int n, rn, rem,n1; + rn = 0; + printf("Enter a number :- "); + scanf("%d", &n); + n1 = n; + while(n != 0){ + rem = n%10; + rn = rn*10 + rem; + n /= 10; + } + if(rn == n1){ + printf("Number is Palindromic"); + } + else{ + printf("Number is not Palindromic"); + } + return 0; +} +``` +![](/images/15.jpg) +--- +## 16. To check whether a number is prime or not: +```C +#include +int main(){ + int a,i,x; + printf("Enter number :- "); + scanf("%d",&x); + a=0; + for(i=1;i<=x;i++){ + if (x%i==0){ + a=a+1; + } + } + if(a==2){ + printf("%d is a prime number",x); + } + else{ + printf("%d is not a prime number",x); + } + return 0; +} +``` +![](/images/16(i).jpg) +![](/images/16(ii).jpg) +--- +## 17. Program to print all prime numbers between 1 to 100: +```C +#include +int main(){ + int a,x,i; + for(x=1;x<=100;x++){ + a=0; + for(i=1;i<=x;i++){ + if (x%i==0){ + a=a+1; + } + } + if(a==2){ + printf("%d\n",x); + } + } + return 0; +} +``` +![](/images/17.jpg) +--- +## 18. To check whether a number is armstrong or not +```C +#include +int main(){ + int n,i,a,num=0; + printf("Enter Number :-\n"); + scanf("%d",&n); + a=n; + while(n!=0){ + i=n%10; + num=num+(i*i*i); + n/=10; + } + if(a==num){ + printf("Entered number is an armstrong number"); + } + else{ + printf("Entered number is not an armstrong number"); + } + return 0; +} +``` +![](/images/18.jpg) +--- +## 19. A program to generate given patterns +```C +#include +int main(){ + // ALL 3 PATTERNS ARE IN THIS FILE + +/********************************************************** +**********************************************************/ + printf("\n"); + printf("FIRST\n"); + printf("\n"); + + int i,j; + for(i=1;i<=4;i++){ + for(j=1;j<=4;j++){ + if(j==1){ + printf("1 "); + } + else if(j==2 && (i==2 || i==3 || i==4)){ + printf("2 "); + } + else if(j==3 && (i==3 || i==4)){ + printf("3 "); + } + else if(j==4 && i==4){ + printf("4 "); + } + } + printf("\n"); + } +/********************************************************** +**********************************************************/ + printf("\n"); + printf("SECOND\n"); + printf("\n"); + + int a,b; + for(a=1;a<=4;a++){ + for(b=1;b<=4;b++){ + if(a==1 && b==1){ + printf("1 "); + } + else if(a==2){ + if(b==1){ + printf("2 "); + } + else if(b==2){ + printf("3 "); + } + } + else if(a==3){ + if(b==1){ + printf("4 "); + } + else if(b==2){ + printf("5 "); + } + else if(b==3){ + printf("6 "); + } + } + else if(a==4){ + if(b==1){ + printf("7 "); + } + else if(b==2){ + printf("8 "); + } + else if(b==3){ + printf("9 "); + } + else if(b==4){ + printf("10 "); + } + } + } + printf("\n"); + } + +/************************************************************ +************************************************************/ + printf("\n"); + printf("THIRD\n"); + printf("\n"); + + int x,y; + for(x=1;x<=4;x++){ + for(y=1;y<=7;y++){ + if(x==1){ + if(y==4){ + printf("1 "); + } + else{ + printf(" "); + } + } + else if(x==2){ + if(y==3 || y==4 || y==5){ + if(y==3 || y==5){ + printf("1 "); + } + else if(y==4){ + printf("2 "); + } + } + else if(y==1 || y==2 || y==6 || y==7){ + printf(" "); + } + } + else if(x==3){ + if(y==4){ + printf("3 "); + } + else if(y==3 || y==5){ + printf("2 "); + } + else if(y==2 || y==6){ + printf("1 "); + } + else if(y==1 || y==7){ + printf(" "); + } + } + else if(x==4){ + if(y==4){ + printf("4 "); + } + else if(y==3 || y==5){ + printf("3 "); + } + else if(y==2 || y==6){ + printf("2 "); + } + else if(y==1 || y==7){ + printf("1 "); + } + } + } + printf("\n"); + } + return 0; +} +``` +![](/images/19.jpg) +--- +## 20. To find largest number in 1 dimentional array +```C +#include +int main(){ + int x,n,a; + int arr1[10]; + for(x=0;x<10;x++){ + printf("Enter number for %d place in array :- ",x+1); + scanf("%d",&n); + arr1[x] = n; + } + for(int i=0;i<10;i++){ + a=0; + for(int j=0;j<10;j++){ + if(arr1[i]>=arr1[j]){ + a=a+1; + } + } + if(a==10){ + printf("%d is largest number in array",arr1[i]); + } + } + return 0; +} +``` +![](/images/20.jpg) +--- +## 21. To find sum of 'n' natural numbers in an array +```C +#include +int main(){ + int n1,sum=0; + printf("Enter natural number till which you want sum of the numbers:- "); + scanf("%d",&n1); + int arr1[n1]; + for(int x=1;x<=n1;x++){ + arr1[x-1]=x; + } + for(int y=1;y<=n1;y++){ + sum=sum+arr1[y-1]; + } + printf("Sum of %d natural numbers is %d \n",n1,sum); + return 0; +} +``` +![](/images/21.jpg) +--- +## 22. Program to add two matrices +```C +#include +int main() { + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d",&m ,&n); + printf("Enter the elements of first matrix\n"); + for(c=0;c +int main(){ + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter elements of first matrix\n"); + for (c=0;c +#include +int main() { + char string[100]; + int i,n,c=0; + printf("Enter the string:- "); + scanf("%s",string); + n=strlen(string); + for(i=0;i +#include +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; +} + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; +} + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2){ + return 1; + } + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]){ + count++; + } + } + if (count == len1){ + return 0; + } + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0){ + printf("They are equal"); + } + else{ + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + default: + exit(0); + } + } + return 0; +} +``` +![](/images/25.jpg) +--- +## 26. Programs to swap two numbers using call by value and call by refernce +```C +// Call by refernce +#include +void swap(int*, int*); +int main(){ + int a,b; + printf("Enter the value of a:- "); + scanf("%d",&a); + printf("Enter the value of b:- "); + scanf("%d",&b); + printf("a = %d\n", a); + printf("b = %d\n", b); + swap(&a, &b); + printf("Swapped values\n"); + printf("a = %d\n", a); + printf("b = %d\n", b); + return 0; +} +void swap(int *x, int *y){ + int temp; + temp = *y; + *y = *x; + *x = temp; +} + +// Call by value +#include +int swap(int x,int y){ + int temp=x; + x=y; + y=temp; + printf("a = %d,b = %d\n",x,y); +} +int main(){ + int a,b; + printf("Enter 1st number ('a') :- "); + scanf("%d",&a); + printf("Enter 2nd number ('b') :- "); + scanf("%d",&b); + printf("a = %d,b = %d\n",a,b); + printf("Swapped values :- \n"); + swap(a,b); + return 0; +} +``` +![](/images/26i.jpg) +![](/images/26ii.jpg) +--- +## 27. To calculate factorial of entered number +```C +#include +int main(){ + int n1,n0,sum; + sum=1; + printf("Enter Number :- "); + scanf("%d",&n1); + for(int x=n1;x>1;x--){ + sum=sum*x; + printf("%d * ",x); + n1=n1-1; + } + printf("1"); + printf("\n"); + printf("%d",sum); + return 0; +} +``` +![](/images/27.jpg) +--- +## 28. Program to print febonacci series +```C +#include +int main(){ + int y,n1,n2,fn; + printf("Enter length of fibonacci series :- "); + scanf("%d",&y); + n1=0; + n2=1; + printf("0, 1, "); + for(int x=0;x<=y;x++){ + fn=n1+n2; + printf("%d, ",fn); + n1=n2; + n2=fn; + } + return 0; +} +``` +![](/images/28.jpg) +--- +## 29. Program to calculate average of 5 numbers using function +```C +#include +int avg(int a1,int a2,int a3,int a4,int a5){ + int result; + result=(a1+a2+a3+a4+a5)/5; + return result; +} +int main(){ + int n1,n2,n3,n4,n5,average; + printf("Enter 5 numbers:-\n"); + scanf("%d",&n1); + scanf("%d",&n2); + scanf("%d",&n3); + scanf("%d",&n4); + scanf("%d",&n5); + average=avg(n1,n2,n3,n4,n5); + printf("Average of the numbers is :- %d",average); + return 0; +} +``` +![](/images/29.jpg) +--- +## 30. Program to implement linear serach and binary search +```C +#include +#include +void linear_search(int search_key,int array[100],int n){ + int i,location; + for(i=1;i<=n;i++){ + if(search_key == array[i]){ + location = i; + printf("______________________________________\n"); + printf("The location of Search = %d is %d\n",search_key,location); + printf("______________________________________\n"); + } + } +} +void binary_search(int search_key,int array[100],int n){ + int mid,i,low,high; + low = 1; + high = n; + mid = (low + high)/2; + i=1; + while(search_key != array[mid]){ + if(search_key <= array[mid]){ + low = 1; + high = mid+1; + mid = (low+high)/2; + } + else{ + low = mid+1; + high = n; + mid = (low+high)/2; + } + } + printf("__________________________________\n"); + printf("location=%d\t",mid); + printf("Search=%d Found!\n",search_key); + printf("__________________________________\n"); +} +int main(){ + int array[100],search_key,i,j,n,low,high,location,choice; + void linear_search(int search_key,int array[100],int n); + void binary_search(int search_key,int array[100],int n); + system("cls"); + printf("ENTER THE SIZE OF THE ARRAY:-"); + scanf("%d",&n); + printf("ENTER THE ELEMENTS OF THE ARRAY:-\n"); + for(i=1;i<=n;i++){ + scanf("%d",&array[i]); + } + printf("ENTER THE SEARCH:-"); + scanf("%d",&search_key); + printf("___________________\n"); + printf("1.LINEAR SEARCH\n"); + printf("2.BINARY SEARCH\n"); + printf("___________________\n"); + printf("ENTER YOUR CHOICE:-"); + scanf("%d",&choice); + switch(choice){ + case 1: + linear_search(search_key,array,n); + break; + case 2: + binary_search(search_key,array,n); + break; + default: + exit(0); + } + return 0; +} +``` +![](/images/30i.jpg) +![](/images/30ii.jpg) +--- +## 31. Program to implement bubble sort +```C +#include +int main(){ + int array[50], n, c, d, swap; + printf("Enter number of elements:- "); + scanf("%d", &n); + printf("Enter %d integers:- \n", n); + for (c = 0; c < n; c++){ + scanf("%d", &array[c]); + } + for (c = 0 ; c < n - 1; c++){ + for (d = 0 ; d < n - c - 1; d++){ + if (array[d] > array[d+1]){ + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + printf("Sorted in ascending order:-\n"); + for (c = 0; c < n; c++){ + printf("%d, ", array[c]); + } + return 0; +} +``` +![](/images/31.jpg) +--- +## 32. Program to store information of students using array of structures +```C +#include +struct student{ + char name[15],address[30]; + int roll,grade; +}; +int main(){ + int n1,i; + printf("Enter number of students:- "); + scanf("%d",&n1); + struct student s[n1]; + for(i=0;i +int main() +{ + int a[10][10], t[10][10], r, c, i, j; + printf("Enter rows and columns of matrix:- "); + scanf("%d %d", &r, &c); + printf("Enter elements of matrix:-\n"); + for(i=0; i +int main() { + int variable; + int *pointer; + variable = 99; + pointer = &variable; + printf("Address of variable = %p", &pointer); + return 0; +} +``` +![](/images/34.jpg) +--- +## 35. Program to access array using pointer +```C +#include +int main(){ + int data[5],i; + printf("Enter 5 elements:- \n"); + for(i = 0; i < 5; ++i){ + scanf("%d", data + i); + } + printf("5 Elements you entered:- \n"); + for(i = 0; i < 5; ++i){ + printf("%d\n", *(data + i)); + } + return 0; +} +``` +![](/images/35.jpg) +--- diff --git a/Hemangi Jain/README.md b/Hemangi Jain/README.md new file mode 100644 index 0000000..cdbfedf --- /dev/null +++ b/Hemangi Jain/README.md @@ -0,0 +1 @@ +## Project File For Programing for Problem Solving. diff --git a/Hemangi Jain/images/10.jpg b/Hemangi Jain/images/10.jpg new file mode 100644 index 0000000..c513e11 Binary files /dev/null and b/Hemangi Jain/images/10.jpg differ diff --git a/Hemangi Jain/images/11.JPEG b/Hemangi Jain/images/11.JPEG new file mode 100644 index 0000000..611e990 Binary files /dev/null and b/Hemangi Jain/images/11.JPEG differ diff --git a/Hemangi Jain/images/12.jpg b/Hemangi Jain/images/12.jpg new file mode 100644 index 0000000..8d777df Binary files /dev/null and b/Hemangi Jain/images/12.jpg differ diff --git a/Hemangi Jain/images/13.jpg b/Hemangi Jain/images/13.jpg new file mode 100644 index 0000000..a0a2277 Binary files /dev/null and b/Hemangi Jain/images/13.jpg differ diff --git a/Hemangi Jain/images/14.jpg b/Hemangi Jain/images/14.jpg new file mode 100644 index 0000000..48746d8 Binary files /dev/null and b/Hemangi Jain/images/14.jpg differ diff --git a/Hemangi Jain/images/15.jpg b/Hemangi Jain/images/15.jpg new file mode 100644 index 0000000..4d1f3c8 Binary files /dev/null and b/Hemangi Jain/images/15.jpg differ diff --git a/Hemangi Jain/images/16(i).jpg b/Hemangi Jain/images/16(i).jpg new file mode 100644 index 0000000..45ee53a Binary files /dev/null and b/Hemangi Jain/images/16(i).jpg differ diff --git a/Hemangi Jain/images/16(ii).jpg b/Hemangi Jain/images/16(ii).jpg new file mode 100644 index 0000000..de5ec88 Binary files /dev/null and b/Hemangi Jain/images/16(ii).jpg differ diff --git a/Hemangi Jain/images/17.jpg b/Hemangi Jain/images/17.jpg new file mode 100644 index 0000000..0d790e0 Binary files /dev/null and b/Hemangi Jain/images/17.jpg differ diff --git a/Hemangi Jain/images/18.jpg b/Hemangi Jain/images/18.jpg new file mode 100644 index 0000000..c45c523 Binary files /dev/null and b/Hemangi Jain/images/18.jpg differ diff --git a/Hemangi Jain/images/19.jpg b/Hemangi Jain/images/19.jpg new file mode 100644 index 0000000..5a15a73 Binary files /dev/null and b/Hemangi Jain/images/19.jpg differ diff --git a/Hemangi Jain/images/2.jpg b/Hemangi Jain/images/2.jpg new file mode 100644 index 0000000..d7e64a3 Binary files /dev/null and b/Hemangi Jain/images/2.jpg differ diff --git a/Hemangi Jain/images/20.jpg b/Hemangi Jain/images/20.jpg new file mode 100644 index 0000000..dbb1895 Binary files /dev/null and b/Hemangi Jain/images/20.jpg differ diff --git a/Hemangi Jain/images/21.jpg b/Hemangi Jain/images/21.jpg new file mode 100644 index 0000000..81a0794 Binary files /dev/null and b/Hemangi Jain/images/21.jpg differ diff --git a/Hemangi Jain/images/22.jpg b/Hemangi Jain/images/22.jpg new file mode 100644 index 0000000..61dfe9f Binary files /dev/null and b/Hemangi Jain/images/22.jpg differ diff --git a/Hemangi Jain/images/23.jpg b/Hemangi Jain/images/23.jpg new file mode 100644 index 0000000..a64a315 Binary files /dev/null and b/Hemangi Jain/images/23.jpg differ diff --git a/Hemangi Jain/images/24.jpg b/Hemangi Jain/images/24.jpg new file mode 100644 index 0000000..0bd9d99 Binary files /dev/null and b/Hemangi Jain/images/24.jpg differ diff --git a/Hemangi Jain/images/25.jpg b/Hemangi Jain/images/25.jpg new file mode 100644 index 0000000..932e186 Binary files /dev/null and b/Hemangi Jain/images/25.jpg differ diff --git a/Hemangi Jain/images/26i.jpg b/Hemangi Jain/images/26i.jpg new file mode 100644 index 0000000..e03673a Binary files /dev/null and b/Hemangi Jain/images/26i.jpg differ diff --git a/Hemangi Jain/images/26ii.jpg b/Hemangi Jain/images/26ii.jpg new file mode 100644 index 0000000..7f432ce Binary files /dev/null and b/Hemangi Jain/images/26ii.jpg differ diff --git a/Hemangi Jain/images/27.jpg b/Hemangi Jain/images/27.jpg new file mode 100644 index 0000000..4d0d77a Binary files /dev/null and b/Hemangi Jain/images/27.jpg differ diff --git a/Hemangi Jain/images/28.jpg b/Hemangi Jain/images/28.jpg new file mode 100644 index 0000000..3aafe5f Binary files /dev/null and b/Hemangi Jain/images/28.jpg differ diff --git a/Hemangi Jain/images/29.jpg b/Hemangi Jain/images/29.jpg new file mode 100644 index 0000000..737eb31 Binary files /dev/null and b/Hemangi Jain/images/29.jpg differ diff --git a/Hemangi Jain/images/3.jpg b/Hemangi Jain/images/3.jpg new file mode 100644 index 0000000..577627c Binary files /dev/null and b/Hemangi Jain/images/3.jpg differ diff --git a/Hemangi Jain/images/30i.jpg b/Hemangi Jain/images/30i.jpg new file mode 100644 index 0000000..2cb0ccd Binary files /dev/null and b/Hemangi Jain/images/30i.jpg differ diff --git a/Hemangi Jain/images/30ii.jpg b/Hemangi Jain/images/30ii.jpg new file mode 100644 index 0000000..3e581a4 Binary files /dev/null and b/Hemangi Jain/images/30ii.jpg differ diff --git a/Hemangi Jain/images/31.jpg b/Hemangi Jain/images/31.jpg new file mode 100644 index 0000000..43dc483 Binary files /dev/null and b/Hemangi Jain/images/31.jpg differ diff --git a/Hemangi Jain/images/32.jpg b/Hemangi Jain/images/32.jpg new file mode 100644 index 0000000..68d8e5e Binary files /dev/null and b/Hemangi Jain/images/32.jpg differ diff --git a/Hemangi Jain/images/33.jpg b/Hemangi Jain/images/33.jpg new file mode 100644 index 0000000..4bb93d0 Binary files /dev/null and b/Hemangi Jain/images/33.jpg differ diff --git a/Hemangi Jain/images/34.jpg b/Hemangi Jain/images/34.jpg new file mode 100644 index 0000000..86d80bf Binary files /dev/null and b/Hemangi Jain/images/34.jpg differ diff --git a/Hemangi Jain/images/35.jpg b/Hemangi Jain/images/35.jpg new file mode 100644 index 0000000..1be93fa Binary files /dev/null and b/Hemangi Jain/images/35.jpg differ diff --git a/Hemangi Jain/images/4.jpg b/Hemangi Jain/images/4.jpg new file mode 100644 index 0000000..5e8403a Binary files /dev/null and b/Hemangi Jain/images/4.jpg differ diff --git a/Hemangi Jain/images/5.jpg b/Hemangi Jain/images/5.jpg new file mode 100644 index 0000000..d583eba Binary files /dev/null and b/Hemangi Jain/images/5.jpg differ diff --git a/Hemangi Jain/images/6.jpg b/Hemangi Jain/images/6.jpg new file mode 100644 index 0000000..0e10407 Binary files /dev/null and b/Hemangi Jain/images/6.jpg differ diff --git a/Hemangi Jain/images/7.jpg b/Hemangi Jain/images/7.jpg new file mode 100644 index 0000000..52edec8 Binary files /dev/null and b/Hemangi Jain/images/7.jpg differ diff --git a/Hemangi Jain/images/8.jpg b/Hemangi Jain/images/8.jpg new file mode 100644 index 0000000..2388176 Binary files /dev/null and b/Hemangi Jain/images/8.jpg differ diff --git a/Hemangi Jain/images/9.jpg b/Hemangi Jain/images/9.jpg new file mode 100644 index 0000000..c9f1d7c Binary files /dev/null and b/Hemangi Jain/images/9.jpg differ diff --git a/Hemangi Jain/images/Gndec.jpg b/Hemangi Jain/images/Gndec.jpg new file mode 100644 index 0000000..2e1965a Binary files /dev/null and b/Hemangi Jain/images/Gndec.jpg differ diff --git a/Hemangi Jain/images/img1.JPG b/Hemangi Jain/images/img1.JPG new file mode 100644 index 0000000..14efb20 Binary files /dev/null and b/Hemangi Jain/images/img1.JPG differ diff --git a/MuskanPPS.md b/MuskanPPS.md new file mode 100644 index 0000000..e9beee0 --- /dev/null +++ b/MuskanPPS.md @@ -0,0 +1,1260 @@ +![](https://i.imgur.com/J62Mvcf.jpg) +# ESC-18104/18105 Programming for Problem Solving + + **Name**:Muskan Dhiman + **Branch**:CSE C1 + **Class Roll No.**:1915333 + **Submitted To**:Ms. Goldendeep Kaur + + +## 1) To print my name using puts +```c +#include +int main() +{ + +puts("******************************"); +puts("My name is Muskan Dhiman"); +puts("******************************") +return 0; +``` + +Output: +![](https://i.imgur.com/IfugLah.jpg) + +## 2) To print the college address in multiline statements +```c +#include +int main() +{ +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab\n"); +return 0; +} +``` +Output: +![](https://i.imgur.com/UoP4d5h.jpg) + + + +## 3) Program to add to integers by taking input from the user +```c +#include + int main() + { + int a, b, c; + + printf("Enter two integers: "); + scanf("%d %", &a, &b); + c = a+b; + printf("%d + %d = %d\n", a, b, c); + return 0; +} + + +``` +Output: +![](https://i.imgur.com/NVdpVOQ.jpg) + +## 4) Program to compute quotient and remainder +```c +#include + int main() +{ + int dividend, divisor, quotient, remainder; + printf("Enter dividend: "); + scanf("%d", ÷nd); + printf("Enter divisor: "); + scanf("%d", &divisor); + quotient = dividend / divisor; + remainder = dividend % divisor; + printf("Quotient = %d\n", quotient); + printf("Remainder = %d\n", remainder); + return 0; +} +``` +Output: +![](https://i.imgur.com/UPBTLuN.jpg) + + + +## 5) Program to swap to numbers without using third variable +```c +#include + int main() + { + int a=10, b=20; + printf("Before swap a=%d b=%d",a,b); + a=a+b; + b=a-b; +a=a-b; + printf("\nAfter swap a=%d b=%d\n",a,b); +return 0; +} +``` + +Output: +![](https://i.imgur.com/9w8ZKpC.jpg) + + +## 6) Program to check whether a number is even or odd +```c +#include +int main() + { + int a; + printf("Enter an integer: "); + scanf("%d", &a); + if(a % 2 == 0) + printf("%d is even.\n", a); + else + printf("%d is odd.\n", a); + return 0; +} +``` +Output: +![](https://i.imgur.com/quv3xbk.jpg) + + + +## 7) Program to find the greater of two numbers +```c +#include +int main() + { + int a, b; + printf("Enter two integers :"); + scanf("%d%d", &a, &b); + if (a>b) + { + printf("Largest number is %d.\n", a); + } + else + { + printf("Largest number is %d.\n", b); + } + return 0; +} +``` +Output: +![](https://i.imgur.com/voUuoKn.jpg) + + + +## 8) Program to find the greatest of three numbers +```c +#include +int main() + { + int a, b, c; + + printf("\nEnter value of a, b, c:"); + scanf("%d %d %d",&a,&b,&c); + if((a>b)&&(a>c)) +{ + printf("\n a is greatest\n"); +} + else if((b>c)&&(b>a)) +{ + printf("\n b is greatest\n"); +} + else +{ + printf("\n c is greatest\n"); +} + return 0; + } + ``` + Output: + ![](https://i.imgur.com/HSqUVxN.jpg) + + + +## 9) Program to print grade of students according to marks entered +```c +#include +int main() + { + int marks; + printf("Enter your marks "); + scanf("%d",&marks); + if(marks<0 || marks>100) + { + printf("Wrong Entry\n"); + } + else if(marks<50) + { + printf("Grade F\n"); + } + else if(marks>=50 && marks<60) + { + printf("Grade D\n"); + } + else if(marks>=60 && marks<70) + { + printf("Grade C\n"); + } + else if(marks>=70 && marks<80) + { + printf("Grade B\n"); + } + else if(marks>=80 && marks<90) + { + printf("Grade A\n"); + } + else + { + printf("Grade A+\n"); + } + return 0; + } +``` + +Output: +![](https://i.imgur.com/nztwRK1.jpg) + +## 10) Program to find the roots of quadratic equation +```c +#include +#include +int main() +{ + double a, b, c, discriminant, root1, root2, realPart, imaginaryPart; + printf("Enter coefficients a, b and c: "); + scanf("%lf %lf %lf",&a, &b, &c); + discriminant = b*b-4*a*c; + if (discriminant > 0) + { + root1 = (-b+sqrt(discriminant))/(2*a); + root2 = (-b-sqrt(discriminant))/(2*a); + printf("root1 = %.2lf and root2 = %.2lf",root1 , root2); + } + else if (discriminant == 0) + { + root1 = root2 = -b/(2*a); + printf("root1 = root2 = %.2lf;", root1); + } + else + { + realPart = -b/(2*a); + imaginaryPart = sqrt(-discriminant)/(2*a); + printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart); + } + return 0; +} +``` +Output: +![](https://i.imgur.com/ww3WFLP.jpg) + + +## 11) Program to check whether a year is leap year or not +```c +#include +int main() + { + int year; + printf("Enter a year: "); + scanf("%d",&year); + if(year%4 == 0) + { + if( year%100 == 0) + { + if ( year%400 == 0) + printf("%d is a leap year.\n", year); + else + printf("%d is not a leap year.\n", year); + } + else + printf("%d is a leap year.\n", year ); + } + else + printf("%d is not a leap year.\n", year); + return 0; +} +``` +Output: +![](https://i.imgur.com/bSCkLyF.jpg) + + + +## 12) Program to generate multiplication table of 5 +```c +#include +int main() +{ int n=5, i; + printf("Table of %d\n",n); +for(i=1; i<=10; ++i) + { + printf("%d * %d = %d \n", n, i, n*i); + } + return 0; +} +``` +Output: +![](https://i.imgur.com/T67PO6m.jpg) + + + +## 13) Program to make simple calculator using switch case +```c +#include + int main() + { + int num1,num2; + float result; + char ch; + printf("Enter first number: "); + scanf("%d",&num1); + printf("Enter second number: "); + scanf("%d",&num2); + printf("Choose operation to perform (+,-,*,/,%): "); + scanf(" %c",&ch); + result=0; + switch(ch) + { + case '+': + result=num1+num2; + break; + case '-': + result=num1-num2; + break; + case '*': + result=num1*num2; + break; + case '/': + result=(float)num1/(float)num2; + break; + case '%': + result=num1%num2; + break; + default: + printf("Invalid operation.\n"); + } + printf("Result: %d %c %d = %f\n",num1,ch,num2,result); + return 0; +} +``` +Output: +![](https://i.imgur.com/1TMtpey.jpg) + + + +## 14) Program to calculate reverse of a number +```c +#include +int main() +{ + int n, reversedNumber = 0, remainder; + printf("Enter an integer: "); + scanf("%d", &n); + while(n != 0) + { + remainder = n%10; + reversedNumber = reversedNumber*10+ remainder; + n /= 10; + } + printf("Reversed Number = %d\n", reversedNumber); + return 0; +} + +``` +Output: +![](https://i.imgur.com/spjFKGP.jpg) + + +## 15) Program to check whether a number is palindrome or not +```c +#include +int main() + { + int n, reversedInteger = 0, remainder,originalInteger; + printf("Enter an integer: "); + scanf("%d", &n); + originalInteger = n; + while( n!=0 ) + { + remainder = n%10; + reversedInteger =reversedInteger*10 + remainder; + n /= 10; + } + if (originalInteger == reversedInteger) + printf("%d is a palindrome.\n", originalInteger); + else + printf("%d is not a palindrome.\n", originalInteger); + return 0; +} + +``` +Output: +![](https://i.imgur.com/wWzh72P.jpg) + + +## 16) Program to check whether a number is prime or not +```c + #include +int main() +{ + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + for(i = 2; i <= n-1; i++) + { + if(n%i == 0) + { + flag = 1; + break; + } + } + if (n == 1) + { + printf("1 is neither a prime nor a composite number.\n"); + } + else + { + if (flag == 0) + printf("%d is a prime number.\n", n); + else + printf("%d is not a prime number.\n", n); + } + return 0; +} +``` + +Output: +![](https://i.imgur.com/to1jV6P.jpg) + + +## 17) Program to print prime numbers from 1 to 100 +```c +#include +int main(void) + { + for(int i=2;i<100;i++) + { + for(int j=2;j +int main() +{ + int number, originalNumber, remainder, result =0; + printf("Enter a three digit integer: "); + scanf("%d", &number); + originalNumber = number; + while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + } + if(result == number) + printf("%d is an Armstrong number.\n",number); + else + printf("%d is not an Armstrong number.\n",number); + return 0; +} + +``` +Output: +![](https://i.imgur.com/xpqHFUd.jpg) + + +## 19) Program to print different patterns + +### i +```c +#include +int main() +{ + int i, j; + for(i=1; i<=4; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} +``` + +Output: +![](https://i.imgur.com/pr8XOsA.jpg) +### ii +```c +#include +int main() +{ + int i, j, number= 1; + for(i=1; i <= 4; i++) + { + for(j=1; j <= i; ++j) + { + printf("%d ", number); + ++number; + } +printf("\n"); + } +return 0; +} +``` +Output: +![](https://i.imgur.com/BnkRWfz.jpg) + + + +## 20) Program to find largest element from one dimensional array +```c +#include +int main() + { + int i, n; + float arr[100]; + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + for(i = 1; i < n; ++i) + { + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f\n", arr[0]); + return 0; + } + ``` + + Output: + ![](https://i.imgur.com/OuXUNvo.jpg) + + + ## 21) Program to find the sum of N natural numbers in an array + ```c + #include +int main() +{ +int n, sum = 0, c, value; +printf("How many numbers you want to add?\n"); +scanf("%d",&n); +printf("Enter %d integers\n", n); +for (c = 1; c <= n; c++) +{ +scanf("%d", &value); +sum = sum +value; +} +printf("Sum of the integers = %d\n", sum); +return 0; +} +``` +Output: +![](https://i.imgur.com/bDQzpXO.jpg) + + + +## 22) Program to add two matrices +```c +#include +int main() +{ + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter number of columns (between 1 and 100): "); + scanf("%d", &c); + printf("\nEnter elements of 1st matrix:\n"); + for(i=0; i +int main() +{ +int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; +printf("Enter rows and column for first matrix: "); +scanf("%d %d", &r1, &c1); +printf("Enter rows and column for second matrix: "); +scanf("%d %d",&r2, &c2); +while (c1 != r2) +{ +printf("Error! column of first matrix not equal to row of second.\n\$ +printf("Enter rows and column for first matrix: "); +scanf("%d %d", &r1,&c1); +printf("Enter rows and column for second matrix: "); +scanf("%d %d",&r2, &c2); +} +printf("\nEnter elements of matrix 1:\n"); +for(i=0; i +#include +int main() +{ +char string1[20]; +int i, length; +int flag = 0; +printf("Enter a string:"); +scanf("%s", string1); +length = strlen(string1); +for(i=0;i < length ;i++) +{ +if(string1[i] != string1[length-i-1]) +{ +flag = 1; +break; + +} +} +if (flag) +{ +printf("%s is not a palindrome\n", string1); +} +else +{ +printf("%s is a palindrome\n", string1); +} +return 0; +} +``` + +Output: +![](https://i.imgur.com/ejLFOu7.jpg) + +## 25) Program to perform basic operations like length of string, string concat, string copy, string compare and string reverse +```c +#include +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; +} + int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; +case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` + Output: + ![](https://i.imgur.com/mUrDP13.jpg) +## 26) Program to swap two numbers using call by value and call by refernce + +### Call by reference + ```c +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +Output: +![](https://i.imgur.com/R3gOd8L.jpg) +### Call by value +```c +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +Output: +![](https://i.imgur.com/U0xpwws.jpg) + +## 27) Program to calculate factorial of a number with and without recursion both + +### With recursion + + #include + long int multiplyNumbers(int n); + int main() + { + int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; + } + long int multiplyNumbers(int n) + { + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; + } + + + Output: + ![](https://i.imgur.com/9n0pp3V.jpg) + ### Without recursion + +```c +#include + +int main() +{ + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` + +Output: +![](https://i.imgur.com/57Btrk4.jpg) + +## 28) Program to print fibonacci series with and without recursion both +### With recursion + #include + + int f(int); + + int main() + { + int n, i = 0, c; + + scanf("%d", &n); + + printf("Fibonacci series terms are:\n"); + + for (c = 1; c <= n; c++) + { + printf("%d\n", f(i)); + i++; + } + + return 0; + } + + int f(int n) + { + if (n == 0 || n == 1) + return n; + else + return (f(n-1) + f(n-2)); + } + + Output: + ![](https://i.imgur.com/jW7oZTG.jpg) + + + ### Without recursion + ```c +#include +int main() +{ + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("\n%d %d\n",n1,n2); + for(i=2;i +int avg(int,int,int,int,int); + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively: "); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); + printf("Average of the numbers is %d\n",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` + Output: + ![](https://i.imgur.com/zIdFX1l.jpg) + ## 30) Program to implement linear search and binary search + + ### Linear search + ```c + #include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + for (c = 0; c < n; c++) + { + if (array[c] == search) + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} +``` +Output: +![](https://i.imgur.com/NK5reLm.jpg) +## Binary search +```c +#include + +int main() +{ + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; +while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +} + ``` + Output: + ![](https://i.imgur.com/l1yxWyl.jpg) + +## 31) Program to implement bubble sort + +```c +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +Output: +![](https://i.imgur.com/dJBg0Ky.jpg) + + + +## 32) Program to store information of 10 students using array of structures + +```c +#include +struct student +{ + char name[50]; + int roll; + float marks; +} s[10]; +int main() +{ + int i; + printf("Enter information of students:\n"); + for(i=0; i<3; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying Information:\n\n"); + for(i=0; i<3; ++i) + { +printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } + return 0; +} +``` + +Output: +![](https://i.imgur.com/gA2YBTq.jpg) +## 33) Program to compute the transpose of a matrix +```c +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} +``` +Output: +![](https://i.imgur.com/cwES99d.jpg) + + + +## 35) Program to access array elements using pointer +```c + +#include +int main() +{ + int data[5], i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` + +Output: +![](https://i.imgur.com/lVjpKfv.jpg) + diff --git a/Nimrat_Kaur1915336.docx b/Nimrat_Kaur1915336.docx new file mode 100644 index 0000000..0ccd312 Binary files /dev/null and b/Nimrat_Kaur1915336.docx differ diff --git a/P19_1.c b/P19_1.c new file mode 100644 index 0000000..ca7cab7 --- /dev/null +++ b/P19_1.c @@ -0,0 +1,16 @@ +//PROGRAM TO PRINT A PATTERN +#include +void main() +{ + int row, col; +printf("\n"); + for(row=1;row<=4;row++) + { + for(col=1;col<=row;col++) + { + printf ("%d ",col); + } + printf("\n"); + } +printf("\n"); +} diff --git a/P19_2.C b/P19_2.C new file mode 100644 index 0000000..b2e7607 --- /dev/null +++ b/P19_2.C @@ -0,0 +1,16 @@ +//PROGRAM TO PRINT A PATTERN -2 +#include +void main() +{ + int row, col; +printf("\n"); + for(row=1;row<=4;row++) + { + for(col=0;col +void main() +{ + int row, col,col1,spaces; +printf("\n"); + for(row=1;row<=4;row++) + { + for(spaces=4;spaces>row;spaces--) + { + printf(" "); + } + for(col=1;col<=row;col++) + { + printf ("%d",col); + } + for(col1=col-2;col1>=1;col1--) + { + printf("%d",col1); + } + printf("\n"); + } +printf("\n"); +} diff --git a/P20.c b/P20.c new file mode 100644 index 0000000..3cff3f7 --- /dev/null +++ b/P20.c @@ -0,0 +1,20 @@ +//PROGRAM TO FIND LARGEST ELEMENT FROM 1-D ARRAY +#include +void main() +{ + int a[10],i, largest,n; +printf("\nEnter number of elements(<=10)::"); +scanf("%d",&n); +for(i=0;i<=n-1;i++) +{ +printf("\nEnter element %d::",i+1); +scanf("%d",&a[i]); +} +largest=a[0]; +for(i=1;i<=n-1;i++) +{ + if(a[i]>largest) + largest=a[i]; +} + printf("\nThe largest element in the array is::%d\n",largest); +} diff --git a/P21.c b/P21.c new file mode 100644 index 0000000..d24e0dc --- /dev/null +++ b/P21.c @@ -0,0 +1,18 @@ +//PROGRAM TO FIND SUM OF N ELEMENTS OF AN ARRAY +#include +void main() +{ + int a[10],i, sum=0,n; +printf("\nEnter number of elements(<=10)::"); +scanf("%d",&n); +for(i=0;i<=n-1;i++) +{ +printf("\nEnter element %d::",i+1); +scanf("%d",&a[i]); +} +for(i=0;i<=n-1;i++) +{ + sum=sum+a[i]; + } + printf("\nSum of elements of the Array is::%d\n",sum); +} diff --git a/P22.c b/P22.c new file mode 100644 index 0000000..d86daf3 --- /dev/null +++ b/P22.c @@ -0,0 +1,40 @@ +//PROGRAM TO FIND SUM OF TWO MATRICES +#include +void main() +{ + int a[3][3],b[3][3], sum[3][3],i,j; +printf("\nEnter elements of the first 3X3 matrix::"); +for(i=0;i<=2;i++) +{ +printf("\n Enter elements of row %d :: ", i+1); + for(j=0;j<=2;j++) + { + scanf("%d",&a[i][j]); + } +} +printf("\nEnter elements of the second 3X3 matrix::"); +for(i=0;i<=2;i++) +{ +printf("\n Enter elements of row %d :: ", i+1); + for(j=0;j<=2;j++) + { + scanf("%d",&b[i][j]); + } +} +printf("\nThe first 3X3 matrix is::\n"); +for(i=0;i<=2;i++) +{ + for(j=0;j<=2;j++) + { + printf("%d ",a[i][j]); + } +printf("\n"); +} + +printf("\nThe second 3X3 matrix is::\n"); +for(i=0;i<=2;i++) +{ + for(j=0;j<=2;j++) + { + printf("%d ",b[i][j]); + } diff --git a/P23.c b/P23.c new file mode 100644 index 0000000..1a384f9 --- /dev/null +++ b/P23.c @@ -0,0 +1,69 @@ +//PROGRAM TO FIND PRODUCT OF TWO MATRICES +#include +#include +int main() +{ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +printf("enter the number of row="); +scanf("%d",&r); +printf("enter the number of column="); +scanf("%d",&c); +printf("\nEnter elements of the first 3X3 matrix::"); +for(i=0;i<=r-1;i++) +{ +printf("\n Enter elements of row %d :: ", i+1); + for(j=0;j<=c-1;j++) + { + scanf("%d",&a[i][j]); + } +} +printf("\nEnter elements of the second 3X3 matrix::"); +for(i=0;i<=r-1;i++) +{ +printf("\n Enter elements of row %d :: ", i+1); + for(j=0;j<=c-1;j++) + { + scanf("%d",&b[i][j]); + } +} +printf("\nThe first 3X3 matrix is::\n"); +for(i=0;i<=r-1;i++) +{ + for(j=0;j<=c-1;j++) + { + printf("%d ",a[i][j]); + } +printf("\n"); +} + +printf("\nThe second 3X3 matrix is::\n"); +for(i=0;i<=r-1;i++) +{ + for(j=0;j<=c-1;j++) + { + printf("%d ",b[i][j]); + } +printf("\n"); +} +printf("Multiplication of the two matrices is::\n"); +for(i=0;i +#include +void main() +{ + char string1[20]; + int i, length; + int flag = 0; + printf("\nEnter a string::"); + scanf("%s", string1); + length = strlen(string1); + for(i=0;i < length ;i++){ + if(string1[i] != string1[length-i-1]){ + flag = 1; + break; + } +} + if (flag) + { + printf("\n%s is not a palindrome\n", string1); + } + else { + printf("\n%s is a palindrome\n", string1); + } + +} diff --git a/P25.c b/P25.c new file mode 100644 index 0000000..edeba1d --- /dev/null +++ b/P25.c @@ -0,0 +1,73 @@ +// To perform basic string operations + +#include +#include +#include +void main() +{ + char str1[20],str2[20]; + int ch,i,j; + do + { + printf("\n\tMENU"); + printf("\n------------------------------\n"); + printf("1:Find Length of String"); + printf("\n2:Concatenate Strings"); + printf("\n3:Copy String "); + printf("\n4:Compare Strings"); + printf("\n5:Exit"); + printf("\n------------------------------\n"); + printf("\nEnter your choice: "); + scanf("%d",&ch); + switch(ch) + { + case 1: + printf("\nEnter String: "); + scanf("%s",str1); + i=strlen(str1); + printf("\nLength of String : %d\n\n",i); + break; + case 2: + printf("\nEnter First String: "); + scanf("%s",str1); + printf("\nEnter Second string: "); + scanf("%s",str2); + strcat(str1,str2); + printf("\nString After Concatenation : %s\n\n",str1); + break; + case 3: + printf("\nEnter a String1: "); + scanf("%s",str1); + printf("Enter a String2: "); + scanf("%s",str2); +printf("\nString Before Copying:\nString1=\"%s\",String2=\"%s\"\n",str1,str2); + strcpy(str2,str1); + printf("\n-----------------------------------------------\n"); + printf("\"Ccpying string String1 to String2.....\" \n"); + printf("-----------------------------------------------\n"); + printf("String After Copying:\nString1=\"%s\", String2=\"%s\"\n\n",str1,str2); + break; + case 4: + printf("\nEnter First String: "); + scanf("%s",str1); + printf("\nEnter Second String: "); + scanf("%s",str2); + j=strcmp(str1,str2); + if(j==0) + { + printf("\nStrings are Same\n\n"); + } + else + { + + printf("\nStrings are Not Same\n\n"); + } + break; + case 5: + exit(0); + break; + default: + printf("\nInvalid Input. Please Enter valid Input.\n\n "); + } + }while(ch!=5); +} diff --git a/P26-1.c b/P26-1.c new file mode 100644 index 0000000..3cbce52 --- /dev/null +++ b/P26-1.c @@ -0,0 +1,21 @@ +//PROGRAM TO SWAP TWO NUMBERS USING CALL BY REFERENCE +# include +void swap(int *, int *); +int x,y; +void main() +{ + //int x, y; + printf("\nEnter the value of x and y\n::"); + scanf("%d %d",&x,&y); + printf("\nBefore Swapping\nx = %d\ny = %d\n", x, y); + swap(&x,&y); + printf("\nAfter Swapping\nx = %d\ny = %d\n", x, y); +} + +void swap(int *x,int *y) +{ + int tmp; + tmp=*x; + *x=*y; + *y=tmp; +} diff --git a/P26.c b/P26.c new file mode 100644 index 0000000..e13c55c --- /dev/null +++ b/P26.c @@ -0,0 +1,21 @@ +//PROGRAM TO SWAP TWO NUMBERS USING CALL BY VALUE +# include +void swap(int, int); +int x,y; +void main() +{ + //int x, y; + printf("\nEnter the value of x and y\n::"); + scanf("%d %d",&x,&y); + printf("\nBefore Swapping\nx = %d\ny = %d\n", x, y); + swap(x, y); +} + +void swap(int a, int b) +{ + int temp; + temp = b; + b = a; + a = temp; + printf("After Swapping\nx = %d\ny = %d\n", a, b); +} diff --git a/P27-.c b/P27-.c new file mode 100644 index 0000000..13a1727 --- /dev/null +++ b/P27-.c @@ -0,0 +1,17 @@ +//PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION +#include +int factorial(int n); +void main() +{ + int n; + printf("\nEnter a positive integer: "); + scanf("%d", &n); + printf("\nFactorial of %d = %d\n", n, factorial(n)); +} +int factorial(int n) +{ + if (n >= 1) + return n*factorial(n-1); + else + return 1; +} diff --git a/P27-1.c b/P27-1.c new file mode 100644 index 0000000..d26d040 --- /dev/null +++ b/P27-1.c @@ -0,0 +1,15 @@ +//PROGRAM TO FIND FACTORIAL OF A NUMBER WITHOUT USING RECURSION +#include +void main() +{ + int n, i; + + printf("\n Enter any number:: "); + scanf("%d", &n); + + for (i=1; i<=n; i++) + fact = fact*i; + printf("\nFactorial = %ld\n", fact); +} + + diff --git a/P28-1.c b/P28-1.c new file mode 100644 index 0000000..011562c --- /dev/null +++ b/P28-1.c @@ -0,0 +1,18 @@ +// Print Fibonacci series without recursion +#include +void main() +{ + int n1=0,n2=1,n3,i,number; + printf("\nEnter the number of elements of fibonacci series to be printed::"); + scanf("%d",&number); + printf("\n%d %d",n1,n2);//printing 0 and 1 + for(i=2;i +int fibonacci(int); +void main() +{ + int terms; + + printf("\nEnter number of elements of fibonacci series to be printed:: "); + scanf("%d", &terms); + for(int n = 0; n < terms; n++) + { + printf("%d ", fibonacci(n)); + } + printf("\n"); + } + +int fibonacci(int num) +{ + + //base condition + if(num == 0 || num == 1) + { + return num; + } + else + { + // recursive call + return fibonacci(num-1) + fibonacci(num-2); + } +} diff --git a/P29.c b/P29.c new file mode 100644 index 0000000..de32c7a --- /dev/null +++ b/P29.c @@ -0,0 +1,29 @@ +//PROGRAM TO CALCULATE AVERAGE OF 5 NUMBERS USING FUNCTIONS +#include +float calc_avg(int[]); +void main() +{ +int i; +int numbers[5]; +float average; +printf("\nPlease enter the 5 numbers you wish to calculate the average\n"); +for (i = 0; i < 5; i++) +{ +printf("Enter number %d :: \n",i+1); +scanf("%d", &numbers[i]); +} +average=calc_avg(numbers); +printf("The average of the numbers that you've entered is %.2f\n", average); +} + +float calc_avg(int numbers[5]) +{ +int i, sum = 0; +float avg; +for (i = 0; i < 5; i++) +{ +sum = sum+ numbers[i]; +} +avg= (float)sum / 5; +return avg; +} diff --git a/P30.c b/P30.c new file mode 100644 index 0000000..f5d530f --- /dev/null +++ b/P30.c @@ -0,0 +1,85 @@ +//Program to implement linear search and Binary search */ + +#include +#include +void main() +{ + + int array[10],search_item,i,j,n,low,high,location,choice; + void linear_search(int search_item,int array[10],int n); + void binary_search(int search_item,int array[10],int n); +/* read the elements of array */ + printf("\nENTER THE SIZE OF THE ARRAY: "); + scanf("%d",&n); + printf("\nENTER THE ELEMENTS OF THE ARRAY:\n"); + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + } +/* Get the Search element */ + printf("\nENTER THE ELEMENT TO BE SEARCHED:"); + scanf("%d",&search_item); +/* Choice of Search Algorithm */ + printf("\n___________________\n"); + printf("1.LINEAR SEARCH\n"); + printf("2.BINARY SEARCH\n"); + printf("___________________\n"); + printf("ENTER YOUR CHOICE:"); + scanf("%d",&choice); + switch(choice) + { + case 1: + linear_search(search_item,array,n); + break; + case 2: + binary_search(search_item,array,n); + break; + default: + exit(0); +} +/* LINEAR SEARCH */ + void linear_search(int search_item,int array[10],int n) + { + int i,location; + for(i=1;i<=n;i++) + { + if(search_item == array[i]) + { + location = i; + printf("______________________________________\n"); + printf("The location of Search Item = %d is %d\n",search_item,location); + printf("______________________________________\n"); + } + } +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_item,int array[10],int n) +{ + int mid,i,low,high; + low = 1; + high = n; + mid = (low + high)/2; +printf("\nmid=%d\n",mid); + i=1; + while(search_item != array[mid]) + { + if(search_item <= array[mid]) + { + low = 1; + high = mid+1; + mid = (low+high)/2; + } + else + { + low = mid+1; + high = n; + mid = (low+high)/2; + } +} + printf("__________________________________\n"); + printf("location=%d\t",mid); + printf("Search_Item=%d Found!\n",search_item); + printf("__________________________________\n"); +} diff --git a/P31.c b/P31.c new file mode 100644 index 0000000..526a0ca --- /dev/null +++ b/P31.c @@ -0,0 +1,28 @@ +//PROGRAM TO IMPLEMENT BUBBLE SORT +#include +void main() +{ + int a[10],i,j,temp,n; + printf("\n Enter the no.of Elements to Sort: \n"); + scanf("%d",&n); + printf("\n Enter the Elements : \n"); + for(i=0; ia[j]) + { + temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + } + printf("\n Sorted Elements are::\n"); + for(i=0; i +struct student +{ + char name[50]; + int roll; + float marks; +} s[10]; +void main() +{ + int i; + printf("\nEnter information of students:\n"); + // storing information + for(i=0; i<10; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + // printf("\n"); + } + printf("Displaying Information:\n\n"); + // displaying information + for(i=0; i<10; ++i) + { + printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } +} diff --git a/P33.c b/P33.c new file mode 100644 index 0000000..3297508 --- /dev/null +++ b/P33.c @@ -0,0 +1,35 @@ +//PROGRAM TO PRINT TRANSPOSE OF A MATRIX +#include +void main() +{ + static int array[10][10]; + int i, j, m, n; + printf("\nEnter the order of the matrix \n"); + scanf("%d %d", &m, &n); + printf("\nEnter the elements of the matrix\n"); + for (i = 0; i < m; ++i) + { + for (j = 0; j < n; ++j) + { + scanf("%d", &array[i][j]); + } + } + printf("\nThe given matrix is \n"); + for (i = 0; i < m; ++i) + { + for (j = 0; j < n; ++j) + { + printf(" %d", array[i][j]); + } + printf("\n"); + } + printf("Transpose of matrix is \n"); + for (j = 0; j < n; ++j) + { + for (i = 0; i < m; ++i) + { + printf(" %d", array[i][j]); + } + printf("\n"); + } +} diff --git a/P34.C b/P34.C new file mode 100644 index 0000000..309cc8a --- /dev/null +++ b/P34.C @@ -0,0 +1,15 @@ +//PROGRAM TO PRINT ADDRESS OF VARIABLES USING POINTERS +#include +void main( ) +{ +int a ; +int *p ; +printf("\nEnter any integer: ") ; +scanf("%d",&a) ; +p = &a ; +printf("\n Value of Integer : %d ",a) ; +printf("\n Value of Integer : %d ",*p) ; +printf("\n Value of Integer : %d ",*(&a)) ; +printf("\n Address of Integer : %u ",p) ; +printf("\n Address of Integer : %u ",&a) ; +} diff --git a/P35.c b/P35.c new file mode 100644 index 0000000..d4c078b --- /dev/null +++ b/P35.c @@ -0,0 +1,29 @@ +//PROGRAM TO ACCESS ARRAY ELEMENTS USING POINTERS + +#include +void main() +{ + int arr[10]; + int N, i; + int * ptr = arr; // Pointer to arr[0] + + printf("Enter number of elements in the array (<=10): "); + scanf("%d", &N); + printf("Enter elements in array:\n"); + for (i = 0; i < N; i++) + { + scanf("%d", ptr); + // Move pointer to next array element + ptr++; + } + // pointer again points back to first array element + ptr = arr; + printf("Array elements: "); + for (i = 0; i < N; i++) + { + // Print value pointed by the pointer + printf("%d, ", *ptr); + // Move pointer to next array element + ptr++; + } +} diff --git a/PPS (1).html b/PPS (1).html new file mode 100644 index 0000000..e86ef74 --- /dev/null +++ b/PPS (1).html @@ -0,0 +1,778 @@ + + + + + + + + + + + + + PPS - CodiMD + + + + + + + + + + + + + + + + + + +

PPS

My Details

Name - khushboo kumari
+CRN -1915325
+Branch - CSE

1) To print name using puts.

/* Program to print your name */

#include<stdio.h>
+int main() {

puts(“");
+puts(“khushboo kashyap”);
+puts("
”);

return 0;
+output:

##2) To print College address

/* College Address */

#include<stdio.h>
+int main() {

printf("\n\t\t\tGuru Nanak Dev Engineering College,");
+printf("\n\t\t\tGill Road,");
+printf("\n\t\t\tLudhiana , Punjab");

return 0;
+}
+output:

3) Program to add two integers .

/* To add two integers */

#include <stdio.h>
+int main() {

int a,b;

printf("\nEnter the numbers…");

printf("\nA:");
+scanf("%d",&a);

printf("\nB:");
+scanf("%d",&b);

a=a+b;

printf("\n Sum of the number is %d ",a);

return 0;

}
+output:

4) Program to find quotient and remainder.

/* To find quotient and remainder */

#include <stdio.h>

int main() {

int a,b,r,q;

printf("\nEnter the Dividend:");
+scanf("%d",&a);

printf("\nEnter the divisor:");
+scanf("%d",&b);

r=a%b;
+q=a/b;

printf("\nRemainder: %d",r);
+printf("\nQuotient: %d",q);

return 0;
+}
+output:

5) Program to swap two variables without 3rd variable.

/* Swapping without 3rd variable */

#include <stdio.h>
+int main() {

int a,b;

printf("\nEnter the value of A:");
+scanf("%d",&a);

printf("\nEnter the value of B:");
+scanf("%d",&b);

a = a + b;
+b = a - b;
+a = a - b;

printf("\nA: %d",a);
+printf("\nB: %d",b);

return 0;
+}
+output:

6) Program to check even odd number.

/* To find whether number is even or odd */

#include<stdio.h>
+int main() {

int num,temp;

printf(“Enter the Number:”);
+scanf("%d",&num);

if(num%2==0)
+printf("\nNumber is Even…");

else
+printf("\nNumber is Odd…");

printf("\n\n");
+return 0;
+}
+ouyput:

7) Finding greteast of two numbers.

/* Largest one in two */

#include<stdio.h>

int main() {
+int a,b;
+printf(“Enter any two number(A and B): “);
+scanf(”%d%d”, &a, &b);

if(a>b)
+printf("\nA is largest…");
+else
+printf("\nB is largest…");

return 0;
+}
+output:

8) Find greatest of three number .

/* Largest of three number */

#include<stdio.h>
+int main() {
+int x, y, z, large;

printf(" Enter any three integer numbers for x, y, z : ") ;

scanf("%d %d %d", &x, &y, &z) ;

large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ;

printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ;

return 0;
+}
+output:

9) Program to assign grade to student according to percentage.

/* To find grade of a student by marks */

#include<stdio.h>
+int main() {

int s1,s2,s3,s4,s5,agg;
+float perc;

printf(“Enter the Marks in 5 Subjects Respectively:\n”);

scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);

agg=s1+s2+s3+s4+s5; // Aggregate Marks

perc=agg/500.0*100; // Perc Marks

if(perc>=90)
+{
+printf("\nA");

}

else if (perc>=80 && perc<90)
+{
+printf("\nB");

}

else if(perc>=70 && perc<80)
+{
+printf("\nC");

}

else if(perc>=60 && perc<70)
+{
+printf("\nD");
+}
+else if(perc>=50 && perc<60)
+{
+printf("\nE");
+}
+else
+{
+printf("\nScope of Improvement…");
+}
+return 0;
+}
+output:
+

11) Program to check year is leap or not.

/* To find whether year is leap or not */

#include<stdio.h>
+int main() {

int year,temp;

printf(“Enter teh year:”);
+scanf("%d",&year);

temp=year%4;

if(year%1000)
+{
+if(year%400
0)
+printf("\nLeap year…");
+}

else
+{
+if(year%4==0)
+printf("\nLeap year…");

else
+printf("\nNot a Leap year…");
+}

return 0;
+}
+output:

12) Program to print table of 5.

/* Table of 5 */

#include<stdio.h>

int main() { int res;

for(int i=1;i<=10;i++) {

res=5*i;

printf("\n5*%d=%d",i,res);
+}

return 0;
+}
+output:

13) To make simple calculator using switch case.

/* C Program to Create Simple Calculator using Switch Case */

#include <stdio.h>

int main() {
+char Operator;
+float num1, num2, result = 0;

printf("\n Please Enter an Operator (+, -, *, /) : “);
+scanf(”%c", &Operator);

printf("\n Please Enter the Values for two Operands: num1 and num2 : “);
+scanf(”%f%f", &num1, &num2);

switch(Operator)
+{
+case ‘+’:
+result = num1 + num2;
+break;
+case ‘-’:
+result = num1 - num2;
+break;
+case ‘*’:
+result = num1 * num2;
+break;
+case ‘/’:
+result = num1 / num2;
+break;
+default:
+printf("\n You have enetered an Invalid Operator ");
+}

printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result);

return 0;
+}
+output:

14) To calculate reverse of a number.

/* To find reverse of a Number*/

#include<stdio.h>
+int main() {

int num,rev=0;

printf("\nEnter the Number:");
+scanf("%ld",&num);

while(num!=0)
+{
+rev = rev * 10;
+rev = rev + num%10;
+num = num/10;
+}

printf("\nReversed number:%d",rev);

printf("\n\n");
+return 0;
+}
+output:

15) To check whether number is palindrome or not.

/* Palindrome */

#include<stdio.h>
+int main() {

int n,rev=0,check,rem;

printf("\nEnter the number:");
+scanf("%d",&n);
+check=n;

while( n!=0 )
+{
+rem = n%10;
+rev = rev*10 + rem;
+n /= 10;
+}

if(rev==check)
+printf("\nReversed number is equal to entered number…");

else
+printf("\nReversed number is not equal to entered number…");

printf("\n\n");
+return 0;
+}
+output:

16) To check whether a number is prime or not.

/* Program to check prime no. */

#include <stdio.h>
+#include <stdlib.h>

int main() {

int num, j, flag;

printf(“Enter a number \n”);
+scanf("%d", &num);

if (num <= 1)
+{
+printf("%d is not a prime numbers \n", num);
+exit(1);
+}
+flag = 0;
+for (j = 2; j <= num / 2; j++)
+{
+if ((num % j) == 0)
+{
+flag = 1;
+break;
+}
+}
+if (flag == 0)
+printf("%d is a prime number \n", num);
+else
+printf("%d is not a prime number \n", num);

return 0;

}
+output:![](https://)

17) Program to print prime number to 100.

/* Prime number from 1 to 100 */

#include<stdio.h>

int main(){

int numbr,k,remark;

printf(" The prime numbers between 1 and 100 : \n");

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

{

remark=0;

for(k=2;k<=numbr/2;k++){

if((numbr % k) == 0){

remark++;

break;
+}

}

if(remark==0)
+printf("\n %d ",numbr);

}

return 0;
+}
+output:

18) Program to check whether a number is armstrong or not.

/* To check armstrong number */

#include <stdio.h>
+int main()
+{
+int number, originalNumber, remainder, result = 0;

printf(“Enter a three digit integer: “);
+scanf(”%d”, &number);

originalNumber = number;

while (originalNumber != 0)
+{
+remainder = originalNumber%10;
+result += remainderremainderremainder;
+originalNumber /= 10;

}

if(result == number)
+printf("%d is an Armstrong number.",number);
+else
+printf("%d is not an Armstrong number.",number);

return 0;
+}
+output:![](https://)

19) Print Different Patterns.

i)

/* Pattern 1*/

#include <stdio.h>
+int main() {

int i,j,r;

printf(“Enter number of rows: “);
+scanf(”%d”,&r);

for(i=1; i<=r; ++i)
+{
+for(j=1; j<=i; ++j)
+{
+printf("%d “,j);
+}
+printf(”\n");
+}
+return 0;
+}
+output:![](https://)

ii)

/* Pattern 2*/

#include <stdio.h>
+int main() {

int r,i,j,num= 1;
+printf(“Enter number of rows: “);
+scanf(”%d”,&r);
+for(i=1;i<=r;i++)
+{
+for(j=1;j<=i;++j)
+{
+printf("%d",num);
+++num;
+}
+printf("\n");
+}
+return 0;
+}
+output:
+

20) Program to find largest from 1 dimensional array.

/* Largest in 1 dimensional array */

#include <stdio.h>
+int main() {

int i, n;
+float arr[100];

printf(“Enter total number of elements(1 to 100): “);
+scanf(”%d”, &n);
+printf("\n");

// Stores number entered by the user
+for(i = 0; i < n; ++i)
+{
+printf(“Enter Number %d: “, i+1);
+scanf(”%f”, &arr[i]);
+}
+// Loop to store largest number to arr[0]
+for(i = 1; i < n; ++i)
+{
+// Change < to > if you want to find the smallest element
+if(arr[0] < arr[i])
+arr[0] = arr[i];
+}
+printf(“Largest element = %.2f”, arr[0]);
+return 0;
+}
+output:

21) To find sumof the N natural numbers in an array.

/* Sum of N no.s in array */

#include<stdio.h>

int main() {
+printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
+int n, sum = 0, c, array[100];

printf(“Enter the number of integers you want to add: “);
+scanf(”%d”, &n);

printf("\n\nEnter %d integers \n\n", n);

for(c = 0; c < n; c++)
+{
+scanf("%d", &array[c]);
+sum += array[c]; // same as sum = sum + array[c]
+}

printf("\n\nSum = %d\n\n", sum);
+return 0;
+}
+output:

22) Program to add two matrices .

/* Addition of matrix */

#include <stdio.h>

int main() {

int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf(“Enter the number of rows and columns of matrix\n”);
+scanf("%d%d", &m, &n);

printf(“Enter the elements of first matrix\n”);

for (c = 0; c < m; c++)
+for (d = 0; d < n; d++)
+scanf("%d", &first[c][d]);

printf(“Enter the elements of second matrix\n”);

for (c = 0; c < m; c++)
+for (d = 0 ; d < n; d++)
+scanf("%d", &second[c][d]);

printf(“Sum of entered matrices:-\n”);

for (c = 0; c < m; c++) {
+for (d = 0 ; d < n; d++) {
+sum[c][d] = first[c][d] + second[c][d];
+printf("%d\t", sum[c][d]);
+}
+printf("\n");
+}

return 0;
+}
+output:

23) Program to multiply two matrices .

#include <stdio.h>
+int main()
+{
+int m, n, p, q, c, d, k, sum = 0;
+int first[10][10], second[10][10], multiply[10][10];

printf(“Enter number of rows and columns of first matrix\n”);
+scanf("%d%d", &m, &n);

printf(“Enter elements of first matrix\n”);

for (c = 0; c < m; c++)
+for (d = 0; d < n; d++)
+scanf("%d", &first[c][d]);

printf(“Enter number of rows and columns of second matrix\n”);
+scanf("%d%d", &p, &q);

if (n != p)
+printf(“The matrices can’t be multiplied with each other.\n”);

else
+{
+printf(“Enter elements of second matrix\n”);

for (c=0;c<p;c++)
+for (d = 0; d < q; d++)
+scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {
+for (d = 0; d < q; d++) {
+for (k = 0; k < p; k++) {
+sum = sum + first[c][k]*second[k][d];
+}

multiply[c][d] = sum;
+sum = 0;
+}
+}

printf(“Product of the matrices:\n”);

for (c = 0; c < m; c++) {
+for (d = 0; d < q; d++)
+printf("%d\t", multiply[c][d]);

printf("\n");
+}
+}

return 0;
+}
+output:

24) Program to check whether a string is palindrome or not .

#include<stdio.h>
+#include <string.h>

int main() {
+char s[1000];
+int i,n,c=0;

printf("Enter the string : ");
+gets(s);
+n=strlen(s);

for(i=0;i<n/2;i++)
+{
+if(s[i]==s[n-i-1])
+c++;

}
+

if(c==i)
+printf(“string is palindrome”);
+else
+printf(“string is not palindrome”);

return 0;
+}
+output:

25 Programs to swap two numbers using call by value and call by refernce.

call by refernce:-

#include <stdio.h>
+void swap(int*, int*);

int main() {

int x, y;

printf(“Enter the value of x and y\n”);
+scanf("%d%d",&x,&y);

printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);

swap(&x, &y);

printf(“After Swapping\nx = %d\ny = %d\n”, x, y);

return 0;
+}

void swap(int *a, int *b)
+{
+int temp;

temp = *b;
+*b = *a;
+*a = temp;
+}
+output:

call by value:-

#include <stdio.h>

void swap(int, int);

int main() {

int x, y;

printf(“Enter the value of x and y\n”);
+scanf("%d%d",&x,&y);

printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);

swap(x, y);

printf(“After Swapping\nx = %d\ny = %d\n”, x, y);

return 0;
+}

void swap(int a, int b) {
+int temp;

temp = b;
+b = a;
+a = temp;
+printf(“Values of a and b is %d %d\n”,a,b);
+}
+output:

27) Program to calculate factorial of a number with and without recursion both.

Recursion:-

#include<stdio.h>
+long int multiplyNumbers(int n);
+int main() {

int n;
+printf(“Enter a positive integer: “);
+scanf(”%d”, &n);
+printf(“Factorial of %d = %ld”, n, multiplyNumbers(n));
+return 0;
+}
+long int multiplyNumbers(int n)
+{
+if (n >= 1)
+return n*multiplyNumbers(n-1);
+else
+return 1;
+}
+output:

without recrsion:-

#include <stdio.h>

int main() {

int c, n, fact = 1;

printf(“Enter a number to calculate its factorial\n”);
+scanf("%d", &n);

for (c = 1; c <= n; c++)
+fact = fact * c;

printf(“Factorial of %d = %d\n”, n, fact);

return 0;
+}
+output:

28) Program to print fibonacci series with and without recursion both.

Without recursion:-

/* Program to print fibonaci series */

#include<stdio.h>
+void series(int); //prototype

int main() {

int n;

printf("\n\nEnter the number of terms you wish…");
+scanf("%d",&n);
+printf("\n\n");

series(n); // function call
+printf("\n\n\n");

return 0;
+}

void series(int n) // definition

{
+int t1=0,t2=1,next;

for(int i=0;i<=n;i++)
+{
+printf("%d\t",t1);

next=t1+t2;
+t1=t2;
+t2=next;
+

}
+}
+output:

with recursion:-

#include <stdio.h>
+int fibo(int);

int main() {

int num;
+int result;

printf(“Enter the nth number in fibonacci series: “);
+scanf(”%d”, &num);
+if (num < 0)
+{
+printf(“Fibonacci of negative number is not possible.\n”);
+}
+else
+{
+result = fibo(num);
+printf(“The %d number in fibonacci series is %d\n”, num, result);
+}
+return 0;
+}
+int fibo(int num)
+{
+if (num == 0)
+{
+return 0;
+}
+else if (num == 1)
+{
+return 1;
+}
+else
+{
+return(fibo(num - 1) + fibo(num - 2));
+}
+}
+output:

29) Program to calculate average of 5 numbers using function.

/* program to find average of 5 numbers */

#include<stdio.h>
+int avg(int,int,int,int,int); // prototype

int main() { int a1,a2,a3,a4,a5,res;

printf("\nEnter the numbers respectiively…");
+scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);

res=avg(a1,a2,a3,a4,a5); // function call
+printf(“Average of the numbers %d”,res);

return 0;
+}

int avg(int a1,int a2,int a3,int a4,int a5) // definition

{ int p;
+p=(a1+a2+a3+a4+a5)/5;
+return p;
+}
+output:

30) Program to implement linear serach and binary.

/* Program to implement linear search and Binary search */

#include <stdio.h>
+#include <stdlib.h>

int main() {

/* Declare variables - array_of_number,search_key,i,j,low,high*/

int array[100],search_key,i,j,n,low,high,location,choice;

void linear_search(int search_key,int array[100],int n);

void binary_search(int search_key,int array[100],int n);

clrscr();

/* read the elements of array */

printf(“ENTER THE SIZE OF THE ARRAY:”);

scanf("%d",&n);

printf(“ENTER THE ELEMENTS OF THE ARRAY:\n”);

for(i=1;i<=n;i++)
+{
+scanf("%d",&array[i]);

}

/* Get the Search Key element for Linear Search */

printf(“ENTER THE SEARCH KEY:”);

scanf("%d",&search_key);

/* Choice of Search Algorithm */

printf("___________________\n");

printf(“1.LINEAR SEARCH\n”);

printf(“2.BINARY SEARCH\n”);

printf("___________________\n");

printf(“ENTER YOUR CHOICE:”);

scanf("%d",&choice);

switch(choice)
+{
+case 1:
+linear_search(search_key,array,n);
+break;

case 2: binary_search(search_key,array,n);
+break;

default:

exit(0);

}

return 0;

}

/* LINEAR SEARCH */

void linear_search(int search_key,int array[100],int n)
+{

/*Declare Variable */

int i,location;

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

if(search_key == array[i])
+{

location = i;

printf("______________________________________\n");

printf(“The location of Search Key = %d is %d\n”,search_key,location);

printf("______________________________________\n");

}

}

}

/* Binary Search to find Search Key */

void binary_search(int search_key,int array[100],int n)
+{

int mid,i,low,high;

low = 1;

high = n;

mid = (low + high)/2;

i=1;

while(search_key != array[mid])
+{

if(search_key <= array[mid])
+{

low = 1;

high = mid+1;

mid = (low+high)/2;

}
+else
+{

low = mid+1;
+high = n;

mid = (low+high)/2;
+}

}

printf("__________________________________\n");

printf(“location=%d\t”,mid);

printf(“Search_Key=%d Found!\n”,search_key);

printf("__________________________________\n");

}
+output:

31) Program to implement bubble sort.

/* Bubble sort implementation */

#include <stdio.h>

int main()
+{
+int array[100], n, c, d, swap;

printf(“Enter number of elements\n”);
+scanf("%d", &n);

printf(“Enter %d integers\n”, n);

for (c = 0; c < n; c++)
+scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)
+{
+for (d = 0 ; d < n - c - 1; d++)
+{
+if (array[d] > array[d+1]) /* For decreasing order use < */
+{
+swap = array[d];
+array[d] = array[d+1];
+array[d+1] = swap;
+}
+}
+}

printf(“Sorted list in ascending order:\n”);

for (c = 0; c < n; c++)
+printf("%d\n", array[c]);

return 0;
+}
+output:

32) Program to store information of 10 students using array of structures.

#include<stdio.h>
+struct student
+{
+char name[20],address[30];
+int grade,roll,dob;
+};

int main()
+{
+struct student s[10];
+int i;
+for(i=0;i<10;i++)
+{
+printf("\nEnter records for student[%d]\n",i+1);
+printf(“Enter name: “);
+gets(s[i].name);
+printf(“Enter address: “);
+gets(s[i].address);
+printf(“Enter class, roll number and date of birth(year): “);
+scanf(”%d%d%d”,&s[i].grade,&s[i].roll,&s[i].dob);
+}
+printf(“Records of the 10 students are here”);
+for(i=0;i<10;i++)
+{
+printf(”\nStudent %d”,i+1);
+printf(”\nName: %s”,s[i].name);
+printf("\nAddress: %s",s[i].address);
+printf("\nClass: %d",s[i].grade);
+printf("\nRoll No.: %d",s[i].roll);
+printf("\nDOB: %d",s[i].dob);
+printf("\n");
+}
+return 0;
+}
+output:

33) Programs to compute the transpose of a matrix.

#include <stdio.h>
+int main()
+{
+int a[10][10], transpose[10][10], r, c, i, j;
+printf(“Enter rows and columns of matrix: “);
+scanf(”%d %d”, &r, &c);
+// Storing elements of the matrix
+printf("\nEnter elements of matrix:\n");
+for(i=0; i<r; ++i)
+for(j=0; j<c; ++j)
+{
+printf(“Enter element 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 the transpose of matrix a
+for(i=0; i<r; ++i)
+for(j=0; j<c; ++j)
+{
+transpose[j][i] = a[i][j];
+}
+// Displaying the transpose of matrix a
+printf("\nTranspose of Matrix:\n");
+for(i=0; i<c; ++i)
+for(j=0; j<r; ++j)
+{
+printf("%d “,transpose[i][j]);
+if(j==r-1)
+printf(”\n\n");
+}
+return 0;
+}
+output:

34) Program to print the address of variable using pointer.

#include <stdio.h>
+int main() {
+int a;
+int *pt;

a = 10;
+pt = &a;

printf("\n[&a ]:Address of A = %p", &a);

return 0;
+}
+output:
+

35) Program to access array using pointer.

#include <stdio.h>
+int main()
+{
+int data[5],i;
+printf(“Enter elements: “);
+for(i = 0; i < 5; ++i)
+scanf(”%d”, data + i);
+printf(“You entered: \n”);
+for(i = 0; i < 5; ++i
+printf("%d\n", *(data + i));
+return 0;
+}
+output:

+ + + + + + + + + diff --git a/PPS MD File.md b/PPS MD File.md new file mode 100644 index 0000000..571d6ef --- /dev/null +++ b/PPS MD File.md @@ -0,0 +1,1191 @@ +## **PROGRAMMING FOR PPS SOLVING** +### **NAME: PRIYANSHI TANGRI** +### **CRN: 1915340** +### **BRANCH: CSE C1** +### **SUBMITTED TO: MS GOLDENDEEP MAM** +___ + + ### 1. TO PRINT NAME USING PUTS + + #include + int main() + { + puts("My Name is Priyanshi Tangri\n"); + return 0; + } + +![](https://i.imgur.com/2ELqDID.jpg) + + + + + +### 2. TO PRINT COLLEGE NAME + #include + int main() + { + puts("Guru Nanak Dev Engineering College,\n"); + puts("Gill Road,\n"); + puts("Ludhiana,Punjab,\n"); + return 0; + } +![](https://i.imgur.com/8NDJAs8.jpg) + + + +### 3. TO ADD TWO NUMBERS + + #include + int main() { + + int a,b; + + printf("\nEnter the numbers...."); + + printf("\nA:"); + scanf("%d",&a); + + printf("\nB:"); + scanf("%d",&b); + + a=a+b; + + printf("\n Sum of the number is %d/n ",a); + + return 0; + + } + +![](https://i.imgur.com/8bgsWYf.jpg) + + + + +### 4. TO FIND QUOTIENT AND REMAINDER + + + #include + int main(){ + int dividend, divisor, quotient, remainder; + printf("Enter dividend: "); + scanf("%d", ÷nd); + printf("Enter divisor: "); + scanf("%d", &divisor); + + quotient = dividend / divisor; + + remainder = dividend % divisor; + printf("Quotient = %d\n", quotient); + printf("Remainder = %d\n", remainder); + return 0; + } +![](https://i.imgur.com/i50Awxh.jpg) + +### 5.SWAP TWO VARIABLES WITHOUT THIRD VARIABLE + + #include + int main() + { + int a=30, b=20; + printf("Before swap a=%d b=%d\n",a,b); + a=a+b;//a=50 (30+20) + b=a-b;//b=30 (50-20) + a=a-b;//a=20 (50-30) + printf("After swap a=%d b=%d\n",a,b); + return 0; + } +![](https://i.imgur.com/cGafDH9.jpg) + +### 6. TO FIND IF NUMBER IS EVEN OR ODD + + #include + int main() + { + int number; + printf("Enter an integer: "); + scanf("%d", &number); + if(number % 2 == 0) + printf("%d is even.\n", number); + else + printf("%d is odd.\n", number); + return 0; + } +![](https://i.imgur.com/MfsZkYq.jpg) + +### 7. FINDING GREATEST OF TWO NUMBERS + + + + #include + + int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + + if(a>b) + printf("\nA is largest.....\n"); + else + printf("\nB is largest.....\n"); + + return 0; + } +![](https://i.imgur.com/HW8y0ag.jpg) + +### 8. LARGEST OF THREE NUMBER + + #include + int main() + { + double n1, n2, n3; + printf("Enter three different numbers: "); + scanf("%lf %lf %lf", &n1, &n2, &n3); + if( n1>=n2 && n1>=n3 ) + printf("%.2f is the largest number.\n", n1); + if( n2>=n1 && n2>=n3 ) + printf("%.2f is the largest number.\n", n2); + if( n3>=n1 && n3>=n2 ) + printf("%.2f is the largest number.\n", n3); + return 0; + } +![](https://i.imgur.com/SoHaLpP.jpg) + + + +### 9. FIND GRADES OF STUDENT + + #include + void main() + { + int marks; + printf("Enter your marks "); + scanf("%d",&marks); + if(marks<0 || marks>100) + { + printf("Wrong Entry"); + } + else if(marks<50) + { + printf("Grade F \n"); + } + else if(marks>=50 && marks<60) + { + printf("Grade D \n"); + } + else if(marks>=60 && marks<70) + { + printf("Grade C \n"); + } + else if(marks>=70 && marks<80) + { + printf("Grade B \n"); + } + else if(marks>=80 && marks<90) + { + printf("Grade A \n"); + } + else + { + printf("Grade A+ \n"); + } + + +![](https://i.imgur.com/mnST198.jpg) +### 10. TO PRINT TABLE OF FIVE + + #include + + main() { int res; + + for(int i=1;i<=10;i++) { + + res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; + } +![](https://i.imgur.com/99dtuHh.jpg) +### 11. TO MAKE SIMPLE CALCULATOR USING SWITCH CASE + + #include + int main() { + char operator; + double firstNumber,secondNumber; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf",&firstNumber, &secondNumber); + switch(operator) + { + case '+': + printf("%.1lf + %.1lf = %.1lf \n",firstNumber, secondNumber, firstNumber + secondNumber); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber); + break; + default: + printf("Error! operator is not correct"); + } + + return 0; + + } + +![](https://i.imgur.com/poos0oT.jpg) + + + + +### 12. TO CALCULATE REVERSE OF A NUMBER + + + #include + + int main() + { + int n, reverse = 0; + + printf("Enter a number to reverse\n"); + scanf("%d", &n); + + while (n != 0) + { + reverse = reverse * 10; + reverse = reverse + n%10; + n = n/10; + } + + printf("Reverse of entered number is = %d\n", reverse); + + return 0; + } +![](https://i.imgur.com/AfMkJgA.jpg) + + +### 13. TO CHEAK WHETHER NUMBER IS PALINDROME OR NOT + + + #include + + int main() + { + int n, r = 0, t; + + printf("Enter a number to check if it is a palindrome or not\n"); + scanf("%d", &n); + + t = n; + + while (t != 0) + { + r = r * 10; + r = r + t%10; + t = t/10; + } + + if (n == r) + printf("%d is a palindrome number.\n", n); + else + printf("%d isn't a palindrome number.\n", n); + + return 0 ; + } +![](https://i.imgur.com/Y65SB8z.jpg) + + +### 14. TO CHEAK NUMBER IS PRIME OR NOT + + + + #include + int main() + { + int i, num, p = 0; + printf("Please enter a number: \n"); + scanf("%d", &num); + for(i=1; i<=num; i++) + { + if(num%i==0) + { + p++; + } + } + if(p==2) + { + printf("Entered number is %d "\ + "and it is a prime number.",num); + } + else + { + printf("Entered number is %d "\ + "and it is not a prime number.",num); + } + return 0; + } +![](https://i.imgur.com/LSTe3Pu.jpg) + +### 15. PRIME NUMBERS BETWEEN 1-100 + + #include + + int main() + { + int i, Number, count; + + printf(" Prime Number from 1 to 100 are: \n"); + for(Number = 1; Number <= 100; Number++) + { + count = 0; + for (i = 2; i <= Number/2; i++) + { + if(Number%i == 0) + { + count++; + break; + } + } + if(count == 0 && Number != 1 ) + { + printf(" %d ", Number); + } + } + return 0; + } + +![](https://i.imgur.com/2AIwERo.jpg) + + +### 16. TO CHEAK WHETHER A NUMBER IS ARMSTRONG OR NOT + + #include + int main() + { + int n,r,sum=0,temp; + printf("enter the number="); + scanf("%d",&n); + temp=n; + while(n>0) + { + r=n%10; + sum=sum+(r*r*r); + n=n/10; + } + if(temp==sum) + printf("armstrong number\n"); + else + printf("not armstrong number\n"); + return 0; + } +![](https://i.imgur.com/Q5skgsj.jpg) + ### 17. TO PRINT DIFFRENT PATTERN + + #include + int main() + { + int i, j, rows; + printf("Enter number of rows: "); + scanf("%d",&rows); + for(i=1; i<=rows; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + } + +![](https://i.imgur.com/FZXbzr1.jpg) +### 18. PATTERN PRINTING + + #include + + int main() + { + int n, i, c, a = 1; + + printf("Enter the number of rows:"); + scanf("%d", &n); + + for (i = 1; i <= n; i++) + { + for (c = 1; c <= i; c++) + { + printf("%d ",a); + a++; + } + printf("\n"); + } + + return 0; + } + +![](https://i.imgur.com/ZFDelrZ.jpg) + +### 19. PATTERN PRINTING + + + #include + int main() + { + int i, space, rows, k=0, count = 0, count1 = 0; + printf("Enter 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"); + } + return 0; + } + +![](https://i.imgur.com/EcQXiNK.jpg) + +### 20. TO FIND LARGEST FROM ONE DIMENSTIONAL ARRAY + + #include + + int main() + { + + int array[50], size, i, largest; + + printf("Enter the size of the array: "); + scanf("%d", &size); + + printf("Enter %d elements of the array: ", size); + + for (i = 0; i < size; i++) + scanf("%d", &array[i]); + + largest = array[0]; + + for (i = 1; i < size; i++) + { + if (largest < array[i]) + largest = array[i]; + } + + printf("Largest element present in the given array is : %d\n", largest); + + return 0; + + } +![](https://i.imgur.com/v11LdF4.jpg) + + +### 21. TO FIND SUM OF N NATURAL NUMBERS + + + + #include + int main() + { + int n, i, sum = 0; + + printf("Enter a positive integer: "); + scanf("%d",&n); + for(i=1; i <= n; ++i) + { + sum += i; + } + printf("Sum = %d\n",sum); + return 0; + } +![](https://i.imgur.com/120XzeI.jpg) + ### 22. ADDITION OF TWO MATRICES + + #include + + int main() + { + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; + } +![](https://i.imgur.com/sQA1fGN.jpg) + + +### 23. TO CHEAK WHETHER STRING IS PALINDROME OR NOT + + + + #include + #include + + int main(){ + char string1[20]; + int i, length; + int flag = 0; + + printf("Enter a string:"); + scanf("%s", string1); + + length = strlen(string1); + + for(i=0;i < length ;i++){ + if(string1[i] != string1[length-i-1]){ + flag = 1; + break; + } + } + + if (flag) { + printf("%s is not a palindrome\n", string1); + } + else { + printf("%s is a palindrome\n", string1); + } + return 0; + } +![](https://i.imgur.com/h1u6lRp.jpg) + + +### 24. PROGRAMS TO SWAP TWO NUMBERS BY CALL BY VALUE + + #include + void swap(int*, int*); + + int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; + } + + void swap(int *a, int *b) + { + int temp; + + temp = *b; + *a = temp; + } + +![](https://i.imgur.com/94tE8Ay.jpg) +### 24. SWAP TWO NUMBERS B CALL BY REFERENCE + + #include + + void swap(int, int); + + int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; + } + + void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); + } +![](https://i.imgur.com/GI6wChh.jpg) +### 25. FACTORIAL WITH RECURSION + + #include + long int multi(int n); + int main() + { + int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld\n", n, multi(n)); + return 0; + } + long int multi(int n) + { + if (n >= 1) + + } +![](https://i.imgur.com/dXqnzzk.jpg) + + + ### 26.FACTORIAL WITHOUT RECURSION + + #include + + int main() + { + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + printf("Factorial of %d = %d\n", n, fact); + + + return 0; + } + +[](https://)https://i.imgur.com/wMEOv0Y.jpg + +### 27. TO FIND FIBONACCI SERIES WITH RECURSION + + + #include + + int Fibonacci(int); + + int main() + { + int n, i = 0, c; + printf("Enter Number till you want the series:\n"); + scanf("%d",&n); + + printf("Fibonacci series\n"); + + for ( c = 1 ; c <= n ; c++ ) + { + printf("%d\n", Fibonacci(i)); + i++; + } + + return 0; + } + + int Fibonacci(int n) + { + if ( n == 0 ) + return 0; + else if ( n == 1 ) + return 1; + else + return ( Fibonacci(n-1) + Fibonacci(n-2) ); + } +![](https://i.imgur.com/PIjGj8S.jpg) + + + +### 28. TO FIND FIBONACCI SERIES WITHOUT RECURSION + + + #include + int main() + { + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("\n%d %d",n1,n2); + for(i=2;i + float average(int a, int b, int c, int d, int e){ + return (float)(a+b+c+d+e)/5; + } + int main() + { + int num1, num2, num3, num4, num5; + float avg; + + printf("Enter first number: "); + scanf("%d",&num1); + printf("Enter second number: "); + scanf("%d",&num2); + printf("Enter third number: "); + scanf("%d",&num3); + printf("Enter fourth number: "); + scanf("%d",&num4); + printf("Enter fifth number: "); + scanf("%d",&num5); + + + + avg = average(num1, num2, num3, num4, num5); + + + printf("Average of 5 numbers is: %.2f\n",avg); + return 0; + } +![](https://i.imgur.com/Q3nvbeK.jpg) + +### PROGRAMING TO IMPLEMENT LINEAR SEARCH + + #include + + int main() + { + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; + } +![](https://i.imgur.com/G0VioUI.jpg) + +### 31. TO IMPLEMENT BINARY SEARCH + + #include + + int main() + { + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; + } + +![](https://i.imgur.com/AVqCmwN.jpg) + + +### 32. TO SORT LIST USING BUBBLE SORT + + #include + + int main() + { + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; + } +![](https://i.imgur.com/AxC1w4O.jpg) + +### 33. DISPLAYING STUDENT INFORMATION USING ARRAY + + #include + struct student + { + char name[50]; + int roll; + float marks; + } s[10]; + int main() + { + int i; + printf("Enter information of students:\n"); + for(i=0; i<3; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying Information:\n\n"); + for(i=0; i<3; ++i) + { + printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } + return 0; + } +![](https://i.imgur.com/PP6QAp7.jpg) + + +### 34. TO FIND TRANSPOSE OF A MATRIX + + + #include + int main() + { + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + printf("\nEnter elements of matrix:\n"); + for(i=0; i + int main() { + int b; + int *pt; + + b = 10; + pt = &b; + + printf("\n[&a ]:Address of B = %p\n", &b); + return 0; + } + +![](https://i.imgur.com/SxrQvCN.jpg) + + ### 36. TO ACCESS ARRAY USING POINTER + + + + #include + int main() + { + int data[5], i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; + } + ![](https://i.imgur.com/wdqZRu6.jpg) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PPS.md b/PPS.md deleted file mode 100644 index 3c07ec1..0000000 --- a/PPS.md +++ /dev/null @@ -1,1013 +0,0 @@ -![College Logo](https://www.gndec.ac.in/logo.png) - -# **Programming for Problem Solving** -## **Name:- Konark Lohat** -## **CRN:-1915328** -## **Branch:- CSE-C1** -## **Submitted To:- Ms. Goldendeep Kaur** ---- - -### 1) To print name. -```C -#include -int main() -{ - puts("~~~~~~~~~~~~~~~~~~~~~~~~~"); - puts("My name is Konark Lohat"); - puts("~~~~~~~~~~~~~~~~~~~~~~~~~"); - return 0; -} -``` -![1.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/01.JPG) -### 2) To print College address. -```C -#include -int main() -{ - printf("Guru Nanak Dev Engineering College,\nGill Road,\nLudhiana, Punjab"); - return 0; -} -``` -![2.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/02.JPG) -### 3) Program to add two integers. -```C -#include -int main() -{ - int a,b,sum; - printf("Enter the value of first integer: "); - scanf("%d", &a); - printf("Enter the value of second integer: "); - scanf("%d", &b); - sum = a + b; - printf("Sum is %d", sum); - return 0; -} -``` -![3.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/03.JPG) -### 4) Program to find quotient and remainder. -```C -#include -int main() -{ - int divisor, dividend, quotient, remainder; - printf("Enter the value of divisor: "); - scanf("%d", &divisor); - printf("Enter the value of dividend: "); - scanf("%d", ÷nd); - quotient = dividend/divisor; - remainder = dividend%divisor; - printf("Qoutient is %d\n", quotient); - printf("Remainder is %d", remainder); - return 0; -} -``` -![4.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/04.JPG) -### 5) Program to swap two variables without 3rd variable. -```C -#include -int main() -{ - int a,b; - printf("Enter the value of a = "); - scanf("%d", &a); - printf("Enter the value of b = "); - scanf("%d", &b); - a = a + b; - b = a - b; - a = a - b; - printf("~~~~~~~~~~~~~~~~\nAfter Swap\n~~~~~~~~~~~~~~~~\n"); - printf("Value of a = %d\nValue of b = %d", a,b); - return 0; -} -``` -![5.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/05.JPG) -### 6) Program to check even odd number. -```C -#include -int main() -{ - int number; - printf("Enter the number: "); - scanf("%d", &number); - if (number % 2 == 0) - printf("Number is Even"); - else - printf("Number is Odd"); - return 0; -} -``` -![6.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/06.JPG) -### 7) Finding greteast of two numbers. -```C -#include -int main() -{ - int num1, num2; - printf("Enter the first number: "); - scanf("%d", &num1); - printf("Enter the second number: "); - scanf("%d", &num2); - if (num1 > num2) - printf("First Number is Greatest i.e %d", num1); - else - printf("Second Number is Greatest i.e %d", num2); - return 0; -} -``` -![7.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/07.JPG) -### 8) Find greatest of three number . -```C -#include -int main() -{ - int a,b,c; - printf("Enter the value of a "); - scanf("%d", &a); - printf("Enter the value of b "); - scanf("%d", &b); - printf("Enter the value of c "); - scanf("%d", &c); - if (a>b) - { - if(a > c) - printf("a is the greatest"); - else - printf("c is the greatest"); - } - else - { - if (b > c) - printf("b is the greatest"); - else - printf("c is the greatest"); - - } - return 0; - -} -``` -![8.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/08.JPG) -### 9) Program to assign grade to student according to percentage. -```C -#include -int main() -{ - int marks; - printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nSchema for marks and grades\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nMarks\tGrade\n0-30\tF\n31-50\tD\n51-70\tC\n71-80\tB\n81-90\tA2\n91-100\tA1\n"); - printf("Enter is the marks (Out of 100): "); - scanf("%d", &marks); - if (marks>=0) - { - if (marks <= 90) - { - if (marks <= 80) - { - if (marks <= 70) - { - if (marks <= 50) - { - if (marks <= 30) - printf("Grade is F"); - else - printf("Grade is D"); - } - else - printf("Grade is C"); - } - else - printf("Grade is B"); - } - else - printf("Grade is A2"); - } - else - printf("Grade is A1"); - } - else - printf("Invalid Input"); - return 0; -} -``` -![9.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/09.JPG) -### 10) Program to print roots of quadratic equation. -```C -#include -#include -int main() -{ - int a,b,c,root1,root2; - printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nSchema of a Quadratic Equation is a(x^2) + b(x) + c = 0\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); - printf("Enter is value of a "); - scanf("%d", &a); - printf("Enter is value of b "); - scanf("%d", &b); - printf("Enter is value of c "); - scanf("%d", &c); - root1 = ((-b) + sqrt((b*b) - (4*a*c)))/(2*a); - root2 = ((-b) - sqrt((b*b) - (4*a*c)))/(2*a); - printf("Roots are %d and %d", root1,root2); - return 0; -} -``` -![10.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/10.JPG) -### 11) Program to check year is leap or not. -```C -#include -int main() -{ - int year; - printf("Enter the year: "); - scanf("%d", &year); - if (year%4 == 0) - printf("%d is a Leap Year", year); - else - printf("%d is not a Leap Year", year); - return 0; -} -``` -![11.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/11.JPG) -### 12) Program to print table of 5. -```C -#include -int main() -{ - int num,x; - printf("Enter the number of mutiples of 5 you want: "); - scanf("%d", &num); - for (x=1; x<=num; x++) - { - printf("5\t*\t%d\t=\t%d\n", x,x*5); - } - return 0; -} -``` -![12.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/12.JPG) -### 13) To make simple calculator using switch case. -```C -#include -int main() -{ - double num1,num2; - char operator; - printf("Select the Operator (+ - * /)"); - scanf("%c", &operator); - printf("\nEnter the numbers: "); - scanf("%lf %lf", &num1,&num2); - switch(operator) - { - case '+': - printf("%.2lf + %.2lf = %.2lf", num1,num2,num1+num2); - break; - case '-': - printf("%.2lf - %.2lf = %.2lf", num1,num2,num1-num2); - break; - case '*': - printf("%.2lf * %.2lf = %.2lf", num1,num2,num1*num2); - break; - case '/': - printf("%.2lf / %.2lf = %.2lf", num1,num2,num1/num2); - break; - default: - printf("Invalid Operator"); - } - - return 0; -} -``` -![13.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/13.JPG) -### 14) To calculate reverse of a number. -```C -#include -int main() -{ - int num, reverse=0; - printf("Enter the number: "); - scanf("%d", &num); - while(num != 0) - { - reverse = reverse * 10; - reverse = reverse + num%10; - num = num/10; - } - printf("Reverse of the Entered Number is %d", reverse); - return 0; -} -``` -![14.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/14.JPG) -### 15) To check whether number is palindrome or not. -```C - #include -int main() -{ - int num,original, reverse=0; - printf("Enter the number: "); - scanf("%d", &num); - original = num; - while(num != 0) - { - reverse = reverse * 10; - reverse = reverse + num%10; - num = num/10; - } - if (original == reverse) - printf("Number is a palindrome"); - else - printf("Number is not a palindorme"); - return 0; -} -``` -![15.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/15.JPG) -### 16) To check whether a number is prime or not. -```C -#include -int main() -{ - int num, k=0, x; - printf("Enter the number: "); - scanf("%d", &num); - for (x=1; x<=num; x++) - { - if (num%x == 0) - { - k++; - } - } - if (k == 2) - printf("Number is a Prime Number"); - else - { - if (k == 1) - printf("Number is neither a prime nor a composite"); - else - printf("Number is not a prime number"); - } -} -``` -![16.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/16.JPG) -### 17) Program to print prime numbers from 1 to 100. -```C -#include - -int main() -{ - int i, Number, count; - - printf(" Prime Number from 1 to 100 are: \n"); - for(Number = 1; Number <= 100; Number++) - { - count = 0; - for (i = 2; i <= Number/2; i++) - { - if(Number%i == 0) - { - count++; - break; - } - } - if(count == 0 && Number != 1 ) - { - printf(" %d ", Number); - } - } - return 0; -} - ``` - ![17.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/17.JPG) -### 18) Program to check whether a number is armstrong or not. -```C -#include -int main() -{ - int number,remainder,sum=0,original_number; - printf("Enter the Number: "); - scanf("%d", &number); - original_number = number; - while(number>0) - { - remainder = number%10; - sum = sum+(remainder*remainder*remainder); - number = number/10; - } - if (original_number == sum) - printf("Number is a Armstrong Number"); - else - printf("Number is not a Amrstrong Number"); - return 0; -} - -``` -![18.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/18.JPG) -### 19) Print the following patterns: -### i) Pattern 1. -```C -#include -int main() -{ - int x,y; - for(x=1; x<5; x++) - { - for (y=1; y<=x; y++) - { - printf("%d", y); - } - printf("\n"); - } - return 0; -} -``` -![19_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19a.JPG) - -### ii) Pattern 2. -```C -#include -int main() -{ - int x,y,z=1; - for (x=1; x<5; x++) - { - for (y=1; y<=x; y++) - { - printf("%d", z++); - } - printf("\n"); - } - return 0; -} -``` -![19_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19b.JPG) - -### iii) Pattern 3. -```C -#include -int main() -{ - int p,q,r,s,t; - for (p=1; p<5; p++) - { - for (q=1; q<5-p; q++) - { - printf(" "); - } - for (r=1; r<=p; r++) - { - printf("%d", r); - } - for (s=p-1; s>=1; s--) - { - printf("%d", s); - } - printf("\n"); - } - return 0; -} -``` -![19_3.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/19c.JPG) -### 20) Program to find largest from 1 dimensional array. -```C -#include -int main() -{ - int i, n, arr[5],largest; - for(i = 0; i < 6; i++) - { - printf("Enter Number %d: ", i); - scanf("%d", &arr[i]); - } - for(i = 0; i < 6; i++) - { - if(largest < arr[i]) - largest = arr[i]; - } - printf("Largest element = %d", largest); - return 0; -} -``` -![20.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/20.JPG) - -### 21) To find sumof the N natural numbers in an array. -```C -#include -int main() -{ - int arr[1000],n,x,y,sum=0; - printf("Enter the number of elements you want to input(From 1 to 1000): "); - scanf("%d", &n); - for (x=0; x -int main() -{ - int A[4][4] = { {1, 1, 1, 1},{2, 2, 2, 2},{3, 3, 3, 3}, {4, 4, 4, 4}}; - - int B[4][4] = { {1, 1, 1, 1}, {2, 2, 2, 2},{3, 3, 3, 3},{4, 4, 4, 4}}; - - int C[4][4]; - int i, j; - int k, l; - for (k = 0; k < 4; k++) - { - for (l = 0; l < 4; l++) - { - C[k][l] = A[k][l] + B[k][l]; - } - } - printf("Result matrix is \n"); - for (i = 0; i < 4; i++) - { - for (j = 0; j < 4; j++) - { - printf("%d ", C[i][j]); - } - printf("\n"); - } - - return 0; -} -``` -![22.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/22.JPG) -### 23) Program to multiply two matrices . -```C -#include -int main() -{ - int mat1[4][4] = { {1, 1, 1, 1},{2, 2, 2, 2},{3, 3, 3, 3},{4, 4, 4, 4}}; - - int mat2[4][4] = { {1, 1, 1, 1},{2, 2, 2, 2},{3, 3, 3, 3},{4, 4, 4, 4}}; - - int res[4][4]; - int i, j, k; - for (i = 0; i < 4; i++) - { - for (j = 0; j < 4; j++) - { - res[i][j] = 0; - for (k = 0; k < 4; k++) - { - res[i][j] += mat1[i][k]*mat2[k][j]; - } - } - } - printf("Result matrix is \n"); - for (i = 0; i < 4; i++) - { - for (j = 0; j < 4; j++) - { - printf("%d ", res[i][j]); - } - printf("\n"); - } - - return 0; -} -``` -![23.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/23.JPG) -### 24) Program to check whether a string is palindrome or not . -```C -#include -#include -int main() -{ - int a,b,c,shit=0; - char str[9]; - printf("Enter the string you want to check"); - scanf("%s", str); - for (a=0; a -#include -int main() -{ - int s1[10],s2[10],s3[40],i,j; - printf("Enter the string 1 (MAX. is 10 Elements): "); - scanf("%s", s1); - printf("Enter the string 2 (MAX. is 10 Elements): "); - scanf("%s", s2); - if(strcmp(s1,s2)==0) - printf("Entered strings are same\n"); - else - printf("Entered strings are not same\n"); - printf("Length of string 1 and string 2 is %d and %d respectively\n", strlen(s1),strlen(s2)); - printf("Concatenated string is %s\n", strcat(s1,s2)); - printf("New Copied string from string 2 by strcpy() is %s\n", strcpy(s3,s2)); - printf("Reverse of string 2 is %s",strrev(s2)); - return 0; -} -``` -![25.jpg](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/25.JPG) -### 26) Programs to swap two numbers using call by value and call by refernce. - -### Call by reference -```C -/* Call by reference */ - -#include -void swap(int*, int*); - -int main() { - - int x, y; - - printf("Enter the value of x and y\n"); - scanf("%d%d",&x,&y); - - printf("Before Swapping\nx = %d\ny = %d\n", x, y); - - swap(&x, &y); - - printf("After Swapping\nx = %d\ny = %d\n", x, y); - - return 0; -} - -void swap(int *a, int *b) -{ - int temp; - - temp = *b; - *b = *a; - *a = temp; -} -``` -![26_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/26.JPG) -### call by value:- -```C -/* Call by value */ - -#include - -void swap(int, int); - -int main() { - - int x, y; - - printf("Enter the value of x and y\n"); - scanf("%d%d",&x,&y); - - printf("Before Swapping\nx = %d\ny = %d\n", x, y); - - swap(x, y); - - printf("After Swapping\nx = %d\ny = %d\n", x, y); - - return 0; -} - -void swap(int a, int b) { - int temp; - - temp = b; - b = a; - a = temp; - printf("Values of a and b is %d %d\n",a,b); -} -``` -![26_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/26b.JPG) - -### 27) Program to calculate factorial of a number with and without recursion both. -```C -/* Recursion */ - -#include -long long int factorial(long long int x); -int main() -{ - long int number; - printf("Enter the number of which you want to know the factorial: "); - scanf("%d", &number); - printf("Factorial of the Entered Number is %d", factorial(number)); - return 0; -} -long long int factorial(long long int x) -{ - if (x>=1) - return x*factorial(x-1); - else - return 1; -} -``` -![27_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/27a.JPG) - -```C -#include -int main() -{ - int number,factorial=1,x; - printf("Enter the number: "); - scanf("%d", &number); - for (x=1; x<=number; x++) - { - factorial = factorial*x; - } - printf("Factorial of %d is %d", number, factorial); - return 0; -} -``` -![27_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/27b.JPG) - -### 28) Program to print fibonacci series with and without recursion both. -```C -#include -int fib(int j); -int main() -{ - int n,i,j=0; - printf("Enter the Number of terms you want to print: "); - scanf("%d", &n); - printf("Fibonacci Series upto %d terms are: ", n); - for (i=1; i<=n; i++) - { - printf("%d\n", fib(j)); - j++; - } - return 0; -} -int fib(int j) -{ - if (j == 1 || j == 0) - return j; - else - return (fib(j-1) + fib(j-2)); -} -``` -![28_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/28a.JPG) -```C -#include -int main() -{ - int i,j,n, f[100000]={0,1}; - printf("Enter the Number upto which you want to print the fibonacci seires"); - scanf("%d", &n); - for (i=0; i -int average(); -int main() -{ - int a,b,c,d,e; - printf("Enter the 5 values of whom you want to calculate average"); - scanf("%d %d %d %d %d", &a,&b,&c,&d,&e); - printf("Average is %d", average(a,b,c,d,e)); - return 0; -} -int average(int a, int b, int c, int d, int e) -{ - return ((a+b+c+d+e)/5); -} - ``` -![29.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/29.JPG) -### 30) Program to implement linear serach and binary. - -### Linear Search Program -```C -#include -int main() -{ - int a[99999],i,j,s,k; - printf("Enter the numbers you want to enter: "); - scanf("%d", &i); - for (j=0; j -int main() -{ - int number, first, middle, last, search, i, arr[9999]; - printf("Enter the value of numbers you want to enter: "); - scanf("%d", &number); - printf("Enter those %d values: \n", number); - for (i=0; ilast) - printf("%d is not present in the above list", search); - return 0; -} -``` - -![30_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/30b.JPG) - -### 31) Program to implement bubble sort. -```C -#include - -int main() -{ - int array[100], n, c, d, swap; - - printf("Enter number of elements\n"); - scanf("%d", &n); - - printf("Enter %d integers\n", n); - - for (c = 0; c < n; c++) - scanf("%d", &array[c]); - - for (c = 0 ; c < n - 1; c++) - { - for (d = 0 ; d < n - c - 1; d++) - { - if (array[d] > array[d+1]) - { - swap = array[d]; - array[d] = array[d+1]; - array[d+1] = swap; - } - } - } - - printf("Sorted list in ascending order:\n"); - - for (c = 0; c < n; c++) - printf("%d\n", array[c]); - - return 0; -} -``` -![31.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/31.JPG) -### 32) Program to store information of 10 students using array of structures. -```C -#include -struct student -{ - char name[50]; - int roll; - int age; - int marks; - char sex; -} s[5]; -int main() -{ - int i; - printf("Enter the information of students: \n"); - for (i=0; i<5; i++) - { - s[i].roll = i+1; - printf("\nFor Roll Number %d: \n", s[i].roll); - printf("Enter Name: "); - scanf("%s", &s[i].name); - printf("Enter Age: "); - scanf("%d", &s[i].age); - printf("Enter Marks: "); - scanf("%d", &s[i].marks); - printf("Enter the Sex:"); - scanf("%s", &s[i].sex); - printf("\n\n"); - } - printf("Information Entered is: \n\n"); - for (i=0; i<5; i++) - { - printf("\nRoll Number: %d\n", i+1); - printf("Name: %s\n", s[i].name); - printf("Age: %d\n", s[i].age); - printf("Marks: %d\n", s[i].marks); - printf("Sex: %c\n", s[i].sex); - printf("\n\n"); - } - return 0; -} -``` -![32_1.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/32.JPG) -![32_2.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/32_cont.JPG) -### 33) Programs to compute the transpose of a matrix. -```C -#include -int main() -{ - int m1[3][3]={{1,1,1},{2,2,2},{3,3,3}}, m1t[3][3], i,j; - for (i=0; i<3; i++) - { - for (j=0; j<3; j++) - { - m1t[j][i] = m1[i][j]; - } - } - for (j=0; j<3; j++) - { - for (i=0; i<3; i++) - { - printf("%d", m1[i][j]); - } - printf("\n"); - } - return 0; -} -``` -![33.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/33.JPG) -### 34) Program to print the address of variable using pointer. -```C -#include - -int main() { - int a; - int *pt; - - printf("Pointer Example Program : Print Pointer Address\n"); - a = 10; - pt = &a; - - printf("\n[a ]:Value of A = %d", a); - printf("\n[*pt]:Value of A = %d", *pt); - printf("\n[&a ]:Address of A = %p", &a); - printf("\n[pt ]:Address of A = %p", pt); - printf("\n[&pt]:Address of pt = %p", &pt); - printf("\n[pt ]:Value of pt = %p", pt); - - return 0; -} -``` -![34.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/34.JPG) -### 35) Program to access array using pointer. -```C -int main() -{ - int data[5], i; - printf("Enter elements: "); - for(i = 0; i < 5; ++i) - scanf("%d", data + i); - printf("You entered: \n"); - for(i = 0; i < 5; ++i) - printf("%d\n", *(data + i)); - return 0; -} -``` -![35.png](https://github.com/LastComrade/PPS_Programs/blob/master/PPS%20Programs%20MD%20File/35.JPG) diff --git a/PPSPrac.md b/PPSPrac.md new file mode 100644 index 0000000..c2cfede --- /dev/null +++ b/PPSPrac.md @@ -0,0 +1,1385 @@ +![](https://i.imgur.com/eBZLVAa.png) +--- +## **

ESC-18104/18105 Programming for Problem Solving

** +--- +### **

Name - _Rishab Mehndiratta_

** +### **

CRN - _1915346_

** +### **

Branch - _CSE C2_

** +### **

Submitted to - _Ms. Goldendeep Kaur_

** +--- + + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("~~~~~~~~~~~~~~"); +puts("Rishab"); +puts("~~~~~~~~~~~~~~"); + +return 0; +} +``` +#### Result: +![result](https://i.imgur.com/ZIBEU5M.png) +--- +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\t -Guru Nanak Dev Engineering College,"); +printf("\n\t\t\t -Gill Road,"); +printf("\n\t\t\t -Ludhiana , Punjab\n"); + +return 0; +} +``` +#### Result: +![result address](https://i.imgur.com/6d1VQuI.png) + +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d \n",a); + +return 0; + +} +``` +#### Result: +![addInt.c](https://i.imgur.com/70km7pn.png) + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend: "); +scanf("%d",&a); + +printf("\nEnter the divisor: "); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d \n",q); + +return 0; +} +``` +##### Result : +![quotient.c](https://i.imgur.com/xGSua2X.png) + +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("Enter the value of A: "); +scanf("%d",&a); + +printf("Enter the value of B: "); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d"\n,b); + +return 0; +} +``` +#### Result: +![swappingWithout.c](https://i.imgur.com/u26diBO.png) +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even"); + + else + printf("\nNumber is Odd\n"); + + printf("\n"); + return 0; +} +``` +#### Result: +![EvenOdd.c](https://i.imgur.com/S5KIreQ.png) +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a,&b); + +if(a>b) +printf("\nA is largest"); +else + printf("\nB is largest\n"); + +return 0; +} +``` +#### Result: +![greatest.c](https://i.imgur.com/vJJ0a4I.png) +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +#### Result: +![greatest3.c](https://i.imgur.com/6kCn1ed.png) +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA\n"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB\n"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC\n"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD\n"); + } + else if(perc>=50 && perc<60) + { + printf("\nE\n"); + } + else + { + printf("\nScope of Improvement\n"); + } + return 0; +} +``` +#### Result: +![Percentage.c](https://i.imgur.com/J4Hvbq1.png) +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year: "); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year\n"); + +else + printf("\nNot a Leap year\n"); + } + + return 0; +} +``` +#### Result: +![leapYear.c](https://i.imgur.com/d5hdSqL.png) +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { + int i,res; + +for(i=0;i<=10;i++) { +res = 5*i; + printf("5 X %d = %d\n",i,res); + } + + return 0; +} +``` +#### Result: +![multiply.c](https://i.imgur.com/Ggjc5kb.png) +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f\n", num1, Operator, num2, result); + +return 0; +} +``` +#### Result: +![](https://i.imgur.com/MWA0qG0.png) +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d\n",rev); + + printf("\n\n"); + return 0; +} +``` +#### Result: +![](https://i.imgur.com/RTj5v0K.png) +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nNumber is a Pallindrome\n"); + + else + printf("\nNumber is not a Pallindrome\n"); + + printf("\n\n"); + return 0; +} +``` +#### Result: +![](https://i.imgur.com/kJGiObD.png) +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +#### Result: +![](https://i.imgur.com/SBPNF6O.png) +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` + #### Result: + ![](https://i.imgur.com/56jiKH1.png) +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.\n",number); + else + printf("%d is not an Armstrong number\n.",number); + +return 0; +} + +``` +#### Result: +![](https://i.imgur.com/lghRXZe.png) +### 19) Print Different Patterns. +#### i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +#### Result: +![](https://i.imgur.com/BvdTmjg.png) +#### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +####Result: +![](https://i.imgur.com/fI3noch.png) +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +#### Result: +![](https://i.imgur.com/MCiLLyl.png) +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +#### Result: +![](https://i.imgur.com/C84Fwdc.png) +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include +int main(){ + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter number of columns (between 1 and 100): "); + scanf("%d", &c); + printf("\nEnter elements of 1st matrix:\n"); + for(i=0; i +int main() +{ + int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; + printf("Enter rows and column for first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for second matrix: "); + scanf("%d %d",&r2, &c2); + + while (c1 != r2) + { + printf("Error! column of first matrix not equal to row of second.\n\n"); + printf("Enter rows and column for first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for second matrix: "); + scanf("%d %d",&r2, &c2); + } + + printf("\nEnter elements of matrix 1:\n"); + for(i=0; i +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +#### Result: +![](https://i.imgur.com/2KNH7TD.png) +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +#### Result: +![]() +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` +#### Result: +![]() + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld\n", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` +#### Result: +![](https://i.imgur.com/EKlxzF2.png) + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +#### Result: +![](https://i.imgur.com/PqmuBQI.png) +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish: "); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\n",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +#### Result: +![](https://i.imgur.com/jr4y7zQ.png) +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +#### Result: +![](https://i.imgur.com/1PIBSLt.png) +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } +``` +#### Result: +![](https://i.imgur.com/6kQ0e42.png) +### 30) Program to implement linear serach and binary. +```C +/* Linear Search */ +#include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} + + +``` +#### Result: +![](https://i.imgur.com/ZuG2NKm.png) + +```c +/* Binary Search */ + +#include + +int main() +{ + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +} +``` +#### Result: +![](https://i.imgur.com/kzAQpnP.png) +### 31) Program to implement bubble sort. +```C +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} + +``` +#### Result: +![](https://i.imgur.com/5EpIq52.png) +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[50]; + int roll; + float marks; +} s[10]; +int main() +{ + int i; + printf("Enter information of students:\n"); + for(i=0; i<3; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying Information:\n\n"); + for(i=0; i<3; ++i) + { + printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } + return 0; +} + +``` +#### Result: +![](https://i.imgur.com/IKvGSkG.png) +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +#### Result: +![](https://i.imgur.com/EChuja6.png) + +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +#### Result: +![](https://i.imgur.com/JqPFaLZ.png) +# **

****** ThankYou ******

** diff --git a/PPSasg.md b/PPSasg.md new file mode 100644 index 0000000..3f2791b --- /dev/null +++ b/PPSasg.md @@ -0,0 +1,1224 @@ +![](https://i.imgur.com/8bi6Eay.jpg) +--- +## **Programming for Problem Solving** +### **Name** ~ Payal +### **Class roll no. ~ 1915338** +### **Branch ~ CSE-C2** +### **Submitted to ~ Ms. Goldendeep Kaur** +--- +### 1) To display name: + + + + +```C +#include +int main() { + + +puts("~~~~~~~~~~~~~~~"); +puts(" Payal Kash"); +puts("~~~~~~~~~~~~~~~"); + +return 0; +} + +``` +Output: +![](https://i.imgur.com/jqoMMI7.png) +### 2) To display College address: +```C +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +Output: +![](https://i.imgur.com/qZOwThe.png) +### 3) Adding two integers: +```C +#include +int main() { + +int x,y,sum; + +printf("Enter two numbers: \n"); +scanf("%d %d",&x,&y); +sum=x+y; +printf("\n Sum of the two numbers: %d ",sum); + +return 0; + +} +``` +Output: +![](https://i.imgur.com/ZXegOv3.png) +### 4) To calculate quotient and remainder: +```C +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +Output: +![](https://i.imgur.com/4EPWYqq.png) +### 5) Swapping two numbers without using third variable: +```C +#include +int main() { + +int a,b; + +printf("\nEnter the value of A: "); +scanf("%d",&a); + +printf("\nEnter the value of B: "); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; +printf("After swapping: \n"); +printf("A: %d and B: %d\n",a,b); + +return 0; +} +``` +Output: +![](https://i.imgur.com/wAlQcwj.png) +### 6) To check whether a number is even or odd: +```C + +#include +void main(){ +int x; +printf("Enter a number:"); +scanf("%d",&x); +if(x%2==0) +printf("The number is even.\n"); +else +printf("The number is odd.\n"); + +} +``` +Output: +![](https://i.imgur.com/FcrxlVI.png) +### 7) Finding greater of two numbers: +```C + +#include + +int main() { + int a,b; + printf("Enter any two numbers:\n"); + scanf("%d%d", &a, &b); + +if(a>b) +printf("%d is greater.\n",a); +else +printf("%d is greater.\n",b); + +return 0; +} +``` +Output: +![](https://i.imgur.com/R3QrMwn.png) +### 8) Finding greatest of three numbers: +```C +#include +void main(){ + +int a,b,c; +printf("Enter three numbers:\n"); +scanf("%d %d %d",&a,&b,&c); +if(a>b && a>c) +{ +printf("%d is the greatest number\n",a); +} +else if(b>a && b>c) +{ +printf("%d is the greatest number\n",b); +} +else{ +printf("%d is the greatest number\n",c);} +} + + +``` +Output: +![](https://i.imgur.com/HQfKrm2.png) +### 9) Calculating grade: +```C +#include +void main() +{ +int marks; +printf("Enter marks:"); +scanf("%d",&marks); + +if(marks<=100 && marks>90){ +printf("Your grade is: A1\n"); + +} +else if(marks<=90 && marks>80) +{ +printf("Your grade is A2\n"); +} +else if(marks<=80 && marks>70) +{ +printf("Your grade is B1\n"); +} +else if(marks<=70 && marks>60) +{ +printf("Your grade is B2\n"); +} +else +{ +printf("FAIL\n"); +} +} + +``` +Output: +![](https://i.imgur.com/SgRdnip.png) +### 10) Finding the roots of a quadratic equation: +```C +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +Output: +![](https://i.imgur.com/EN9cQQG.png) +### 11) To check whether a year is leap or not: +```C +#include +int main() { + + int year; + + printf("Enter the year:"); + scanf("%d",&year); + if(year%4==0) + printf("It is a leap year.\n"); + else + printf("Not a leap year.\n"); + + return 0; +} +``` +Output: +![](https://i.imgur.com/LpI3yd5.png) +### 12) To print the table of any number: +```C + +#include +int main(){ +int x,i=0; +printf("Enter a number: "); +scanf("%d",&x); +while(i<=10) +{ +printf("%d X %d = %d\n",x,i,x*i); +i += 1; +} +return 0; +} + +``` +Output: +![](https://i.imgur.com/qkt3Hkh.png) +### 13) A simple calculator: +```C +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +Output: +![](https://i.imgur.com/JnqN6ek.png) +### 14) Reverse of a number: +```C + +#include +void main() +{ + +int x; +printf("Enter a number:"); +scanf("%d",&x); +int reverse,rem; +int y=x; +while(y!=0) +{ +rem=y%10; +reverse=reverse*10+rem; +y=y/10; + +} +printf("Reversed number is %d \n",reverse); + } + +``` +Output: +![](https://i.imgur.com/sIGu1Ol.png) +### 15) To check whether a number is palindrome or not: +```C +#include +void main() +{ + +int x; +printf("Enter a number:"); +scanf("%d",&x); +int reverse,rem; +int y=x; +while(y!=0) +{ +rem=y%10; +reverse=reverse*10+rem; +y=y/10; + +} +printf("Reversed number is %d \n",reverse); + +if(x==reverse) +printf("The number is a palindrome\n"); +else +printf("It is not a palindrome\n");o + +``` +Output: +![](https://i.imgur.com/eij72SW.png) + + + +### 16) To check whether a number is prime or not: +```C +include +void main(){ +int n,counter=0; +printf("Enter a number: "); +scanf("%d",&n); +int i=n; +while(i!=0) +{ +if(n%i==0){ +counter=counter+1; +} +i=i-1; +} +if(counter>2) +printf("The number is not prime.\n"); +else +printf("The number is prime.\n"); + +} + +``` +Output: +![](https://i.imgur.com/6WGbQ2o.png) +### 17) To print a series of prime numbers: +```C +#include +int main(){ +int a,b,i,end; +printf("Enter a number: "); +scanf("%d",&end); +printf("The series of prime numbers till %d is: \n",end); +for(a=1;a<=end;a++) +{ +i=0; +for(b=1;b<=a;b++) +{ +if(a%b==0) +i++; + +} +if(i==2) +printf("\n%d \t",a); +} +return 0; + +} + + ``` + Output: + ![](https://i.imgur.com/DyJIOIm.png) +### 18) To check whether a number is armstrong or not: +```C +#include +void main(){ +int a,b,rem,x=0; + +printf("Enter a number: "); +scanf("%d",&a); +b=a; +while(a!=0) +{ +rem=a%10; +x= x+ rem*rem*rem; +a=a/10; +} + +if(x==b) +printf("The number is armstrong\n"); +else +printf("The number is not armstrong\n"); +} + +``` +Output: +![](https://i.imgur.com/gvT3ODL.png) +### 19) To print Different Patterns: +### i) Pattern 1: +```C +#include +void main(){ +for(int i=1;i<=5;i++) +{ +for(int j=1;j<=i;j++) +{ +printf("%d ",j); +} +printf("\n"); +} +} + +``` +Output: +![](https://i.imgur.com/SzolNdw.png) +### ii) Pattern 2: +```C + +#include +int main() +{ +int i, space, k=0; + for(i=1; i<=7; ++i, k=0) + { + for(space=1; space<=7-i; ++space) + { + printf(" "); + } + while(k != 2*i-1) + { + printf("* "); + ++k; + } + printf("\n"); + } + return 0; +} + + +``` +Output: +![](https://i.imgur.com/xAzgfBg.png) +### 20) To find the largest number from a one dimensional array: +```C +include +void main(){ +int a[10]; +int i,n; +printf("Enter the number of elements to be entered in the array:"); +scanf("%d",&n); +printf("Enter the elements of array:"); +for(i=0;imax) +{ +max=a[i]; +} +} +printf("The largest number is %d\n",max); +} + +``` +Output: +![](https://i.imgur.com/TO9hTBY.png) +### 21) To find the sum of array elements: +```C +#include +void main() +{ +int a[10],n; +printf("ENter number of elements of array: "); +scanf("%d",&n); +printf("Enter the elements of array: "); +int i,sum; +for(i=0;i +int main(){ + +int a[3][3],b[3][3],c[3][3],i,j; +printf("Enter the first matrix:\n "); +for(i=0;i<3;i++) +{ + for(j=0;j<3;j++) + { + scanf("%d",&a[i][j]); + } +printf("\n"); +} + +printf("Enter the second matrix: \n"); +for(i=0;i<3;i++) +{ + for(j=0;j<3;j++) +{ +scanf("%d",&b[i][j]); +} +printf("\n"); +} + +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +c[i][j]=a[i][j]+b[i][j]; +} +} +printf("The addition of two matrices is:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +printf("\t %d",c[i][j]); +} +printf("\n"); +} +return 0; + + +``` +Output: +![](https://i.imgur.com/lw5xjQb.png) +### 23) Multiplication of matrices: +```C +#include +void main() +{ +int a[3][3],b[3][3],c[3][3],i,j,k; +int sum=0; +printf("Enter the first matrix:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +scanf("%d",&a[i][j]); +} +} +printf("Enter the sceond matrix:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +scanf("%d",&b[i][j]); +} +} +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +sum=0; +for(k=0;k<3;k++) +{ +sum=sum+a[i][j]*b[i][j]; +c[i][j]=sum; +} +} + +} +printf("The multiplication of two matrices is:\n"); +for(i=0;i<3;i++) +{ +for(j=0;j<3;j++) +{ +printf("\t%d",c[i][j]); +} +printf("\n"); +} +} + +``` +Output: +![](https://i.imgur.com/tPEZuNc.png) +### 24) To check whether a string is palindrome or not: +```C +#include +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + scanf("%s",s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +Output: +![](https://i.imgur.com/CTRfOaW.png) +### 26) Swapping two numbers: +### i) Call by reference: +```C + + +#include +void swap(int *a, int *b); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +Output: +![](https://i.imgur.com/oNNANHD.png) +### ii) Call by value: +```C +include + +void swap(int x,int y); + +void main() +{ +int n1,n2; +printf("Enter n1: "); +scanf("%d",&n1); +printf("Enter n2 :"); +scanf("%d",&n2); +void swap(int n1,int n2); +printf("after swapping: n1= %d and n2= %d\n",n2,n1); +} + + +void swap(int x,int y){ + +printf("Enter 1st number x: "); +scanf("%d",&x); +printf("Enter 2nd number y: "); +scanf("%d",&y); +int temp; +temp=x; +x=y; +y=temp; +printf("After swapping: x = %d and y= %d",y,x); +} + +``` +Output: +![](https://i.imgur.com/sR9Kwit.png) + +### 27) Factorial of a number: +### i) With recursion: +```C +#include + +int factorial(int m); + +int main(){ + +int n,result; +printf("Enter a number: "); +scanf("%d",&n); +result = factorial(n); +printf("The factorial of the number is: %d\n",result); +} + +int factorial(int m) +{ +if(m<0) +printf("Factorial of a negative number is not possible."); + +else if(m==0 || m==1) +return 1; + +else +return (m*factorial(m-1)); +} +``` +Output: +![](https://i.imgur.com/YfcvJ87.png) +### ii) Without recursion: +```C + + +#include + +void main() +{ +int a,fact=1; +printf("Enter a number: "); +scanf("%d",&a); +int i=a; +while(i>0) +{ +fact = fact*i; +i= i-1; + +} +printf("The factorial of the entered number is %d \n",fact); +} + + +``` +Output: +![](https://i.imgur.com/YfcvJ87.png) +### 28) Finonacci series: +### i) With recursion: +```C + +#include + +int fibo(int n); +int main(){ +int n,i=0,c; +printf("Enter a number: "); + +scanf("%d",&n); +printf("Fibonacci series:\n"); +for(c=1;c<=n;c++) + { + printf("%d\n",fibo(i)); + i=i+1; + } +return 0; + + +} +int fibo(int n){ +if(n==0) +return 0; +else if(n==1) +return 1; +else +return (fibo(n-1)+fibo(n-2)); + +} + +``` +Output: +![](https://i.imgur.com/6yZ5mQk.png) +### ii) Without recursion: +```C + +#include +int main() +{ + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("%d \n%d\n",n1,n2); + for(i=2;i +int average(); +void main(){ +int avg; +avg=average(); +printf("The average of five numbers is: %d\n",avg); +} +int average() +{ +int a,b,c,d,e,result; +printf("Enter the five numbers: "); +scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); +result=(a+b+c+d+e)/5; +return result; +} + + + ``` + Output: + ![](https://i.imgur.com/W9XnidG.png) +### 30) Linear and binary search: +### i) Linear search: + ```C + #include + +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} + + +``` +Output: +![](https://i.imgur.com/gU0Vu22.png) + +### ii) Binary search: +```C +#include + +int main() +{ + int c, first, last, middle, n, search, array[100]; + + printf("Enter number of elements\n"); + scanf("%d",&n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d",&array[c]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +} + +``` +Output: +![](https://i.imgur.com/h4dPD7n.png) +### 31) Bubble Sort: +```C + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +Output: +![](https://i.imgur.com/g4h29XA.png) +### 32) Program to store information of 10 students using array of structures. +```C +#include +struct student +{ + char name[50]; + int roll; + float marks; +} s[10]; +int main() +{ + int i; + printf("Enter information of students:\n"); + for(i=0; i<3; ++i) + { + s[i].roll = i+1; + printf("\nFor roll number%d,\n",s[i].roll); + printf("Enter name: "); + scanf("%s",s[i].name); + printf("Enter marks: "); + scanf("%f",&s[i].marks); + printf("\n"); + } + printf("Displaying Information:\n\n"); + for(i=0; i<3; ++i) + { + printf("\nRoll number: %d\n",i+1); + printf("Name: "); + puts(s[i].name); + printf("Marks: %.1f",s[i].marks); + printf("\n"); + } + return 0; +} + +``` +Output: +![](https://i.imgur.com/Vr78u23.png) +### 33) Transpose of a matrix: +```C + +#include +int main(){ +int a[3][3]; +printf("Enter a 3X3 matrix: "); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +scanf("%d",&a[i][j]); +} +} +printf("The matrix you entered is:\n"); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +printf("%d\t",a[i][j]); +} +printf("\n"); +} +printf("The transpose of the matrix is:\n "); +for(int i=0;i<3;i++) +{ +for(int j=0;j<3;j++) +{ +printf("%d\t",a[j][i]); +} +printf("\n"); +} +} +``` +Output: +![](https://i.imgur.com/DLUsA0m.png) +### 34) To display address of a variable using pointers: +```C +#include +int main() { + int a; + int *p; +printf("Enter a value: "); +scanf("%d",&a); + p = &a; + + printf("Address of the value is: %d\n", p); + + + return 0; +} + +``` +Output: +![](https://i.imgur.com/GfXzJmg.png) +### 35) Accessing an array using pointer: +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} +``` +Output: +![](https://i.imgur.com/ycENt8S.png) + diff --git a/README.md b/README.md deleted file mode 100644 index ca04a7d..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# PPS -Programming for Problem Solving - -Follow instructions give in README.md file, found in respective folder. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ + diff --git a/Untitled.md b/Untitled.md new file mode 100644 index 0000000..8f000c0 --- /dev/null +++ b/Untitled.md @@ -0,0 +1,488 @@ +## Functions +A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. +You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. +A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. +A function can also be referred as a method or a sub-routine or a procedure, etc. + + +## Why do need functions? +- Functions help us in reducing code redundancy. If functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call it everywhere. This also helps in maintenance as we have to change at one place if we make future changes to the functionality. +- Functions make code modular. Consider a big file having many lines of codes. It becomes really simple to read and use the code if the code is divided into functions. +- Functions provide abstraction. For example, we can use library functions without worrying about their internal working. + +### C functions can be classified into two categories +1. Library functions +2. User-Defined functions + +![image alt](https://3.bp.blogspot.com/-bCszLcVsvSo/XHKVNdlf5WI/AAAAAAAAAZ0/DTRX_N5lKQ8qMBEQvCppnKKaiFRHkUrgwCLcBGAs/s1600/c%2Bfun.png) + + + +Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries. +A User-defined functions on the other hand, are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space. +### Function Declaration +General syntax for function declaration is, +returntype functionName(type1 parameter1, type2 parameter2,...); +Like any variable or an array, a function must also be declared before its used. Function declaration informs the compiler about the function name, parameters is accept, and its return type. The actual body of the function can be defined separately. It's also called as Function Prototyping. +Function declaration consists of 4 parts. + +- returntype +- function name +- parameter list +- terminating semicolon + + +returntype +When a function is declared to perform some sort of calculation or any operation and is expected to provide with some result at the end, in such cases, a return statement is added at the end of function body. Return type specifies the type of value(int, float, char, double) that function is expected to return to the program which called the function. + +### Note: In case your function doesn't return any value, the return type would be void. + +functionName +Function name is an identifier and it specifies the name of the function. The function name is any valid C identifier and therefore must follow the same naming rules like other variables in C language. + + +parameter list +The parameter list declares the type and number of arguments that the function expects when it is called. Also, the parameters in the parameter list receives the argument values when the function is called. They are often referred as formal parameters. + +Let's write a simple program with a main() function, and a user defined function to multiply two numbers, which will be called from the main() function. + + #include + +int multiply(int a, int b); // function declaration + +int main() +{ + int i, j, result; + printf("Please enter 2 numbers you want to multiply..."); + scanf("%d%d", &i, &j); + + result = multiply(i, j); // function call + printf("The result of muliplication is: %d", result); + + return 0; +} + +int multiply(int a, int b) +{ + return (a*b); // function defintion, this can be done in one line +} + +### Function definition Syntax +Just like in the example above, the general syntax of function definition is, + +returntype functionName(type1 parameter1, type2 parameter2,...) +{ + // function body goes here +} +### Calling a function +When a function is called, control of the program gets transferred to the function. +functionName(argument1, argument2,...); + +## Passing Arguments to a function +Arguments are the values specified during the function call, for which the formal parameters are declared while defining the function. + + +![image alt](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRwHJrfJ8x11EpmcOiBt4aOCUgtahhwgijA1QIdDdtj7NrpI3HR) +It is possible to have a function with parameters but no return type. It is not necessary, that if a function accepts parameter(s), it must return a result too. +![image alt](https://www.studytonight.com/c/images/passing-argument-c-functions.jpg) + +While declaring the function, we have declared two parameters a and b of type int. Therefore, while calling that function, we need to pass two arguments, else we will get compilation error. And the two arguments passed should be received in the function definition, which means that the function header in the function definition should have the two parameters to hold the argument values. These received arguments are also known as formal parameters. The name of the variables while declaring, calling and +defining a function can be different. + +## Returning a value from function +A function may or may not return a result. But if it does, we must use the return statement to output the result. return statement also ends the function execution, hence it must be the last statement of any function. If you write any statement after the return statement, it won't be executed. +![image alt](https://www.studytonight.com/c/images/return-statement-c-functions.jpg) + +The datatype of the value returned using the return statement should be same as the return type mentioned at function declaration and definition. If any of it mismatches, you will get compilation error. +## Type of User-defined Functions in C +There can be 4 different types of user-defined functions, they are: + +Function with no arguments and no return value +Function with no arguments and a return value +Function with arguments and no return value +Function with arguments and a return value +Below, we will discuss about all these types, along with program examples. + +## Function with no arguments and no return value +Such functions can either be used to display information or they are completely dependent on user inputs. + +Below is an example of a function, which takes 2 numbers as input from user, and display which is the greater number. + +#include + +void greatNum(); // function declaration + +int main() +{ + greatNum(); // function call + return 0; +} + +void greatNum() // function definition +{ + int i, j; + printf("Enter 2 numbers that you want to compare..."); + scanf("%d%d", &i, &j); + if(i > j) { + printf("The greater number is: %d", i); + } + else { + printf("The greater number is: %d", j); + } +} +## Function with no arguments and a return value +We have modified the above example to make the function greatNum() return the number which is greater amongst the 2 input numbers. + +#include + +int greatNum(); // function declaration + +int main() +{ + int result; + result = greatNum(); // function call + printf("The greater number is: %d", result); + return 0; +} + +int greatNum() // function definition +{ + int i, j, greaterNum; + printf("Enter 2 numbers that you want to compare..."); + scanf("%d%d", &i, &j); + if(i > j) { + greaterNum = i; + } + else { + greaterNum = j; + } + // returning the result + return greaterNum; +} +## Function with arguments and no return value +We are using the same function as example again and again, to demonstrate that to solve a problem there can be many different ways. + +This time, we have modified the above example to make the function greatNum() take two int values as arguments, but it will not be returning anything. + +#include + +void greatNum(int a, int b); // function declaration + +int main() +{ + int i, j; + printf("Enter 2 numbers that you want to compare..."); + scanf("%d%d", &i, &j); + greatNum(i, j); // function call + return 0; +} + +void greatNum(int x, int y) // function definition +{ + if(x > y) { + printf("The greater number is: %d", x); + } + else { + printf("The greater number is: %d", y); + } +} + + +## Function with arguments and a return value +This is the best type, as this makes the function completely independent of inputs and outputs, and only the logic is defined inside the function body. + +#include + +int greatNum(int a, int b); // function declaration + +int main() +{ + int i, j, result; + printf("Enter 2 numbers that you want to compare..."); + scanf("%d%d", &i, &j); + result = greatNum(i, j); // function call + printf("The greater number is: %d", result); + return 0; +} + +int greatNum(int x, int y) // function definition +{ + if(x > y) { + return x; + } + else { + return y; + } +} +## Nesting of Functions +C language also allows nesting of functions i.e to use/call one function inside another function's body. We must be careful while using nested functions, because it may lead to infinite nesting. + + +function1() +{ + // function1 body here + + function2(); + + // function1 body here +} +If function2() also has a call for function1() inside it, then in that case, it will lead to an infinite nesting. They will keep calling each other and the program will never terminate. + +Not able to understand? Lets consider that inside the main() function, function1() is called and its execution starts, then inside function1(), we have a call for function2(), so the control of program will go to the function2(). But as function2() also has a call to function1() in its body, it will call function1(), which will again call function2(), and this will go on for infinite times, until you forcefully exit from program execution. + +## What is Recursion? +Recursion is a special way of nesting functions, where a function calls itself inside it. We must have certain conditions in the function to break out of the recursion, otherwise recursion will occur infinite times. + +function1() +{ + // function1 body + function1(); + // function1 body +} + +Example: Factorial of a number using Recursion +#include + +int factorial(int x); //declaring the function + +void main() +{ + int a, b; + + printf("Enter a number..."); + scanf("%d", &a); + b = factorial(a); //calling the function named factorial + printf("%d", b); +} + +int factorial(int x) //defining the function +{ + int r = 1; + if(x == 1) + return 1; + else + r = x*factorial(x-1); //recursion, since the function calls itself + + return r; +} +Similarly, there are many more applications of recursion in C language. Go to the programs section, to find out more programs using recursion. +## Types of Function calls in C +Functions are called by their names, we all know that, then what is this tutorial for? Well if the function does not have any arguments, then to call a function you can directly use its name. But for functions with arguments, we can call a function in two different ways, based on how we specify the arguments, and these two ways are: + +Call by Value +Call by Reference + +## Call by Value +Calling a function by value means, we pass the values of the arguments which are stored or copied into the formal parameters of the function. Hence, the original values are unchanged only the parameters inside the function changes. + +#include + +void calc(int x); + +int main() +{ + int x = 10; + calc(x); + // this will print the value of 'x' + printf("\nvalue of x in main is %d", x); + return 0; +} + +void calc(int x) +{ + // changing the value of 'x' + x = x + 10 ; + printf("value of x in calc function is %d ", x); +} + +value of x in calc function is 20 +value of x in main is 10 + +In this case, the actual variable x is not changed. This is because we are passing the argument by value, hence a copy of x is passed to the function, which is updated during function execution, and that copied value in the function is destroyed when the function ends(goes out of scope). So the variable x inside the main() function is never changed and hence, still holds a value of 10. + + + +But we can change this program to let the function modify the original x variable, by making the function calc() return a value, and storing that value in x. + +#include + +int calc(int x); + +int main() +{ + int x = 10; + x = calc(x); + printf("value of x is %d", x); + return 0; +} + +int calc(int x) +{ + x = x + 10 ; + return x; +} + +value of x is 20 + +## Call by Reference +In call by reference we pass the address(reference) of a variable as argument to any function. When we pass the address of any variable as argument, then the function will have access to our variable, as it now knows where it is stored and hence can easily update its value. + +In this case the formal parameter can be taken as a reference or a pointer(don't worry about pointers, we will soon learn about them), in both the cases they will change the values of the original variable. + +#include + +void calc(int *p); // functin taking pointer as argument + +int main() +{ + int x = 10; + calc(&x); // passing address of 'x' as argument + printf("value of x is %d", x); + return(0); +} + +void calc(int *p) //receiving the address in a reference pointer variable +{ + /* + changing the value directly that is + stored at the address passed + */ + *p = *p + 10; +} + +value of x is 20 +## How to pass Array to a Function in C +Whenever we need to pass a list of elements as argument to any function in C language, it is prefered to do so using an array. But how can we pass an array as argument to a function? Let's see how its done. + +## Declaring Function with array as a parameter +There are two possible ways to do so, one by using call by value and other by using call by reference. + +We can either have an array as a parameter. +int sum (int arr[]); +Or, we can have a pointer in the parameter list, to hold the base address of our array. +int sum (int* ptr); +We will study the second way in details later when we will study pointers. + +## Returning an Array from a function +We don't return an array from functions, rather we return a pointer holding the base address of the array to be returned. But we must, make sure that the array exists after the function ends i.e. the array is not local to the function. + +int* sum (int x[]) +{ + // statements + return x ; +} +We will discuss about this when we will study pointers with arrays. + +## Passing arrays as parameter to function +Now let's see a few examples where we will pass a single array element as argument to a function, a one dimensional array to a function and a multidimensional array to a function. + + + +## Passing a single array element to a function +Let's write a very simple program, where we will declare and define an array of integers in our main() function and pass one of the array element to a function, which will just print the value of the element. + +#include + +void giveMeArray(int a); + +int main() +{ + int myArray[] = { 2, 3, 4 }; + giveMeArray(myArray[2]); //Passing array element myArray[2] only. + return 0; +} + +void giveMeArray(int a) +{ + printf("%d", a); +} + +4 + +## Passing a complete One-dimensional array to a function +To understand how this is done, let's write a function to find out average of all the elements of the array and print it. + +We will only send in the name of the array as argument, which is nothing but the address of the starting element of the array, or we can say the starting memory address. + +#include + +float findAverage(int marks[]); + +int main() +{ + float avg; + int marks[] = {99, 90, 96, 93, 95}; + avg = findAverage(marks); // name of the array is passed as argument. + printf("Average marks = %.1f", avg); + return 0; +} + +float findAverage(int marks[]) +{ + int i, sum = 0; + float avg; + for (i = 0; i <= 4; i++) { + sum += marks[i]; + } + avg = (sum / 5); + return avg; +} + +94.6 + +## Passing a Multi-dimensional array to a function +Here again, we will only pass the name of the array as argument. + +#include + +void displayArray(int arr[3][3]); + +int main() +{ + int arr[3][3], i, j; + printf("Please enter 9 numbers for the array: \n"); + for (i = 0; i < 3; ++i) + { + for (j = 0; j < 3; ++j) + { + scanf("%d", &arr[i][j]); + } + } + // passing the array as argument + displayArray(arr); + return 0; +} + +void displayArray(int arr[3][3]) +{ + int i, j; + printf("The complete array is: \n"); + for (i = 0; i < 3; ++i) + { + // getting cursor to new line + printf("\n"); + for (j = 0; j < 3; ++j) + { + // \t is used to provide tab space + printf("%d\t", arr[i][j]); + } + } +} + +Please enter 9 numbers for the array: +1 +2 +3 +4 +5 +6 +7 +8 +9 +The complete array is: +1 2 3 +4 5 6 +7 8 9 + + + diff --git a/_sandstorm-init.html b/_sandstorm-init.html new file mode 100644 index 0000000..5abb654 --- /dev/null +++ b/_sandstorm-init.html @@ -0,0 +1,2132 @@ + + + + + + + + + +PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105) - CodiMD + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +


PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105)

NAME : GURKIRAT SINGH

ROLL NO : 1915314

BRANCH : COMPUTER SCIENCE

SECTION : C ‘1’


1. PROGRAM TO FIND A NUMBER IS EVEN OR ODD

#include<stdio.h>
+ 
+int main()
+{
+  int n;
+ 
+  printf("Enter an integer\n");
+  scanf("%d", &n);
+ 
+  if (n%2 == 0)
+    printf("Even\n");
+  else
+    printf("Odd\n");
+ 
+  return 0;
+}
+

#2.PROGRAM OF SWAP TWO NUMBERS WITHOUT USING THIRD VAULE

#include<stdio.h>  
+ int main()    
+{    
+int a=10, b=20;      
+printf("Before swap a=%d b=%d",a,b);      
+a=a+b;//a=30 (10+20)    
+b=a-b;//b=10 (30-20)    
+a=a-b;//a= (30-10)    
+printf("\nAfter swap a=%d b=%d",a,b);    
+return 0;  
+}  
+

## 3.To print our name using puts
+#include <stdio.h>
+
+int main(){
+	
+	char name[]="Gurkirat Singh";
+
+	printf("My name is: ");
+	puts(name);
+	
+	return 0;
+}
+

4) Program to find quotient and remainder.

    /* To find quotient and remainder */
+
+#include <stdio.h>
+
+int main() {
+    
+int a,b,r,q;
+
+printf("\nEnter the Dividend:");
+scanf("%d",&a);
+
+printf("\nEnter the divisor:");
+scanf("%d",&b);
+
+r=a%b;
+q=a/b;
+
+printf("\nRemainder: %d",r);
+printf("\nQuotient: %d",q);
+
+return 0;
+}
+

5) Program to swap two variables without 3rd variable.

    /* Swapping without 3rd variable  */
+
+#include <stdio.h>
+int main() {
+    
+int a,b;
+
+printf("\nEnter the value of A:");
+scanf("%d",&a);
+
+printf("\nEnter the value of B:");
+scanf("%d",&b);
+
+   a = a + b;
+   b = a - b;
+   a = a - b;
+   
+printf("\nA: %d",a);
+printf("\nB: %d",b);
+
+return 0;
+}
+

6) Program to check even odd number.

/* To find whether number is even or odd */
+
+#include<stdio.h>
+int main() { 
+
+  int num,temp;
+
+  printf("Enter the Number:");
+  scanf("%d",&num);
+
+  if(num%2==0)
+  printf("\nNumber is Even....");
+
+  else
+  printf("\nNumber is Odd....");
+
+  printf("\n\n");
+  return 0;
+}
+

7) Finding greteast of two numbers.

     /* Largest one in two */
+
+#include<stdio.h>
+
+int main() {
+    int a,b;
+    printf("Enter any two number(A and B): ");
+    scanf("%d%d", &a, &b);
+    
+if(a>b)
+printf("\nA is largest....");
+else
+        printf("\nB is largest.....");
+    
+return 0;
+}
+

8) Find greatest of three number .

/* Largest of three number */
+
+#include<stdio.h>
+int main() {
+int x, y, z, large;
+        
+ printf(" Enter any three integer numbers for x, y, z :  ") ;
+
+scanf("%d %d %d", &x, &y, &z) ;
+
+large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ;
+
+printf("\n\n Largest  or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator :  %d", large) ;
+       
+ return 0;
+}
+

9) Program to assign grade to student according to percentage.

/* To find grade of a student by marks */
+
+#include<stdio.h>
+int main() { 
+    
+  int s1,s2,s3,s4,s5,agg;
+  float perc;
+
+  printf("Enter the Marks in 5 Subjects Respectively:\n");
+
+scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
+
+agg=s1+s2+s3+s4+s5; // Aggregate Marks
+  
+  perc=agg/500.0*100;  // Perc Marks
+
+  if(perc>=90)
+  {
+      printf("\nA");
+      
+  }
+  
+  else if (perc>=80 && perc<90)
+  { 
+      printf("\nB");
+      
+  }
+  
+  else if(perc>=70 && perc<80)
+  {
+      printf("\nC");
+      
+  }
+  
+  else if(perc>=60 && perc<70)
+  { 
+      printf("\nD");
+  }
+  else if(perc>=50 && perc<60)
+  {
+      printf("\nE");
+  }
+  else 
+     {
+          printf("\nScope of Improvement....");
+    } 
+    return 0;
+}
+

10) Program to print roots of quadratic equation.

/*Program to print roots */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+ 
+int main() {
+    float a, b, c, root1, root2;
+    float realp, imagp, disc;
+ 
+printf("Enter the values of a, b and c \n");
+    scanf("%f %f %f", &a, &b, &c);
+    
+/* If a = 0, it is not a quadratic equation */
+
+if (a == 0 || b == 0 || c == 0)
+    {
+        printf("Error: Roots cannot be determined \n");
+        exit(1);
+    }
+    else
+    {
+        disc = b * b - 4.0 * a * c;
+        if (disc < 0)
+        {
+            printf("Imaginary Roots\n");
+            realp = -b / (2.0 * a) ;
+            imagp = sqrt(abs(disc)) / (2.0 * a);
+            printf("Root1 = %f  +i %f\n", realp, imagp);
+            printf("Root2 = %f  -i %f\n", realp, imagp);
+        }
+
+else if (disc == 0)
+        {
+            printf("Roots are real and equal\n");
+            root1 = -b / (2.0 * a);
+            root2 = root1;
+            printf("Root1 = %f\n", root1);
+            printf("Root2 = %f\n", root2);
+        }
+
+else if (disc > 0 )
+        {
+            printf("Roots are real and distinct \n");
+            root1 =(-b + sqrt(disc)) / (2.0 * a);
+            root2 =(-b - sqrt(disc)) / (2.0 * a);
+            printf("Root1 = %f  \n", root1);
+            printf("Root2 = %f  \n", root2);
+        }
+
+}
+   return 0;
+}
+

11) Program to check year is leap or not.

/* To find whether year is leap or not */
+
+#include<stdio.h>
+int main() { 
+
+  int year,temp;
+
+  printf("Enter teh year:");
+  scanf("%d",&year);
+
+  temp=year%4;
+
+  if(year%100==0)
+  {
+     if(year%400==0)
+     printf("\nLeap year...");
+  }
+
+  else
+  {
+    if(year%4==0)
+    printf("\nLeap year...");
+
+else
+    printf("\nNot a Leap year...");
+   }
+  
+  return 0;
+}
+

12) Program to print table of 5.

/* Table of 5 */
+
+#include<stdio.h>
+
+int main() { int res;
+
+for(int i=1;i<=10;i++) {
+
+res=5*i;
+    
+   printf("\n5*%d=%d",i,res);
+   }
+
+   return 0;
+}
+

13) To make simple calculator using switch case.

/* C Program to Create Simple Calculator using Switch Case */
+ 
+#include <stdio.h>
+ 
+int main() {
+	char Operator;
+	float num1, num2, result = 0;
+	
+printf("\n Please Enter an Operator (+, -, *, /)  :  ");
+scanf("%c", &Operator);
+  	
+printf("\n Please Enter the Values for two Operands: num1 and num2  :  ");
+scanf("%f%f", &num1, &num2);
+  	
+switch(Operator)
+  	{
+  		case '+':
+  			result = num1 + num2;
+  			break;
+  		case '-':
+  			result = num1 - num2;
+  			break;
+  		case '*':
+  			result = num1 * num2;
+  			break;
+  		case '/':
+  			result = num1 / num2;
+  			break;
+		default:
+			printf("\n You have enetered an Invalid Operator ");				    			
+	}
+  
+printf("\n The result of %.2f %c %.2f  = %.2f", num1, Operator, num2, result);
+	
+return 0;
+}
+

14) To calculate reverse of a number.

/* To find reverse of a Number*/
+
+#include<stdio.h>
+int main() { 
+
+  int num,rev=0;
+
+  printf("\nEnter the Number:");
+  scanf("%ld",&num);
+
+while(num!=0)
+{ 
+    rev = rev * 10;
+    rev = rev + num%10;
+    num = num/10;
+  }
+  
+
+  printf("\nReversed number:%d",rev);
+
+  printf("\n\n");
+  return 0;
+}
+

15) To check whether number is palindrome or not.

/* Palindrome */
+
+#include<stdio.h>
+int main() { 
+
+  int n,rev=0,check,rem;
+
+  printf("\nEnter the number:");
+  scanf("%d",&n);
+  check=n;
+  
+  while( n!=0 )
+    {
+        rem = n%10;
+        rev = rev*10 + rem;
+        n /= 10;
+    }
+  
+  if(rev==check)
+  printf("\nReversed number is equal to entered number....");
+  
+  else 
+  printf("\nReversed number is not equal  to entered number....");
+  
+  printf("\n\n");
+  return 0;
+}
+

16) To check whether a number is prime or not.

/* Program to check prime no. */
+
+#include <stdio.h>
+#include <stdlib.h>
+ 
+int main() {
+    
+   int num, j, flag;
+  
+  printf("Enter a number \n");
+   scanf("%d", &num);
+ 
+  if (num <= 1)
+    {
+        printf("%d is not a prime numbers \n", num);
+        exit(1);
+    }
+    flag = 0;
+    for (j = 2; j <= num / 2; j++)
+    {
+        if ((num % j) == 0)
+        {
+            flag = 1;
+            break;
+        }
+    }
+    if (flag == 0)
+        printf("%d is a prime number \n", num);
+     else
+        printf("%d is not a prime number \n", num);
+
+return 0;
+    
+}
+

17) Program to print prime number to 100.

/* Prime number from 1 to 100 */
+ 
+ #include<stdio.h>
+
+   int main(){
+
+int numbr,k,remark;
+
+printf(" The prime numbers between 1 and 100 : \n");
+
+   for(numbr=2;numbr<=100;++numbr)
+
+  {
+
+   remark=0;
+
+  for(k=2;k<=numbr/2;k++){
+
+  if((numbr % k) == 0){
+
+ remark++;
+
+  break;
+     }
+
+   }
+
+   if(remark==0)
+    printf("\n    %d ",numbr);
+
+ }
+
+  return 0;
+
+   } 
+

18) Program to check whether a number is armstrong or not.

/* To check armstrong number */
+
+#include <stdio.h>
+int main()
+{
+    int number, originalNumber, remainder, result = 0;
+
+printf("Enter a three digit integer: ");
+    scanf("%d", &number);
+
+originalNumber = number;
+
+while (originalNumber != 0)
+    {
+        remainder = originalNumber%10;
+        result += remainder*remainder*remainder;
+        originalNumber /= 10;
+
+}
+
+if(result == number)
+        printf("%d is an Armstrong number.",number);
+    else
+        printf("%d is not an Armstrong number.",number);
+
+return 0;
+}
+
+

19) Print Different Patterns.

i) Pattern 1.

#include <stdio.h>
+int main() {
+    
+   int i,j,r;
+    
+   printf("Enter number of rows: ");
+    scanf("%d",&r);
+    
+for(i=1; i<=r; ++i)
+    {
+        for(j=1; j<=i; ++j)
+        {
+            printf("%d ",j);
+        }
+        printf("\n");
+    }
+    return 0;
+}
+
+

ii) Pattern 2.

#include <stdio.h>
+int main() {
+    
+   int r,i,j,num= 1;
+   printf("Enter number of rows: ");
+    scanf("%d",&r);
+    for(i=1;i<=r;i++)
+    {
+        for(j=1;j<=i;++j)
+        {
+            printf("%d",num);
+            ++num;
+        }
+        printf("\n");
+    }
+    return 0;
+}
+

20) Program to find largest from 1 dimensional array.

/* Largest in 1 dimensional array */
+
+#include <stdio.h>
+int main() {
+    
+   int i, n;
+   float arr[100];
+    
+   printf("Enter total number of elements(1 to 100): ");
+    scanf("%d", &n);
+    printf("\n");
+    
+    
+// Stores number entered by the user
+    for(i = 0; i < n; ++i)
+    {
+       printf("Enter Number %d: ", i+1);
+       scanf("%f", &arr[i]);
+    }
+    // Loop to store largest number to arr[0]
+    for(i = 1; i < n; ++i)
+    {
+       // Change < to > if you want to find the smallest element
+       if(arr[0] < arr[i])
+           arr[0] = arr[i];
+    }
+    printf("Largest element = %.2f", arr[0]);
+    return 0;
+}
+

21) To find sumof the N natural numbers in an array.

/* Sum of N no.s in array */
+     
+#include<stdio.h>
+
+int main() {
+    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
+    int n, sum = 0, c, array[100];
+
+printf("Enter the number of integers you want to add: ");
+    scanf("%d", &n);
+
+ printf("\n\nEnter %d integers \n\n", n);
+
+for(c = 0; c < n; c++)
+   {
+        scanf("%d", &array[c]);
+        sum += array[c];    // same as sum = sum + array[c]
+    }
+
+  printf("\n\nSum = %d\n\n", sum);
+    return 0;
+}
+

22) Program to add two matrices .

/*  Addition of matrix */
+    
+#include <stdio.h>
+ 
+int main() {
+   
+   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
+ 
+   printf("Enter the number of rows and columns of matrix\n");
+   scanf("%d%d", &m, &n);
+   
+   printf("Enter the elements of first matrix\n");
+ 
+   for (c = 0; c < m; c++)
+      for (d = 0; d < n; d++)
+         scanf("%d", &first[c][d]);
+ 
+   printf("Enter the elements of second matrix\n");
+ 
+   for (c = 0; c < m; c++)
+      for (d = 0 ; d < n; d++)
+         scanf("%d", &second[c][d]);
+   
+   printf("Sum of entered matrices:-\n");
+   
+   for (c = 0; c < m; c++) {
+      for (d = 0 ; d < n; d++) {
+         sum[c][d] = first[c][d] + second[c][d];
+         printf("%d\t", sum[c][d]);
+      }
+      printf("\n");
+   }
+ 
+   return 0;
+}
+

23) Program to multiply two matrices .

/* Multiplation of matrices */
+
+#include <stdio.h>
+int main()
+     {
+      int m, n, p, q, c, d, k, sum = 0;
+      int first[10][10], second[10][10],   multiply[10][10];
+ 
+  printf("Enter number of rows and columns of first matrix\n");
+  scanf("%d%d", &m, &n);
+  
+  printf("Enter elements of first matrix\n");
+ 
+  for (c = 0; c < m; c++)
+    for (d = 0; d < n; d++)
+      scanf("%d", &first[c][d]);
+ 
+  printf("Enter number of rows and columns of second matrix\n");
+  scanf("%d%d", &p, &q);
+ 
+  if (n != p)
+    printf("The matrices can't be multiplied with each other.\n");
+  
+  else
+  {
+    printf("Enter elements of second matrix\n");
+ 
+for (c=0;c<p;c++)
+      for (d = 0; d < q; d++)
+        scanf("%d", &second[c][d]);
+ 
+  for (c = 0; c < m; c++) {
+      for (d = 0; d < q; d++) {
+        for (k = 0; k < p; k++) {
+          sum = sum + first[c][k]*second[k][d];
+        }
+ 
+   multiply[c][d] = sum;
+        sum = 0;
+      }
+    }
+ 
+   printf("Product of the matrices:\n");
+ 
+   for (c = 0; c < m; c++) {
+      for (d = 0; d < q; d++)
+        printf("%d\t", multiply[c][d]);
+ 
+   printf("\n");
+    }
+  }
+ 
+  return 0;
+}
+

24) Program to check whether a string is palindrome or not .

#include<stdio.h>
+#include <string.h>
+ 
+int main() {
+    char s[1000];
+    int i,n,c=0;
+ 
+   printf("Enter  the string : ");
+   gets(s);
+   n=strlen(s);
+ 
+for(i=0;i<n/2;i++)
+    {
+    	if(s[i]==s[n-i-1])
+    	c++;
+ 
+ 	}
+if(c==i)
+ 	    printf("string is palindrome");
+    else
+        printf("string is not palindrome");
+ 
+return 0;
+}
+

25) Program to perform basic operations like lenghth of string ,string concat, string copy ,string compare and string reverse.

/*Fundamental string operation, lenth, concatenation, compare and copy strings without string.h */
+
+#include <stdio.h> 
+#include <stdlib.h>
+  
+int find_length(char string[]) {
+    int len = 0, i;
+    for (i = 0; string[i] != '\0'; i++) {
+      len++;
+    }
+    return len;
+  }
+
+void join_strings(char string1[], char string2[]) {
+  int i, len1, len2;
+  len1 = find_length(string1);
+  len2 = find_length(string2);
+  for (i = len1; i < len1 + len2; i++) {
+    string1[i] = string2[i - len1];
+  }
+  string1[i] = '\0'; //adding null character at the end of input
+}
+/*returns 0 if thery are same otherwise returns 1*/
+
+int compare_strings(char string1[], char string2[]) {
+  int len1, len2, i, count = 0;
+  len1 = find_length(string1);
+  len2 = find_length(string2);
+   if (len1 != len2)
+    return 1;
+  for (i = 0; i < len1; i++) {
+    if (string1[i] == string2[i])
+      count++;
+  }
+  if (count == len1)
+    return 0;
+  return 1;
+}
+
+void copy_string(char destination[], char source[]) {
+  int len, i;
+  len = find_length(source);
+  for (i = 0; i < len; i++) {
+    destination[i] = source[i];
+  }
+  destination[i] = '\0';
+}
+
+int main() {
+  char string1[20], string2[20]; //string variables declaration with size 20
+  int choice;
+  while (1) {
+    printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n");
+    printf("Enter your choice: ");
+    scanf("%d", & choice);
+    switch (choice) {
+    case 1:
+      printf("Enter the string: ");
+      scanf("%s", string1);
+      printf("The length of string is %d", find_length(string1));
+      break;
+    case 2:
+      printf("Enter two strings: ");
+      scanf("%s%s", string1, string2);
+      join_strings(string1, string2);
+      printf("The concatenated string is %s", string1);
+      break;
+    case 3:
+      printf("Enter two strings: ");
+      scanf("%s%s", string1, string2);
+      if (compare_strings(string1, string2) == 0) {
+        printf("They are equal");
+      } else {
+        printf("They are not equal");
+      }
+      break;
+    case 4:
+      printf("Enter a string: ");
+      scanf("%s", string1);
+      printf("String1 = %s\n");
+      printf("After copying string1 to string 2\n");
+      copy_string(string2, string1);
+      printf("String2 = %s", string2);
+      break;
+    case 5:
+      exit(0);
+    }
+  }
+  return 0;
+}
+

26) Programs to swap two numbers using call by value and call by refernce.

/* Call by reference */
+
+#include <stdio.h>
+void swap(int*, int*);
+ 
+int main() {
+   
+   int x, y;
+ 
+   printf("Enter the value of x and y\n");
+   scanf("%d%d",&x,&y);
+ 
+   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
+ 
+   swap(&x, &y); 
+ 
+   printf("After Swapping\nx = %d\ny = %d\n", x, y);
+ 
+   return 0;
+}
+ 
+void swap(int *a, int *b)
+{
+   int temp;
+ 
+   temp = *b;
+   *b = *a;
+   *a = temp;
+}
+

call by value:-

/* Call by value */
+
+#include <stdio.h>
+ 
+void swap(int, int);
+ 
+int main() {
+   
+   int x, y;
+ 
+   printf("Enter the value of x and y\n");
+   scanf("%d%d",&x,&y);
+ 
+   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
+ 
+   swap(x, y); 
+ 
+   printf("After Swapping\nx = %d\ny = %d\n", x, y);
+ 
+   return 0;
+}
+ 
+void swap(int a, int b) {
+   int temp;
+ 
+   temp = b;
+   b = a;
+   a = temp;
+    printf("Values of a and b is %d  %d\n",a,b);
+}
+

27) Program to calculate factorial of a number with and without recursion both.

/* Recursion */
+
+#include<stdio.h>
+long int multiplyNumbers(int n);
+int main() {
+    
+int n;
+    printf("Enter a positive integer: ");
+    scanf("%d", &n);
+    printf("Factorial of %d = %ld", n, multiplyNumbers(n));
+    return 0;
+}
+long int multiplyNumbers(int n)
+{
+    if (n >= 1)
+        return n*multiplyNumbers(n-1);
+    else
+        return 1;
+}
+
/* Without recrsion: */
+
+#include <stdio.h>
+
+int main() {
+  
+  int c, n, fact = 1;
+ 
+  printf("Enter a number to calculate its factorial\n");
+  scanf("%d", &n);
+ 
+  for (c = 1; c <= n; c++)
+    fact = fact * c;
+ 
+  printf("Factorial of %d = %d\n", n, fact);
+ 
+  return 0;
+}
+

28) Program to print fibonacci series with and without recursion both.

/* Program to print fibonaci series with recursion */
+
+#include<stdio.h>
+void series(int);		//prototype
+
+int main() {
+
+  int n;
+
+printf("\n\nEnter the number of terms you wish.......");
+  scanf("%d",&n);
+  printf("\n\n");
+
+  series(n);			// function call
+  printf("\n\n\n");
+
+  return 0;
+}
+
+void series(int n)		// definition
+
+{
+   int t1=0,t2=1,next;
+
+   for(int i=0;i<=n;i++)
+  {
+    printf("%d\t",t1);
+
+    next=t1+t2;
+    t1=t2;
+    t2=next;
+  }
+}
+

// Fibonaci series without  recursion:-
+#include <stdio.h>
+int fibo(int);
+ 
+int main() {
+
+int num;
+int result;
+ 
+  printf("Enter the nth number in fibonacci series: ");
+    scanf("%d", &num);
+    if (num < 0)
+    {
+        printf("Fibonacci of negative number is not possible.\n");
+    }
+    else
+    {
+        result = fibo(num);
+        printf("The %d number in fibonacci series is %d\n", num, result);
+    }
+    return 0;
+}
+int fibo(int num)
+{
+    if (num == 0)
+    {
+        return 0;
+    }
+    else if (num == 1)
+    {
+        return 1;
+    }
+    else
+    {
+        return(fibo(num - 1) + fibo(num - 2));
+    }
+}
+

29) Program to calculate average of 5 numbers using function.

 /* program to find average of 5 numbers  */
+ 
+#include<stdio.h>
+int avg(int,int,int,int,int);      // prototype
+ 
+int main() { int a1,a2,a3,a4,a5,res;
+   
+   printf("\nEnter the numbers respectiively....");
+   scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
+   
+   res=avg(a1,a2,a3,a4,a5);     // function call
+   printf("Average of the numbers %d",res);
+   
+   return 0; 
+ }
+ 
+ int avg(int a1,int a2,int a3,int a4,int a5)      // definition
+ 
+ { int p; 
+   p=(a1+a2+a3+a4+a5)/5;
+   return p;
+ }
+

30) Program to implement linear serach and binary.

/* Program to implement linear search and Binary search */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main() {
+
+/* Declare variables - array_of_number,search_key,i,j,low,high*/
+
+int array[100],search_key,i,j,n,low,high,location,choice;
+
+  void linear_search(int search_key,int array[100],int n);
+
+  void binary_search(int search_key,int array[100],int n);
+
+  clrscr();
+
+/* read the elements of array */
+
+  printf("ENTER THE SIZE OF THE ARRAY:");
+
+  scanf("%d",&n);
+
+printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
+
+  for(i=1;i<=n;i++)
+  {
+  scanf("%d",&array[i]);
+
+}
+
+/* Get the Search Key element for Linear Search */
+
+  printf("ENTER THE SEARCH KEY:");
+
+  scanf("%d",&search_key);
+
+/* Choice of Search Algorithm */
+
+  printf("___________________\n");
+
+  printf("1.LINEAR SEARCH\n");
+
+  printf("2.BINARY SEARCH\n");
+
+  printf("___________________\n");
+
+  printf("ENTER YOUR CHOICE:");
+
+  scanf("%d",&choice);
+
+  switch(choice)
+  {
+   case 1:
+      linear_search(search_key,array,n);
+       break;
+
+  case 2:      binary_search(search_key,array,n);
+ break;
+
+default:
+
+exit(0);
+
+}
+
+ return 0;
+
+}
+
+/* LINEAR SEARCH */
+
+void linear_search(int search_key,int array[100],int n)
+   {
+
+/*Declare Variable */
+
+int i,location;
+
+for(i=1;i<=n;i++)
+       {
+
+  if(search_key == array[i])
+           {
+
+location = i;
+
+printf("______________________________________\n");
+
+printf("The location of Search Key = %d is %d\n",search_key,location);
+
+printf("______________________________________\n");
+
+}
+
+}
+
+}
+
+/* Binary Search to find Search Key */
+
+void binary_search(int search_key,int array[100],int n)
+{
+
+ int mid,i,low,high;
+
+  low = 1;
+
+  high = n;
+
+mid = (low + high)/2;
+
+i=1;
+
+while(search_key != array[mid])
+{
+
+  if(search_key <= array[mid])
+{
+
+low = 1;
+
+high = mid+1;
+
+mid = (low+high)/2;
+
+ }
+       else
+       {
+
+ low = mid+1;
+   high = n;
+
+mid = (low+high)/2;
+  }
+
+}
+
+printf("__________________________________\n");
+
+printf("location=%d\t",mid);
+
+printf("Search_Key=%d Found!\n",search_key);
+
+printf("__________________________________\n");
+
+}
+

31) Program to implement bubble sort.

   /* Bubble sort implementation  */
+ 
+#include <stdio.h>
+ 
+int main()
+{
+  int array[100], n, c, d, swap;
+ 
+  printf("Enter number of elements\n");
+  scanf("%d", &n);
+ 
+  printf("Enter %d integers\n", n);
+ 
+  for (c = 0; c < n; c++)
+    scanf("%d", &array[c]);
+ 
+  for (c = 0 ; c < n - 1; c++)
+  {
+    for (d = 0 ; d < n - c - 1; d++)
+    {
+      if (array[d] > array[d+1]) /* For decreasing order use < */
+      {
+        swap       = array[d];
+        array[d]   = array[d+1];
+        array[d+1] = swap;
+      }
+    }
+  }
+ 
+  printf("Sorted list in ascending order:\n");
+ 
+  for (c = 0; c < n; c++)
+     printf("%d\n", array[c]);
+ 
+  return 0;
+}
+

32) Program to store information of 10 students using array of structures.

/* Structures for student */
+
+#include<stdio.h>
+struct student
+{
+  char name[20],address[30];
+  int grade,roll,dob;
+};
+
+int main()
+{
+  struct student s[10];
+  int i;
+  for(i=0;i<10;i++)
+  {
+    printf("\nEnter records for student[%d]\n",i+1);
+    printf("Enter name: ");
+    gets(s[i].name);
+    printf("Enter address: ");
+    gets(s[i].address);
+    printf("Enter class, roll number and date of birth(year): ");
+    scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob);
+  }
+  printf("Records of the 10 students are here");
+  for(i=0;i<10;i++)
+  {
+    printf("\nStudent %d",i+1);
+    printf("\nName: %s",s[i].name);
+    printf("\nAddress: %s",s[i].address);
+    printf("\nClass: %d",s[i].grade);
+    printf("\nRoll No.: %d",s[i].roll);
+    printf("\nDOB: %d",s[i].dob);
+    printf("\n");
+  }
+  return 0;
+}
+

33) Programs to compute the transpose of a matrix.

include<stdio.h>
+int main()
+{
+    int a[10][10], transpose[10][10], r, c, i, j;
+    printf("Enter rows and columns of matrix: ");
+    scanf("%d %d", &r, &c);
+    // Storing elements of the matrix
+    printf("\nEnter elements of matrix:\n");
+    for(i=0; i<r; ++i)
+        for(j=0; j<c; ++j)
+        {
+            printf("Enter element 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 the transpose of matrix a
+    for(i=0; i<r; ++i)
+        for(j=0; j<c; ++j)
+        {
+            transpose[j][i] = a[i][j];
+        }
+    // Displaying the transpose of matrix a
+    printf("\nTranspose of Matrix:\n");
+    for(i=0; i<c; ++i)
+        for(j=0; j<r; ++j)
+        {
+            printf("%d  ",transpose[i][j]);
+            if(j==r-1)
+                printf("\n\n");
+        }
+    return 0;
+}
+
+

34) Program to print the address of variable using pointer.

#include <stdio.h>
+int main() {
+  int a;
+  int *pt;
+
+  a = 10;
+  pt = &a;
+
+  printf("\n[&a ]:Address of A = %p", &a);
+
+
+  return 0;
+}
+
+

35) Program to access array using pointer.

#include <stdio.h>
+int main()
+{
+   int data[5],i;
+   printf("Enter elements: ");
+   for(i = 0; i < 5; ++i)
+     scanf("%d", data + i);
+   printf("You entered: \n");
+   for(i = 0; i < 5; ++i)
+      printf("%d\n", *(data + i));
+   return 0;
+}
+
+
+
+
+
+
+
+
+ +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/image/1.jpg b/image/1.jpg new file mode 100644 index 0000000..7fa22ff Binary files /dev/null and b/image/1.jpg differ diff --git a/image/11.jpg b/image/11.jpg new file mode 100644 index 0000000..e469e3b Binary files /dev/null and b/image/11.jpg differ diff --git a/image/12.jpg b/image/12.jpg new file mode 100644 index 0000000..8665373 Binary files /dev/null and b/image/12.jpg differ diff --git a/image/13.jpg b/image/13.jpg new file mode 100644 index 0000000..4c0fc3c Binary files /dev/null and b/image/13.jpg differ diff --git a/image/14.jpg b/image/14.jpg new file mode 100644 index 0000000..a57a6fb Binary files /dev/null and b/image/14.jpg differ diff --git a/image/15.jpg b/image/15.jpg new file mode 100644 index 0000000..ab24dd2 Binary files /dev/null and b/image/15.jpg differ diff --git a/image/16.jpg b/image/16.jpg new file mode 100644 index 0000000..051a117 Binary files /dev/null and b/image/16.jpg differ diff --git a/image/17.jpg b/image/17.jpg new file mode 100644 index 0000000..275bab1 Binary files /dev/null and b/image/17.jpg differ diff --git a/image/18.jpg b/image/18.jpg new file mode 100644 index 0000000..a402b98 Binary files /dev/null and b/image/18.jpg differ diff --git a/image/19i.jpg b/image/19i.jpg new file mode 100644 index 0000000..be0808a Binary files /dev/null and b/image/19i.jpg differ diff --git a/image/19ii.jpg b/image/19ii.jpg new file mode 100644 index 0000000..2b0a922 Binary files /dev/null and b/image/19ii.jpg differ diff --git a/image/2.jpg b/image/2.jpg new file mode 100644 index 0000000..807e235 Binary files /dev/null and b/image/2.jpg differ diff --git a/image/20.jpg b/image/20.jpg new file mode 100644 index 0000000..6534218 Binary files /dev/null and b/image/20.jpg differ diff --git a/image/21.jpg b/image/21.jpg new file mode 100644 index 0000000..c4c084f Binary files /dev/null and b/image/21.jpg differ diff --git a/image/22.jpg b/image/22.jpg new file mode 100644 index 0000000..7bdbfa3 Binary files /dev/null and b/image/22.jpg differ diff --git a/image/23.jpg b/image/23.jpg new file mode 100644 index 0000000..4877114 Binary files /dev/null and b/image/23.jpg differ diff --git a/image/24.jpg b/image/24.jpg new file mode 100644 index 0000000..55bfe51 Binary files /dev/null and b/image/24.jpg differ diff --git a/image/25.jpg b/image/25.jpg new file mode 100644 index 0000000..14e72f5 Binary files /dev/null and b/image/25.jpg differ diff --git a/image/26i.jpg b/image/26i.jpg new file mode 100644 index 0000000..59bcaa4 Binary files /dev/null and b/image/26i.jpg differ diff --git a/image/26ii.jpg b/image/26ii.jpg new file mode 100644 index 0000000..a68d201 Binary files /dev/null and b/image/26ii.jpg differ diff --git a/image/27i.jpg b/image/27i.jpg new file mode 100644 index 0000000..6c71639 Binary files /dev/null and b/image/27i.jpg differ diff --git a/image/27ii.jpg b/image/27ii.jpg new file mode 100644 index 0000000..456a033 Binary files /dev/null and b/image/27ii.jpg differ diff --git a/image/28.jpg b/image/28.jpg new file mode 100644 index 0000000..09fbef7 Binary files /dev/null and b/image/28.jpg differ diff --git a/image/29.jpg b/image/29.jpg new file mode 100644 index 0000000..fe1803d Binary files /dev/null and b/image/29.jpg differ diff --git a/image/3.jpg b/image/3.jpg new file mode 100644 index 0000000..c304ee8 Binary files /dev/null and b/image/3.jpg differ diff --git a/image/30i.jpg b/image/30i.jpg new file mode 100644 index 0000000..0aedb89 Binary files /dev/null and b/image/30i.jpg differ diff --git a/image/30ii.jpg b/image/30ii.jpg new file mode 100644 index 0000000..b954226 Binary files /dev/null and b/image/30ii.jpg differ diff --git a/image/31.jpg b/image/31.jpg new file mode 100644 index 0000000..61ae0b0 Binary files /dev/null and b/image/31.jpg differ diff --git a/image/32.jpg b/image/32.jpg new file mode 100644 index 0000000..5771c6c Binary files /dev/null and b/image/32.jpg differ diff --git a/image/33.jpg b/image/33.jpg new file mode 100644 index 0000000..22f6387 Binary files /dev/null and b/image/33.jpg differ diff --git a/image/35i.jpg b/image/35i.jpg new file mode 100644 index 0000000..02cee84 Binary files /dev/null and b/image/35i.jpg differ diff --git a/image/35ii.jpg b/image/35ii.jpg new file mode 100644 index 0000000..3c03c61 Binary files /dev/null and b/image/35ii.jpg differ diff --git a/image/4.jpg b/image/4.jpg new file mode 100644 index 0000000..72ae62e Binary files /dev/null and b/image/4.jpg differ diff --git a/image/5.jpg b/image/5.jpg new file mode 100644 index 0000000..29465f0 Binary files /dev/null and b/image/5.jpg differ diff --git a/image/6.jpg b/image/6.jpg new file mode 100644 index 0000000..59347c2 Binary files /dev/null and b/image/6.jpg differ diff --git a/image/7.jpg b/image/7.jpg new file mode 100644 index 0000000..85e5de5 Binary files /dev/null and b/image/7.jpg differ diff --git a/image/8.jpg b/image/8.jpg new file mode 100644 index 0000000..2ae5251 Binary files /dev/null and b/image/8.jpg differ diff --git a/image/9.jpg b/image/9.jpg new file mode 100644 index 0000000..f116ed2 Binary files /dev/null and b/image/9.jpg differ diff --git a/iqbal-1915320.md b/iqbal-1915320.md new file mode 100644 index 0000000..693999c --- /dev/null +++ b/iqbal-1915320.md @@ -0,0 +1,5440 @@ +![](https://jaspreetsarao.files.wordpress.com/2012/05/gne.png) +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105)** +## NAME : GURKIRAT SINGH +### ROLL NO : 1915314 +### BRANCH : COMPUTER SCIENCE +### SECTION : C '1' +____ +### 1. *PROGRAM TO FIND A NUMBER IS EVEN OR ODD* +```C +#include + +int main() +{ + int n; + + printf("Enter an integer\n"); + scanf("%d", &n); + + if (n%2 == 0) + printf("Even\n"); + else + printf("Odd\n"); + + return 0; +} +``` +____ + +#2.PROGRAM OF SWAP TWO NUMBERS WITHOUT USING THIRD VAULE +```C +#include + int main() +{ +int a=10, b=20; +printf("Before swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a= (30-10) +printf("\nAfter swap a=%d b=%d",a,b); +return 0; +} +``` + + + +___ +```C +## 3.To print our name using puts +#include + +int main(){ + + char name[]="Gurkirat Singh"; + + printf("My name is: "); + puts(name); + + return 0; +} +``` +___ + + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + + + + + + diff --git a/my pps file b/my pps file new file mode 100644 index 0000000..a799168 --- /dev/null +++ b/my pps file @@ -0,0 +1,1358 @@ +--- +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + diff --git a/my pps project b/my pps project new file mode 100644 index 0000000..693999c --- /dev/null +++ b/my pps project @@ -0,0 +1,5440 @@ +![](https://jaspreetsarao.files.wordpress.com/2012/05/gne.png) +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105)** +## NAME : GURKIRAT SINGH +### ROLL NO : 1915314 +### BRANCH : COMPUTER SCIENCE +### SECTION : C '1' +____ +### 1. *PROGRAM TO FIND A NUMBER IS EVEN OR ODD* +```C +#include + +int main() +{ + int n; + + printf("Enter an integer\n"); + scanf("%d", &n); + + if (n%2 == 0) + printf("Even\n"); + else + printf("Odd\n"); + + return 0; +} +``` +____ + +#2.PROGRAM OF SWAP TWO NUMBERS WITHOUT USING THIRD VAULE +```C +#include + int main() +{ +int a=10, b=20; +printf("Before swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a= (30-10) +printf("\nAfter swap a=%d b=%d",a,b); +return 0; +} +``` + + + +___ +```C +## 3.To print our name using puts +#include + +int main(){ + + char name[]="Gurkirat Singh"; + + printf("My name is: "); + puts(name); + + return 0; +} +``` +___ + + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + + + + + + diff --git a/p10.c b/p10.c new file mode 100644 index 0000000..7940c97 --- /dev/null +++ b/p10.c @@ -0,0 +1,18 @@ +// Find roots of a quadratic equation +#include +#include +void main() +{ +float a,b,c,discriminant; +float root1, root2; +printf("\nEnter the coefficient of x^2 (a)::"); +scanf("%f",&a); +printf("\nEnter the coefficient of x (b)::"); +scanf("%f",&b); +printf("\nEnter the constant term (c)::"); +scanf("%f",&c); +discriminant =pow(b,2)-4*a*c; +root1=( -b + sqrt(discriminant))/(2*a); +root2=(-b - sqrt(discriminant))/(2*a); +printf("\nRoots are :: %f %f \n", root1,root2); +} diff --git a/p11.c b/p11.c new file mode 100644 index 0000000..44f4b48 --- /dev/null +++ b/p11.c @@ -0,0 +1,22 @@ +// To check whether a year is leap year or not +#include +void main() + { + int year; + printf("\nEnter a year::"); + scanf("%d",&year); + if(year%4==0) +{ +if(year%100 == 0) +{ +if(year%400 == 0) +printf("\n%d is a leap year\n",year); +else +printf("\n%d is not a leap year\n",year); +} +else +printf("\n%d is a leap year\n",year); +} +else +printf("\n%d is not a leap year",year); +} diff --git a/p12.c b/p12.c new file mode 100644 index 0000000..6f20c78 --- /dev/null +++ b/p12.c @@ -0,0 +1,8 @@ +// Program to generate multiple of 5 +#include +void main() + { + int i; +for (i=1;i <=10; i++) +printf("\n5\t*\t%d\t=\t%d",i,5*i); +} diff --git a/p13.c b/p13.c new file mode 100644 index 0000000..7b5213a --- /dev/null +++ b/p13.c @@ -0,0 +1,39 @@ +// To make simple calculator using switch + +#include +void main() + { + int num1, num2, choice; +float result; + +printf("\nEnter the first number::"); +scanf("%d",&num1); +printf("\nEnter the second number::"); +scanf("%d",&num2); +printf("\n\t\tMENU"); +printf("\n1 : ADDITION"); +printf("\n2 : SUBTRACTION"); +printf("\n3 : MULTIPLICATION"); +printf("\n4 : DiVISION"); +printf("\n Enter your choice of Operation to be performed::"); +scanf("%d",&choice); +switch(choice) +{ +case 1: +result = num1+num2; +printf("\nThe Sum is %f", result); +break; +case 2: +result=num1-num2; +printf("\nThe Difference is %f",result); +break; +case 3: +result=num1*num2; +printf("\nThe product is %f", result); +break; +case 4: +result = num1/num2; +printf("\nThe quotient is %f", result); +break; +} +} diff --git a/p14.c b/p14.c new file mode 100644 index 0000000..83394a0 --- /dev/null +++ b/p14.c @@ -0,0 +1,15 @@ +// Calculate reverse of a number +#include +void main() +{ +int number, remainder, reverse_no=0; +printf("\nEnter a number::"); +scanf("%d",&number); +while(number != 0) +{ +remainder = number % 10; +reverse_no = reverse_no * 10 + remainder; +number = number / 10; +} +printf("\n Reverse of the number is %d::", reverse_no); +} diff --git a/p15.c b/p15.c new file mode 100644 index 0000000..58aaa1b --- /dev/null +++ b/p15.c @@ -0,0 +1,22 @@ +// Check whether a number is palindrome + +#include +void main() +{ +int number, remainder, original_no, reverse_no=0; +printf("\nEnter a number::"); +scanf("%d",&number); +original_no=number; +while(number != 0) +{ +remainder = number % 10; +reverse_no = reverse_no * 10 + remainder; +number = number / 10; +} +printf("\n Reverse of the number is %d::", reverse_no); +if (original_no == reverse_no) +printf("\nThe number is a palindrome"); +else +printf("\n The number is not a palindrome"); +} + diff --git a/p16.c b/p16.c new file mode 100644 index 0000000..de5a690 --- /dev/null +++ b/p16.c @@ -0,0 +1,21 @@ +//PROGRAM TO CHECK WHETHER A NO. IS PRIME OR NOT +#include +void main() +{ +int number,i,m=0,flag=0; +printf("Enter a number::"); +scanf("%d",&number); +m=number/2; +for(i=2;i<=m;i++) +{ +if(number%i==0) +{ +printf("\nNumber is NOT prime\n"); +flag=1; +break; +} +} +if(flag==0) +printf("\nNumber IS prime\n"); + } + diff --git a/p17.c b/p17.c new file mode 100644 index 0000000..97255c5 --- /dev/null +++ b/p17.c @@ -0,0 +1,24 @@ +//PROGRAM TO PRINT PRIME NUMBERS FROM 1 TO 100 +#include +void main() +{ + int i, Number, flag; + printf("\nPrime Numbers from 1 to 100 are:: \n"); + for(Number = 1; Number <= 100; Number++) + { + flag = 0; + for (i = 2; i <= Number/2; i++) + { + if(Number%i == 0) + { + flag=1; + break; + } + } + if(flag == 0 && Number != 1 ) + { + printf("%d ", Number); + } + } +printf("\n"); + } diff --git a/p18.c b/p18.c new file mode 100644 index 0000000..32550f1 --- /dev/null +++ b/p18.c @@ -0,0 +1,17 @@ +//PROGRAM TO CHECK WHETHER A NO. IS AN ARMSTRONG NUMBER OR NOT +#include #include +void main() +{ + int number, sum = 0, rem = 0, cube = 0, original_no; + printf ("\nEnter a number::"); + { + rem = number % 10; + cube = pow(rem, 3); + sum = sum + cube; + number = number / 10; + } + if (sum == original_no) + printf ("\nThe given no IS an ARMSTRONG no\n"); + else + printf ("\nThe given no is NOT an ARMSTRONG no\n"); + } diff --git a/p2.c b/p2.c new file mode 100644 index 0000000..7f84454 --- /dev/null +++ b/p2.c @@ -0,0 +1,6 @@ +//print name using puts +#include +void main() +{ +puts("My name is Nimrat Kaur"); +} diff --git a/p3.c b/p3.c new file mode 100644 index 0000000..f490099 --- /dev/null +++ b/p3.c @@ -0,0 +1,8 @@ +//PROGRAM TO PRINT ADDRESS OF COLLEGE +#include +void main() +{ +printf("\nGuru Nanak Dev Engineering College,"); +printf("\n Gill Road,"); +printf("\n Ludhiana, Punjab\n"); +} diff --git a/p4.c b/p4.c new file mode 100644 index 0000000..973bd12 --- /dev/null +++ b/p4.c @@ -0,0 +1,13 @@ +//PROGRAM TO FIND SUM OF TWO NUMBERS +#include +void main() +{ +int a,b,c; +printf("\nEnter first number"); +scanf("%d",&a); +printf("\n Enter second number"); +scanf("%d",&b); +c=a+b; +printf("\n The sum is:: %d",c); +printf("\n"); +} diff --git a/p5-1.c b/p5-1.c new file mode 100644 index 0000000..3f697fb --- /dev/null +++ b/p5-1.c @@ -0,0 +1,12 @@ +//swap two numbers without using third variable +#include + int main() +{ +int a=10, b=20; +printf("\nBefore swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a=20 (30-10) +printf("\nAfter swap a=%d b=%d\n",a,b); +} + diff --git a/p5.c b/p5.c new file mode 100644 index 0000000..e5a23d9 --- /dev/null +++ b/p5.c @@ -0,0 +1,14 @@ +//PROGRAM TO PRINT THE QUOTIENT AND REMAINDER +#include +void main() +{ +int a,b,c,d; +printf("\nEnter first number"); +scanf("%d",&a); +printf("\nEnter second number"); +scanf("%d",&b); +c=a/b; +printf("\nThe quotient is %d",c); +d=a%b; +printf("\nThe remainder is %d\n",d); +} diff --git a/p6.c b/p6.c new file mode 100644 index 0000000..9c5e069 --- /dev/null +++ b/p6.c @@ -0,0 +1,13 @@ +//PROGRAM TO CHECK IF A NUMBER IS EVEN OR ODD +#include +void main() +{ +int a,d; +printf("\nEnter a number"); +scanf("%d",&a); +d=a%2; +if(d==0) +printf("\n The number is even"); +else +printf("\n The number is odd\n"); +} diff --git a/p7.c b/p7.c new file mode 100644 index 0000000..5d84d40 --- /dev/null +++ b/p7.c @@ -0,0 +1,14 @@ +//program to find greater of two numbers +#include +void main() +{ +int a,b,c; +printf("\nEnter the first number"); +scanf("%d",&a); +printf("\nEnter the second number"); +scanf("%d",&b); +if(a>b) +printf("\n The larger number is %d",a); +else +printf("\n The larger number is %d",b); +} diff --git a/p8.c b/p8.c new file mode 100644 index 0000000..13051c5 --- /dev/null +++ b/p8.c @@ -0,0 +1,18 @@ +//Greatest of three numbers +#include +void main() +{ +int a,b,c; +printf("Enter the first number"); +scanf("%d",&a); +printf("Enter the second number"); +scanf("%d",&b); +printf("Enter the third number"); +scanf("%d",&c); +if((a>b) && (a>c)) +printf("\n The largest number is %d",a); +else if((b>a) && (b>c)) +printf("\n The largest number is %d",b); +else +printf("\n The largest number is %d",c); +} diff --git a/p9.c b/p9.c new file mode 100644 index 0000000..dadeac1 --- /dev/null +++ b/p9.c @@ -0,0 +1,19 @@ +#include +void main() +{ +int marks; +printf("Enter the marks of the student out of 100::"); +scanf("%d",&marks); +if(marks>=90) +printf("\nO Grade\n"); +else if(marks>=75 && marks<90) +printf("\nA Grade\n"); +else if(marks>60 && marks <75) +printf("\nB Grade\n"); +else if(marks>=50 && marks <60) +printf("\nC Grade\n"); +else if (marks>=40 && marks <50) +printf("\nD Grade\n"); +else +printf("\nE Grade\n"); +} diff --git a/pps.md b/pps.md new file mode 100644 index 0000000..693999c --- /dev/null +++ b/pps.md @@ -0,0 +1,5440 @@ +![](https://jaspreetsarao.files.wordpress.com/2012/05/gne.png) +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **Programming for Problem Solving** +## **Name** - Iqbal Singh +## **CRN -1915320** +## **Branch - CSE-C1** +## **Submitted to:- MRS Goldendeep Kaur ** +--- + +### 1) To print name. +```C + /* Program to print your name */ + +#include +int main() { + +puts("**************"); +puts("Iqbal singh"); +puts("**************"); + +return 0; +} +``` +### 2) To print College address. +```C + /* College Address */ + +#include +int main() { + +printf("\n\t\t\tGuru Nanak Dev Engineering College,"); +printf("\n\t\t\tGill Road,"); +printf("\n\t\t\tLudhiana , Punjab"); + +return 0; +} +``` +### 3) Program to add two integers. +```C + /* To add two integers */ + +#include +int main() { + +int a,b; + +printf("\nEnter the numbers...."); + +printf("\nA:"); +scanf("%d",&a); + +printf("\nB:"); +scanf("%d",&b); + +a=a+b; + +printf("\n Sum of the number is %d ",a); + +return 0; + +} +``` +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +#include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + +# **PROGRAMMING FOR PROBLEM SOLVING (ESC-18104/18105)** +## NAME : GURKIRAT SINGH +### ROLL NO : 1915314 +### BRANCH : COMPUTER SCIENCE +### SECTION : C '1' +____ +### 1. *PROGRAM TO FIND A NUMBER IS EVEN OR ODD* +```C +#include + +int main() +{ + int n; + + printf("Enter an integer\n"); + scanf("%d", &n); + + if (n%2 == 0) + printf("Even\n"); + else + printf("Odd\n"); + + return 0; +} +``` +____ + +#2.PROGRAM OF SWAP TWO NUMBERS WITHOUT USING THIRD VAULE +```C +#include + int main() +{ +int a=10, b=20; +printf("Before swap a=%d b=%d",a,b); +a=a+b;//a=30 (10+20) +b=a-b;//b=10 (30-20) +a=a-b;//a= (30-10) +printf("\nAfter swap a=%d b=%d",a,b); +return 0; +} +``` + + + +___ +```C +## 3.To print our name using puts +#include + +int main(){ + + char name[]="Gurkirat Singh"; + + printf("My name is: "); + puts(name); + + return 0; +} +``` +___ + + +### 4) Program to find quotient and remainder. +```C + /* To find quotient and remainder */ + +#include + +int main() { + +int a,b,r,q; + +printf("\nEnter the Dividend:"); +scanf("%d",&a); + +printf("\nEnter the divisor:"); +scanf("%d",&b); + +r=a%b; +q=a/b; + +printf("\nRemainder: %d",r); +printf("\nQuotient: %d",q); + +return 0; +} +``` +### 5) Program to swap two variables without 3rd variable. +```C + /* Swapping without 3rd variable */ + +#include +int main() { + +int a,b; + +printf("\nEnter the value of A:"); +scanf("%d",&a); + +printf("\nEnter the value of B:"); +scanf("%d",&b); + + a = a + b; + b = a - b; + a = a - b; + +printf("\nA: %d",a); +printf("\nB: %d",b); + +return 0; +} +``` +### 6) Program to check even odd number. +```C +/* To find whether number is even or odd */ + +#include +int main() { + + int num,temp; + + printf("Enter the Number:"); + scanf("%d",&num); + + if(num%2==0) + printf("\nNumber is Even...."); + + else + printf("\nNumber is Odd...."); + + printf("\n\n"); + return 0; +} +``` +### 7) Finding greteast of two numbers. +```C + /* Largest one in two */ + +#include + +int main() { + int a,b; + printf("Enter any two number(A and B): "); + scanf("%d%d", &a, &b); + +if(a>b) +printf("\nA is largest...."); +else + printf("\nB is largest....."); + +return 0; +} +``` +### 8) Find greatest of three number . +```C +/* Largest of three number */ + +#include +int main() { +int x, y, z, large; + + printf(" Enter any three integer numbers for x, y, z : ") ; + +scanf("%d %d %d", &x, &y, &z) ; + +large = (x > y ? ( x > z ? x : z) : (y > z ? y : z)) ; + +printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ; + + return 0; +} +``` +### 9) Program to assign grade to student according to percentage. +```C +/* To find grade of a student by marks */ + +#include +int main() { + + int s1,s2,s3,s4,s5,agg; + float perc; + + printf("Enter the Marks in 5 Subjects Respectively:\n"); + +scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5); + +agg=s1+s2+s3+s4+s5; // Aggregate Marks + + perc=agg/500.0*100; // Perc Marks + + if(perc>=90) + { + printf("\nA"); + + } + + else if (perc>=80 && perc<90) + { + printf("\nB"); + + } + + else if(perc>=70 && perc<80) + { + printf("\nC"); + + } + + else if(perc>=60 && perc<70) + { + printf("\nD"); + } + else if(perc>=50 && perc<60) + { + printf("\nE"); + } + else + { + printf("\nScope of Improvement...."); + } + return 0; +} +``` +### 10) Program to print roots of quadratic equation. +```C +/*Program to print roots */ + +#include +#include +#include + +int main() { + float a, b, c, root1, root2; + float realp, imagp, disc; + +printf("Enter the values of a, b and c \n"); + scanf("%f %f %f", &a, &b, &c); + +/* If a = 0, it is not a quadratic equation */ + +if (a == 0 || b == 0 || c == 0) + { + printf("Error: Roots cannot be determined \n"); + exit(1); + } + else + { + disc = b * b - 4.0 * a * c; + if (disc < 0) + { + printf("Imaginary Roots\n"); + realp = -b / (2.0 * a) ; + imagp = sqrt(abs(disc)) / (2.0 * a); + printf("Root1 = %f +i %f\n", realp, imagp); + printf("Root2 = %f -i %f\n", realp, imagp); + } + +else if (disc == 0) + { + printf("Roots are real and equal\n"); + root1 = -b / (2.0 * a); + root2 = root1; + printf("Root1 = %f\n", root1); + printf("Root2 = %f\n", root2); + } + +else if (disc > 0 ) + { + printf("Roots are real and distinct \n"); + root1 =(-b + sqrt(disc)) / (2.0 * a); + root2 =(-b - sqrt(disc)) / (2.0 * a); + printf("Root1 = %f \n", root1); + printf("Root2 = %f \n", root2); + } + +} + return 0; +} +``` +### 11) Program to check year is leap or not. +```C +/* To find whether year is leap or not */ + +#include +int main() { + + int year,temp; + + printf("Enter teh year:"); + scanf("%d",&year); + + temp=year%4; + + if(year%100==0) + { + if(year%400==0) + printf("\nLeap year..."); + } + + else + { + if(year%4==0) + printf("\nLeap year..."); + +else + printf("\nNot a Leap year..."); + } + + return 0; +} +``` +### 12) Program to print table of 5. +```C +/* Table of 5 */ + +#include + +int main() { int res; + +for(int i=1;i<=10;i++) { + +res=5*i; + + printf("\n5*%d=%d",i,res); + } + + return 0; +} +``` +### 13) To make simple calculator using switch case. +```C +/* C Program to Create Simple Calculator using Switch Case */ + +#include + +int main() { + char Operator; + float num1, num2, result = 0; + +printf("\n Please Enter an Operator (+, -, *, /) : "); +scanf("%c", &Operator); + +printf("\n Please Enter the Values for two Operands: num1 and num2 : "); +scanf("%f%f", &num1, &num2); + +switch(Operator) + { + case '+': + result = num1 + num2; + break; + case '-': + result = num1 - num2; + break; + case '*': + result = num1 * num2; + break; + case '/': + result = num1 / num2; + break; + default: + printf("\n You have enetered an Invalid Operator "); + } + +printf("\n The result of %.2f %c %.2f = %.2f", num1, Operator, num2, result); + +return 0; +} +``` +### 14) To calculate reverse of a number. +```C +/* To find reverse of a Number*/ + +#include +int main() { + + int num,rev=0; + + printf("\nEnter the Number:"); + scanf("%ld",&num); + +while(num!=0) +{ + rev = rev * 10; + rev = rev + num%10; + num = num/10; + } + + + printf("\nReversed number:%d",rev); + + printf("\n\n"); + return 0; +} +``` +### 15) To check whether number is palindrome or not. +```C +/* Palindrome */ + +#include +int main() { + + int n,rev=0,check,rem; + + printf("\nEnter the number:"); + scanf("%d",&n); + check=n; + + while( n!=0 ) + { + rem = n%10; + rev = rev*10 + rem; + n /= 10; + } + + if(rev==check) + printf("\nReversed number is equal to entered number...."); + + else + printf("\nReversed number is not equal to entered number...."); + + printf("\n\n"); + return 0; +} +``` +### 16) To check whether a number is prime or not. +```C +/* Program to check prime no. */ + +#include +#include + +int main() { + + int num, j, flag; + + printf("Enter a number \n"); + scanf("%d", &num); + + if (num <= 1) + { + printf("%d is not a prime numbers \n", num); + exit(1); + } + flag = 0; + for (j = 2; j <= num / 2; j++) + { + if ((num % j) == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number \n", num); + else + printf("%d is not a prime number \n", num); + +return 0; + +} +``` +### 17) Program to print prime number to 100. +```C +/* Prime number from 1 to 100 */ + + #include + + int main(){ + +int numbr,k,remark; + +printf(" The prime numbers between 1 and 100 : \n"); + + for(numbr=2;numbr<=100;++numbr) + + { + + remark=0; + + for(k=2;k<=numbr/2;k++){ + + if((numbr % k) == 0){ + + remark++; + + break; + } + + } + + if(remark==0) + printf("\n %d ",numbr); + + } + + return 0; + + } + ``` +### 18) Program to check whether a number is armstrong or not. +```C +/* To check armstrong number */ + +#include +int main() +{ + int number, originalNumber, remainder, result = 0; + +printf("Enter a three digit integer: "); + scanf("%d", &number); + +originalNumber = number; + +while (originalNumber != 0) + { + remainder = originalNumber%10; + result += remainder*remainder*remainder; + originalNumber /= 10; + +} + +if(result == number) + printf("%d is an Armstrong number.",number); + else + printf("%d is not an Armstrong number.",number); + +return 0; +} + +``` +### 19) Print Different Patterns. +i) Pattern 1. +```C +#include +int main() { + + int i,j,r; + + printf("Enter number of rows: "); + scanf("%d",&r); + +for(i=1; i<=r; ++i) + { + for(j=1; j<=i; ++j) + { + printf("%d ",j); + } + printf("\n"); + } + return 0; +} + +``` +### ii) Pattern 2. +```C +#include +int main() { + + int r,i,j,num= 1; + printf("Enter number of rows: "); + scanf("%d",&r); + for(i=1;i<=r;i++) + { + for(j=1;j<=i;++j) + { + printf("%d",num); + ++num; + } + printf("\n"); + } + return 0; +} +``` +### 20) Program to find largest from 1 dimensional array. +```C +/* Largest in 1 dimensional array */ + +#include +int main() { + + int i, n; + float arr[100]; + + printf("Enter total number of elements(1 to 100): "); + scanf("%d", &n); + printf("\n"); + + +// Stores number entered by the user + for(i = 0; i < n; ++i) + { + printf("Enter Number %d: ", i+1); + scanf("%f", &arr[i]); + } + // Loop to store largest number to arr[0] + for(i = 1; i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} +``` +### 21) To find sumof the N natural numbers in an array. +```C +/* Sum of N no.s in array */ + +#include + +int main() { + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, sum = 0, c, array[100]; + +printf("Enter the number of integers you want to add: "); + scanf("%d", &n); + + printf("\n\nEnter %d integers \n\n", n); + +for(c = 0; c < n; c++) + { + scanf("%d", &array[c]); + sum += array[c]; // same as sum = sum + array[c] + } + + printf("\n\nSum = %d\n\n", sum); + return 0; +} +``` +### 22) Program to add two matrices . +```C +/* Addition of matrix */ + +#include + +int main() { + + int m, n, c, d, first[10][10], second[10][10], sum[10][10]; + + printf("Enter the number of rows and columns of matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter the elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter the elements of second matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0 ; d < n; d++) + scanf("%d", &second[c][d]); + + printf("Sum of entered matrices:-\n"); + + for (c = 0; c < m; c++) { + for (d = 0 ; d < n; d++) { + sum[c][d] = first[c][d] + second[c][d]; + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + return 0; +} +``` +### 23) Program to multiply two matrices . +```C +/* Multiplation of matrices */ + +#include +int main() + { + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The matrices can't be multiplied with each other.\n"); + + else + { + printf("Enter elements of second matrix\n"); + +for (c=0;c +#include + +int main() { + char s[1000]; + int i,n,c=0; + + printf("Enter the string : "); + gets(s); + n=strlen(s); + +for(i=0;i +#include + +int find_length(char string[]) { + int len = 0, i; + for (i = 0; string[i] != '\0'; i++) { + len++; + } + return len; + } + +void join_strings(char string1[], char string2[]) { + int i, len1, len2; + len1 = find_length(string1); + len2 = find_length(string2); + for (i = len1; i < len1 + len2; i++) { + string1[i] = string2[i - len1]; + } + string1[i] = '\0'; //adding null character at the end of input +} +/*returns 0 if thery are same otherwise returns 1*/ + +int compare_strings(char string1[], char string2[]) { + int len1, len2, i, count = 0; + len1 = find_length(string1); + len2 = find_length(string2); + if (len1 != len2) + return 1; + for (i = 0; i < len1; i++) { + if (string1[i] == string2[i]) + count++; + } + if (count == len1) + return 0; + return 1; +} + +void copy_string(char destination[], char source[]) { + int len, i; + len = find_length(source); + for (i = 0; i < len; i++) { + destination[i] = source[i]; + } + destination[i] = '\0'; +} + +int main() { + char string1[20], string2[20]; //string variables declaration with size 20 + int choice; + while (1) { + printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n"); + printf("Enter your choice: "); + scanf("%d", & choice); + switch (choice) { + case 1: + printf("Enter the string: "); + scanf("%s", string1); + printf("The length of string is %d", find_length(string1)); + break; + case 2: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + join_strings(string1, string2); + printf("The concatenated string is %s", string1); + break; + case 3: + printf("Enter two strings: "); + scanf("%s%s", string1, string2); + if (compare_strings(string1, string2) == 0) { + printf("They are equal"); + } else { + printf("They are not equal"); + } + break; + case 4: + printf("Enter a string: "); + scanf("%s", string1); + printf("String1 = %s\n"); + printf("After copying string1 to string 2\n"); + copy_string(string2, string1); + printf("String2 = %s", string2); + break; + case 5: + exit(0); + } + } + return 0; +} +``` +### 26) Programs to swap two numbers using call by value and call by refernce. +```C +/* Call by reference */ + +#include +void swap(int*, int*); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(&x, &y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int *a, int *b) +{ + int temp; + + temp = *b; + *b = *a; + *a = temp; +} +``` +### call by value:- +```C +/* Call by value */ + +#include + +void swap(int, int); + +int main() { + + int x, y; + + printf("Enter the value of x and y\n"); + scanf("%d%d",&x,&y); + + printf("Before Swapping\nx = %d\ny = %d\n", x, y); + + swap(x, y); + + printf("After Swapping\nx = %d\ny = %d\n", x, y); + + return 0; +} + +void swap(int a, int b) { + int temp; + + temp = b; + b = a; + a = temp; + printf("Values of a and b is %d %d\n",a,b); +} +``` + +### 27) Program to calculate factorial of a number with and without recursion both. +```C +/* Recursion */ + +#include +long int multiplyNumbers(int n); +int main() { + +int n; + printf("Enter a positive integer: "); + scanf("%d", &n); + printf("Factorial of %d = %ld", n, multiplyNumbers(n)); + return 0; +} +long int multiplyNumbers(int n) +{ + if (n >= 1) + return n*multiplyNumbers(n-1); + else + return 1; +} +``` + +```C +/* Without recrsion: */ + +#include + +int main() { + + int c, n, fact = 1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + + for (c = 1; c <= n; c++) + fact = fact * c; + + printf("Factorial of %d = %d\n", n, fact); + + return 0; +} +``` +### 28) Program to print fibonacci series with and without recursion both. +```C +/* Program to print fibonaci series with recursion */ + +#include +void series(int); //prototype + +int main() { + + int n; + +printf("\n\nEnter the number of terms you wish......."); + scanf("%d",&n); + printf("\n\n"); + + series(n); // function call + printf("\n\n\n"); + + return 0; +} + +void series(int n) // definition + +{ + int t1=0,t2=1,next; + + for(int i=0;i<=n;i++) + { + printf("%d\t",t1); + + next=t1+t2; + t1=t2; + t2=next; + } +} +``` +# +```C +// Fibonaci series without recursion:- +#include +int fibo(int); + +int main() { + +int num; +int result; + + printf("Enter the nth number in fibonacci series: "); + scanf("%d", &num); + if (num < 0) + { + printf("Fibonacci of negative number is not possible.\n"); + } + else + { + result = fibo(num); + printf("The %d number in fibonacci series is %d\n", num, result); + } + return 0; +} +int fibo(int num) +{ + if (num == 0) + { + return 0; + } + else if (num == 1) + { + return 1; + } + else + { + return(fibo(num - 1) + fibo(num - 2)); + } +} +``` +### 29) Program to calculate average of 5 numbers using function. +```C + /* program to find average of 5 numbers */ + +#include +int avg(int,int,int,int,int); // prototype + +int main() { int a1,a2,a3,a4,a5,res; + + printf("\nEnter the numbers respectiively...."); + scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); + + res=avg(a1,a2,a3,a4,a5); // function call + printf("Average of the numbers %d",res); + + return 0; + } + + int avg(int a1,int a2,int a3,int a4,int a5) // definition + + { int p; + p=(a1+a2+a3+a4+a5)/5; + return p; + } + ``` +### 30) Program to implement linear serach and binary. + ```C +/* Program to implement linear search and Binary search */ + +#include +#include + +int main() { + +/* Declare variables - array_of_number,search_key,i,j,low,high*/ + +int array[100],search_key,i,j,n,low,high,location,choice; + + void linear_search(int search_key,int array[100],int n); + + void binary_search(int search_key,int array[100],int n); + + clrscr(); + +/* read the elements of array */ + + printf("ENTER THE SIZE OF THE ARRAY:"); + + scanf("%d",&n); + +printf("ENTER THE ELEMENTS OF THE ARRAY:\n"); + + for(i=1;i<=n;i++) + { + scanf("%d",&array[i]); + +} + +/* Get the Search Key element for Linear Search */ + + printf("ENTER THE SEARCH KEY:"); + + scanf("%d",&search_key); + +/* Choice of Search Algorithm */ + + printf("___________________\n"); + + printf("1.LINEAR SEARCH\n"); + + printf("2.BINARY SEARCH\n"); + + printf("___________________\n"); + + printf("ENTER YOUR CHOICE:"); + + scanf("%d",&choice); + + switch(choice) + { + case 1: + linear_search(search_key,array,n); + break; + + case 2: binary_search(search_key,array,n); + break; + +default: + + exit(0); + +} + + return 0; + +} + +/* LINEAR SEARCH */ + +void linear_search(int search_key,int array[100],int n) + { + +/*Declare Variable */ + +int i,location; + +for(i=1;i<=n;i++) + { + + if(search_key == array[i]) + { + +location = i; + +printf("______________________________________\n"); + +printf("The location of Search Key = %d is %d\n",search_key,location); + +printf("______________________________________\n"); + +} + +} + +} + +/* Binary Search to find Search Key */ + +void binary_search(int search_key,int array[100],int n) +{ + + int mid,i,low,high; + + low = 1; + + high = n; + +mid = (low + high)/2; + +i=1; + +while(search_key != array[mid]) +{ + + if(search_key <= array[mid]) +{ + + low = 1; + +high = mid+1; + +mid = (low+high)/2; + + } + else + { + + low = mid+1; + high = n; + + mid = (low+high)/2; + } + +} + +printf("__________________________________\n"); + +printf("location=%d\t",mid); + +printf("Search_Key=%d Found!\n",search_key); + +printf("__________________________________\n"); + +} +``` +### 31) Program to implement bubble sort. +```C + /* Bubble sort implementation */ + +#include + +int main() +{ + int array[100], n, c, d, swap; + + printf("Enter number of elements\n"); + scanf("%d", &n); + + printf("Enter %d integers\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + for (c = 0 ; c < n - 1; c++) + { + for (d = 0 ; d < n - c - 1; d++) + { + if (array[d] > array[d+1]) /* For decreasing order use < */ + { + swap = array[d]; + array[d] = array[d+1]; + array[d+1] = swap; + } + } + } + + printf("Sorted list in ascending order:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} +``` +### 32) Program to store information of 10 students using array of structures. +```C +/* Structures for student */ + +#include +struct student +{ + char name[20],address[30]; + int grade,roll,dob; +}; + +int main() +{ + struct student s[10]; + int i; + for(i=0;i<10;i++) + { + printf("\nEnter records for student[%d]\n",i+1); + printf("Enter name: "); + gets(s[i].name); + printf("Enter address: "); + gets(s[i].address); + printf("Enter class, roll number and date of birth(year): "); + scanf("%d%d%d",&s[i].grade,&s[i].roll,&s[i].dob); + } + printf("Records of the 10 students are here"); + for(i=0;i<10;i++) + { + printf("\nStudent %d",i+1); + printf("\nName: %s",s[i].name); + printf("\nAddress: %s",s[i].address); + printf("\nClass: %d",s[i].grade); + printf("\nRoll No.: %d",s[i].roll); + printf("\nDOB: %d",s[i].dob); + printf("\n"); + } + return 0; +} +``` +### 33) Programs to compute the transpose of a matrix. +```C +include +int main() +{ + int a[10][10], transpose[10][10], r, c, i, j; + printf("Enter rows and columns of matrix: "); + scanf("%d %d", &r, &c); + // Storing elements of the matrix + printf("\nEnter elements of matrix:\n"); + for(i=0; i +int main() { + int a; + int *pt; + + a = 10; + pt = &a; + + printf("\n[&a ]:Address of A = %p", &a); + + + return 0; +} + +``` +### 35) Program to access array using pointer. +```C +#include +int main() +{ + int data[5],i; + printf("Enter elements: "); + for(i = 0; i < 5; ++i) + scanf("%d", data + i); + printf("You entered: \n"); + for(i = 0; i < 5; ++i) + printf("%d\n", *(data + i)); + return 0; +} + + + + + + +