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

Important Programs

principles of programming using c important programs

Uploaded by

shreyasshreshta0
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)
19 views

Important Programs

principles of programming using c important programs

Uploaded by

shreyasshreshta0
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/ 20

IMPORTANT PROGRAMS

Q 1: WAP TO FIND LARGEST ELEMENT FROM AN ARRAY

#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10], i, large;
printf("Enter 10 Array Elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
i=0;
large = arr[i];
while(i<10)
{
if(large<arr[i])
large = arr[i];
i++;
}
printf("\nLargest Number = %d", large);
getch();
return 0;
}

Q2. Wap addition of two matrix.


#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

NOTE: SUBTRACTION OF TWO MATRICES CHANGE THE PLUS SIGN INTO MINUS.

Q2. Wap multiplication of two matrix.

#include<stdio.h>
int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;

// Multiplying i’th row with j’th column


for(int k=0;k<c1;k++)
{
mul[i][j]= mul[i][j]+m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
return 0;
}

Q: WAP TO SORT AN ARRAY USING BUBBLE SORT.

#include <stdio.h>
/*
* Main Function
*/
int main()
{
int n, j, i, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
int array[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 (j = 0 ; j < n - i- 1; j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] =temp;
}
}
}

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

for (i = 0; i < n; i++)


printf("%d\n", array[i]);
return 0;
}

Q: WAP TO SEARCH AN ELEMENT USING BINARY SEARCH

#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

FUNCTIONS PROGRAM:

Q1. Write a c-program using functions to generate the Fibonacci series.

#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);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printe
d
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

Q2: Write a program in C to find the factorial of a given integer using


function.

#include<stdio.h>
#include<math.h>
int main()
{
printf("Enter a Number to Find Factorial: ");
printf("\nFactorial of a Given Number is: %d ",fact());
return 0;
}
int fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}

Q3: WAP TO PRINT SUM OF INTEGERS USING RECURSIVE


FUNCTIONS.

#include <stdio.h>

int add(int n);

int main() {

int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", add(num));
return 0;
}

int add(int n) {
if (n != 0)
return n + add(n - 1);
else
return n;
}

Q4: WAP TO SWAPPING TWO NUMBERS USING CALL BY VALUE


AND CALL BY REFERENCE.
ANS: CALL BY VALUE:
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // prin
ting the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The valu
e of actual parameters do not change by changing the formal parameters in c
all by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Form
al parameters, a = 20, b = 10
}

Output

Before swapping the values in main a = 10, b = 20


After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

CALL BY REFERENCE
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // prin
ting the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The valu
es of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // For
mal parameters, a = 20, b = 10
}

Output

Before swapping the values in main a = 10, b = 20


After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

Q: Write a c-program using function to check whether the given number is


prime or not.
#include <stdio.h>
// function to check if the num is prime or not.
int find_Prime(int num)
{

int i, temp = 0;
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
temp++;
}
}
return temp;
}
int main()

int num, temp = 0;


printf("Enter any number to Check for Prime: ");
scanf("%d", &num);
temp = find_Prime(num);
if (temp == 0 && num != 1)
{
printf("\n %d is a Prime Number", num);
}
else
{
printf("\n %d is Not a Prime Number", num);
}
return 0;

STRINGS PROGRAMS:

Q1: Develop a C program to concatenate 2 strings without using built-in


function.

#include<stdio.h>

int main()
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
return 0;
}

Q2: Develop a C program to compare 2 strings without using built-in


function.

#include<stdio.h>

int main()
{
char str1[20], str2[20];
int i=0, c=0;

printf("\nEnter first string :: ");


gets(str1);
printf("\nEnter second string :: ");
gets(str2);

printf("\nStrings are :: \n\n");


puts(str1);
puts(str2);

while((str1[i]!='\0') || (str2[i]!='\0'))
{
if(str1[i]!=str2[i])
c++;
i++;
}

if(c==0)
puts("\nStrings are equal.\n");
else
puts("\nStrings are not equal.\n");

return 0;
}

Q3: Develop a C program reverse the string without using built-in function.

Refer my notes
Q4: Develop a C program copy the one string to another string without using
built-in function.
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf(“%s”, s1);
for (i = 0; s1[i] != '\0'; i++) {
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Q5: Write a program in C to check whether a given string is PALINDROME
or NOT without using string handling function.

#include<stdio.h>

int main()
{
char string[40];
int length=0, flag=1,i;

printf("Enter string:\n");
gets(string);

for(i=0;string[i]!='\0';i++)
{
length++;
}

for(i=0;i< length/2;i++)
{
if( string[i] != string[length-1-i] )
{
flag=0;
break;
}
}
if(flag==1)
{
printf("PALINDROME");
}
else
{
printf("NOT PALINDROME");
}
return 0;
}

Q: Explain the difference between gets and scanf with example.

// C program to see how scanf()


// stops reading input after whitespaces

#include <stdio.h>
int main()
{
char str[20];
printf("enter something\n");
scanf("%s", str);
printf("you entered: %s\n", str);

return 0;
}
Here the input will be provided by the user and output will be as
follows:
Input: COMPUTER PROGRAMMING
Output: COMPUTER

// C program to show how gets()


// takes whitespace as a string.

#include <stdio.h>
int main()
{
char str[20];
printf("enter something\n");
gets(str);
printf("you entered : %s\n", str);
return 0;
}

Here the input will be provided by the user and output will be as
follows:
Input: COMPUTER PROGRAMMING
Output: COMPUTER PROGRAMMING

POINTERS PROGRAM:

Q:Develop a program using pointer to compute the sum, mean and standard
deviation of all element stored in array of N real number
#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("Enter the no of elements n =");
scanf("%d",&n);
printf(" Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+ pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation=%.3f\n",std);
}

Q 2: What is a pointer? Explain how the pointer variable declared and


initialized.

REFER NOTES

Q 3 : Develop a C program to find the largest of three numbers using pointer.

#include <stdio.h>
int main()
{
int num1, num2, num3;
int *p1, *p2, *p3;

//taking input from user


printf("Enter First Number: ");
scanf("%d",&num1);
printf("Enter Second Number: ");
scanf("%d",&num2);
printf("Enter Third Number: ");
scanf("%d",&num3);

//assigning the address of input numbers to pointers


p1 = &num1;
p2 = &num2;
p3 = &num3;
if(*p1 > *p2)
{
if(*p1 > *p3)
{
printf("%d is the largest number", *p1);
}
else
{
printf("%d is the largest number", *p3);
}
}
else
{
if(*p2 > *p3)
{
printf("%d is the largest number", *p2);
}
else
{
printf("%d is the largest number", *p3);
}
}
return 0;
}

STRUCTURES PROGRAM:

Q: Define a structure by the name DoB consisting of three variable members


dd, mm, and yy of type integer. Develop a C program that would read values
to the individual member and display the date in mm/dd/yy form.

#include <stdio.h>
struct student{
char name[30];
int rollNo;

struct dateOfBirth{
int dd;
int mm;
int yy;
}DO O B; /*created structure variable DOB*/
};
/* Scroll down for full code
*/
int main()
{
struct student std;
printf("Enter name: ");
scanf("%c", &std.name);
printf("Enter roll number: ");
scanf("%d", &std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d %d %d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\n",
std.name, std.rollNo, std.DOB.dd, std.DOB.mm, std.DOB.yy);

return 0;
}

Q2: Implement structures to read, write, compute average- marks and the
students scoring above and below the average marks for a class of N
students.

#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
}

FILES PROGRAM:

Q: WAP TO COPY THE CONTENT OF ONE FILE INTO ANOTHER


FILE.

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

int main()
{
FILE *sourceFile;
FILE *destFile;
char sourcePath[100];
char destPath[100];

char ch;

/* Input path of files to copy */


printf("Enter source file path: ");
scanf("%s", sourcePath);
printf("Enter destination file path: ");
scanf("%s", destPath);

/*
* Open source file in 'r' and
* destination file in 'w' mode
*/
sourceFile = fopen(sourcePath, "r");
destFile = fopen(destPath, "w");
/* fopen() return NULL if unable to open file in given mode. */
if (sourceFile == NULL || destFile == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read/write privilege.\n");

exit(EXIT_FAILURE);
}

/*
* Copy file contents character by character.
*/
ch = fgetc(sourceFile);
while (ch != EOF)
{
/* Write to destination file */
fputc(ch, destFile);

/* Read next character from source file */


ch = fgetc(sourceFile);
}

printf("\nFiles copied successfully.\n");

/* Finally close files to release resources */


fclose(sourceFile);
fclose(destFile);

return 0;
}

Q2: WAP TO OPEN A FILE AND PERFORM READ WRITE


OPERATION IN A FILE.

#include<stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("hello.txt", "r");
while( (ch = getc(fp)! = EOF)
printf("%c",ch);
fclose(fp);
return 0;
}

You might also like