Skip to content

Latest commit

 

History

History
1085 lines (875 loc) · 21.5 KB

File metadata and controls

1085 lines (875 loc) · 21.5 KB

#PPS ##personal details Name-Hardev

CRN-1915316

URN-1905790

Branch-CSE-C1

###1.To print name using puts

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

puts("~~~~~~~~~~"); puts(" Hardev "); puts("~~~~~~~~~~"); return 0; }' output

###2.to print 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 '#include<stdio.h> 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

###4.program to compute 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 using third 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 whether a number is even or odd '#include<stdio.h> 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

###7.program to find greatest of two numbers '#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.program to find greatest of three numbers '#include<stdio.h> 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

###9.program to assign grade to student according to percentage '#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

###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

###11.program to check year is leap or not '#include<stdio.h> 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

###12.program to find 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.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

###14.program to clculate 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.program to check whether a number is palindrome or not '#include<stdio.h> 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

###16.program to check whether a number is prime or not '#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

###17.program to print prime numbers from 1 to 100 '#include <stdio.h>

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

###18.program to check whether a number is armstrong or not '#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

###19.program to print different patterns ####A. '#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 ####B. '#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 ####C. '#include <stdio.h> int main() { int rows, coef = 1, space, i, j; printf("Enter number of rows: "); scanf("%d",&rows); for(i=0; i<rows; i++) { for(space=1; space <= rows-i; space++) printf(" "); for(j=0; j <= i; j++) {
if (j==0 || i==0) coef = 1; else coef = coef*(i-j+1)/j; printf("%4d", coef); } printf("\n"); } return 0; }' output

###20.program to find largest element from one 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.program to find the sum of N natural numbers in an 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 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

###22.program to add two matrices '#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 ' { 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.progarm 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.program to perform basic operations like length of string,string concat,string copy,string compare and string reverse ' 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

###26.programs to swap two numbers using call by value and call by reference ####A.call be 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 ####B.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; printf("Values of a and b is %d %d\n",a,b); }' output

###27.program to find factorial of a number with or without recursion ####A.with 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 ####B.without recursion '#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 write fibonacci series with or withou recursion ####A.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 ####B.without recursion '#include<stdio.h> 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<n;i++) {
if(i<=1) next=i; else {
next=first+second; first=second; second=next; }
printf("%d",next\n); } return 0; } ' output

###29.program to calcuate average of two numbers five numbers using function '#include<stdio.h> 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

###30.program to implement linear and binary search ####A.linear '#include <stdio.h>

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 ####B.binary '#include<stdio.h>

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

###31.program to implement bubble sort '#include<stdio.h>

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

###32.program to store iformation of 10 students using array of structures ' 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

###33.program to comopute 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 pointers '#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 elements usin 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 -------------------------------------------