NAME-JAPNEET SINGH BRANCH-CSE-C1 ROLL NO.-1915323
#include<stdio.h>
int main()
{
puts("My Name Is Japneet Singh");
return 0;
}
#include<stdio.h>
int main()
{
printf("Guru Nanak Dev Engineering College,\n");
printf("Gill Road,\n");
printf("Ludhiana,Punjab.\n");
return 0;
}
#include <stdio.h>
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;
}
#include <stdio.h>
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;
}
#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=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
#include <stdio.h>
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;
}
#include<stdio.h>
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;
}
#include <stdio.h>
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");
}
#include<stdio.h>
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+");
}
}
#include <stdio.h>
#include <math.h>
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;
}
#include <stdio.h>
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;
}
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;
}
# include <stdio.h>
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;
}
#include <stdio.h>
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;
}
#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.", originalInteger);
return 0;
}
#include <stdio.h>
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);
}
}
#include <stdio.h>
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;
}
#include<stdio.h>
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;
}
#include<stdio.h>
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;
}
#include<stdio.h>
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;
}
#include<stdio.h>
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;
}
#include<stdio.h>
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;
}
#include<stdio.h>
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;
}
#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;
}
#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;
}
#include <stdio.h>
#include <string.h>
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;
}
25- Program To Perform Basic Operations Like Length Of String, String Concat,, String copy, String Compare and String Reverse
#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';
}
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;
}
#include<stdio.h>
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);
}
#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;
}
#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;
}
#include <stdio.h>
#include <stdlib.h>
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);
}
}
#include<stdio.h>
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));
}
#include<stdio.h>
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<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
#include<stdio.h>
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;
}
#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)
{
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;
}
#include <stdio.h>
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;
}
#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;
}
#include <stdio.h>
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;
}
#include <stdio.h>
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;
}
#include <stdio.h>
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;
}
#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;
}









































