0% found this document useful (0 votes)
22 views

PROGRAM ALL-1 (2) Merged

The document contains 43 C programming code examples submitted by a student named Rishika Jalan with an enrollment number of 230254. Each example includes the code to solve a programming problem, and outputs the result. The examples cover topics like patterns, loops, functions, strings, matrices and more.

Uploaded by

riya sirohi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

PROGRAM ALL-1 (2) Merged

The document contains 43 C programming code examples submitted by a student named Rishika Jalan with an enrollment number of 230254. Each example includes the code to solve a programming problem, and outputs the result. The examples cover topics like patterns, loops, functions, strings, matrices and more.

Uploaded by

riya sirohi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

/*****************************************************

Program#24:write a program to print a pattern


Student Name:Rishika jalan
Enrollment No.:230254
*****************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j;
printf("enter the value of n=");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf( " * ");
}
printf("\n");
}
getch();
}
OUTPUT:
/*********************************************************
Program#25:write a program to print a pattern
Student Name:Rishika jalan
Enrollment No.:230254
***********************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j;
printf("enter the value of n=");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf(" %d ",j);
}
printf("\n");
}
getch();
}
OUTPUT:
/**********************************************************************************
Program#26:write a program to print number 1to10 using do while loop
Student Name:Rishika jalan
Enrollment No.:230254
*********************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1;
printf("enter the value of n=");
scanf("%d",&n);
do{
printf("%d\n",i);
i++;
}while(i<=n);
getch();
}
OUTPUT:
/***********************************************************************
Program#27:write a program to print a table using do while loop
Student Name:Rishika jalan
Enrollment No.:230254
***********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1,s;
printf("enter the value of n=");
scanf("%d",&n);
do{
s=n*i;
printf("%d*%d=%d\n",n,i,s);
i++;
}while(i<=10);
getch();
}
OUTPUT:
/**************************************************************************
Program#28:write a program to print a table using do while loop
Student Name:Rishika jalan
Enrollment No.:230254
**************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1,s;
printf("enter the value of n=");
scanf("%d",&n);
do{
s=n*i;
printf("%d*%d=%d\n",n,i,s);
i++;
}while(i<1);
getch();
}

OUTPUT:
/*******************************************************************
Program#29:write a program to print a table using while loop
Student Name:Rishika jalan
Enrollment No.:230254
*******************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1,s;
printf("enter the value of n=");
scanf("%d",&n);
while(i<=10){
s=n*i;
printf("%d*%d=%d\n",n,i,s);
i++;
}
getch();
}
OUTPUT:
/*****************************************************************************
Program#30: Wite a program to find a factorial using function
Student Name : Rishika Jalan
Enrollment no.:230254
*****************************************************************************/
#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
void main()
{
clrscr();
int n,result;
printf("enter the value of n=");
scanf("%d",&n);
result=factorial(n);
printf("factorial=%d",result);
getch();
}
OUTPUT:
/******************************************************
Program#31: Write a program to find factorial
Student Name:Rishika Jalan

Enrollment No.230254
*******************************************************/
#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
int f=1;
for(int i=1;i<=n;i++){
f=f*i;
}

return f;
}
void main()
{
clrscr();

int n,result;
printf("enter the number");
scanf("%d",&n);
result=factorial(n);
printf("factorial =%d",result);
getch();

OUTPUT:
/***********************************************************************
Program#32: Write a program to find palindrome
Student Name:Rishika Jalan
Enrollment No.:230254
************************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,rev=0,a,p;
printf("Enter the value of p=");
scanf("%d",&p);
n=p;
while(n>0)
{
a=n%10;
rev=rev*10+a;
n=n/10;
}
if(rev==p)
{
printf("palindrome");
}
else
{
printf("not a palindrome");
}
getch();
}
OUTPUT:
/********************************************************************
Program #33: write a program to print a string
Student name: Rishika Jalan
Enrollment No. :2330254
*********************************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[]="mody";
int i=0;
while(name[i]!='\0')
{
printf("%c",name[i]);
i++;
printf("\n");
}
getch();
}

OUTPUT:
/***********************************************************************************
Program#34: write a program to check whether a given string is
palindrome
Student Name: Rishika Jalan
Enrollment no.:230254
**********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[5]="good",str2[5];
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0){
printf("palindrome");
}
else{
printf("not a palindrome");
}
getch();
}

OUTPUT:
/***********************************************************************************
Program#35: Write a program to check whether a given string is
palindrome
Student Name: Rishika Jalan
Enrollment no.:230254
***********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[5],str2[5];
printf("enter the string");
scanf("%s",str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0){
printf("palindrome");
}
else{
printf("not a palindrome");
}
getch();
}

OUTPUT:
/**********************************************************
Program#36: write a program to add two strings
Student Name: Rishika Jalan
Enrollment no.:230254
**********************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[10],str2[10];
printf("enter the string");
scanf("%s %s",str1,str2);
strcat(str1,str2);
puts(str1);
fflush(stdout);
getch();
}

OUTPUT:
/*******************************************************
Program#37: write a program to print string
Student Name: Rishika Jalan
Enrollment no.:230254
*******************************************************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[]="INDIA";
for(int i=0;name[i]!='\0';i++)
{
printf("%c\n",name[i]);
}
getch();
}

OUTPUT:
/******************************************************************************
Program #38: WRITE A PROGRAM TO CHECK LCM OF TWO
NUMBERS.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
int
main ()
{
int n1, n2, max;
printf ("Enter two positive integers: ");
scanf ("%d %d", &n1, &n2);
max = (n1 > n2) ? n1 : n2;
while (1)
{
if ((max % n1 == 0) && (max % n2 == 0))
{
printf ("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
OUTPUT:
/******************************************************************************
Program#39: WRITE A PROGRAM TO FACTORIAL OF A NUMBER
USING FUNCTION
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/

#include<stdio.h>
int findFact(int);
int main(){
int x,fact,n;
printf("Enter a number to get factorial: ");
scanf("%d",&n);
fact = findFact(n);
printf("The factorial of %d is: %d",n,fact);
return 0;
}
int findFact(int n){
int x,fact=1;
for(x=1;x<=n;x++)
fact=fact*x;
return fact;
}

OUTPUT:
/******************************************************************************
Program#40: WRITE A PROGRAM TO GCD OF TWO NUMBERS
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/

#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}

OUTPUT:
/******************************************************************************
Program#41: WRITE A PROGRAM TO FIND PRIME FACTOR OF A
NUMBER.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
void primeFactors(int n)
{
while (n % 2 == 0) {
printf("%d ", 2);
n=n/2; }
for (int i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
printf("%d ", i);
n = n / i; }
}
if (n > 2)
printf("%d ", n);
}
int main()
{
int n = 315;
primeFactors(n);
return 0 ; }
OUTPUT:
/******************************************************************************
Program#42: WRITE A PROGRAM TO FIND MULTIPLICATION OF
TWO MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
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("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
/******************************************************************************
Program#43: WRITE A PROGRAM TO PRINT DIAGONAL OF MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include<stdio.h>
int main()
{
int array1[10][10],i,j,m,n,sum = 0;
printf("Enter no. of rows :: ");
scanf("%d", &m);
printf("\nEnter no. of columns :: ");
scanf("%d",&n);
printf("\nEnter values to the matrix :: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter a[%d][%d] value :: ",i,j);
scanf("%d", &array1[i][j]);
}
}
printf("\nThe Diagonals elements of a matrix are :: \n\n");
if(m==n)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
printf("\t%d", array1[i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
return 0;
}

OUTPUT:
/******************************************************************************
Program#44: WRITE A PROGRAM TO PRINT UPPER TRIANGULAR
OF MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
#include<conio.h>
int main()
{
int i, j, r, c, array[10][10];
printf("Enter the r and c value:");
scanf("%d%d", &r, &c);
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("array[%d][%d] = ", i, j);
scanf("%d", &array[i][j]);
}
}
printf("matrix is\n");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n");
}

for (i = 1; i <= r; i++)


{
printf("\n");
for (j = 1; j <= c; j++)
{
if (j >= i)
{
printf("%d\t", array[i][j]);
}
else
{
}
}
}
}
OUTPUT:
/******************************************************************************
Program#45: WRITE A PROGRAM TO PRINT LOWER TRIANGULAR
OF MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
#include<conio.h>
int main()
{
int i, j, r, c, array[10][10];
printf("Enter the r and c value:");
scanf("%d%d", &r, &c);
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("array[%d][%d] = ", i, j);
scanf("%d", &array[i][j]);
}
}
printf("matrix is\n");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n");
}
for (i = 1; i <= r; i++)
{
printf("\n");
for (j = 1; j <= c; j++)
{
if (i >= j)
{
printf("%d\t", array[i][j]);
}
else
{
printf("\t");
}
}
}
}
OUTPUT:
/******************************************************************************
Program#46: WRITE A PROGRAM TO PRINT TRANSPOSE OF A
MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

OUTPUT:
/******************************************************************************
Program#47: WRITE A PROGRAM TO PRINT SUM OF A MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include<stdio.h>
int main()
{
int row_size,col_size;
printf("Enter the row Size Of the Matrix:");
scanf("%d",&row_size);
printf("Enter the columns Size Of the Matrix:");
scanf("%d",&col_size);
int matrix[row_size][col_size];
int i,j;
printf("Enter the Matrix Element:\n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
scanf("%d",&matrix[i][j]);
}
}
int sum=0;
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
sum+=matrix[i][j];
}
}
printf("Sum of the Given Matrix Elements is: %d",sum);
return 0;
}

OUTPUT:
/******************************************************************************
Program #48: WRITE A PROGRAM TO FIND THE SMALLEST
DIVISOR OF A NUMBER.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
int findSmallestDivisor(int n) {
int i;
for (i = 2; i <= n; i++) {
if (n % i == 0) {
return i;
}
}
return -1;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

int smallestDivisor = findSmallestDivisor(number);


if (smallestDivisor != -1) {
printf("The smallest divisor of %d is: %d\n", number,
smallestDivisor);
}
else {
printf("The number %d is a prime number.\n", number);
}
return 0;
}

OUTPUT:
/******************************************************************************
Program #49: WRITE A PROGRAM TO DISPLAY 3D MATRIX.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include <stdio.h>
void display3DMatrix(int matrix[3][3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
printf("%d ", matrix[i][j][k]);
}
printf("\n"); // Move to the next row after displaying one 2D matrix
}
printf("\n"); // Add a blank line between 2D matrices
}
}

int main() {
int matrix[3][3][3] = {
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}},
{{19, 20, 21}, {22, 23, 24}, {25, 26, 27}}
};
printf("3D Matrix:\n");
display3DMatrix(matrix);
return 0;
}
OUTPUT:
/******************************************************************************
Program #50: WRITE A PROGRAM TO FIND FACTORIAL OF A
NUMBER USING RECURSION.
Student Name: Rishika Jalan
Enrollment No.:230254
*******************************************************************************/
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
int main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
OUTPUT:
/**********************************************************************************
Program#51: write a program to compare the two strings
Student Name: Rishika Jalan
Enrollment no.:230254
**********************************************************************************/
#include<stdio.h>
#include<string.h>
int main(){
int result;
char str1[]="Jerry",str2[]="Merry";
result=strcmp(str1,str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is smaller than the second string.\n");
} else {
printf("The first string is larger than the second string.\n");
}
return 0;
}

OUTPUT:

You might also like