Programming for Problem Solving (R22), Unit-II Programs
(I [Link], CSE)
1. Write a C Program to implement Single Dimension Array. Use static
initialization. Assign the values in separate statement.
#include<stdio.h>
int main()
{
int i;
int marks[5];//declaration of array
//initialization of array
marks[0]=80;
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output:
GSK 1
2. Write a C Program to implement Single Dimension Array. Use static
initialization. Assign the values in a single statement.
#include<stdio.h>
int main()
{
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
Output:
GSK 2
3. Write a C Program to implement Single Dimension Array. Use dynamic
initialization.
// Program to take 5 values
// from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main()
{
int values[5];
int i;
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(i = 0; i < 5; i++)
{
scanf("%d", &values[i]);
}
printf("Array values are: ");
// printing elements of an array
for(i = 0; i < 5; i++)
{
printf("%d\n", values[i]);
}
return 0;
}
Output:
GSK 3
4. Write a C Program to read and print the values of two-dimensional array.
Use static initialization.
#include<stdio.h>
int main()
{
int i,j;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}
Output:
GSK 4
5. C program to find the sum of two matrices of order 2*2
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
int i,j;
// Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
printf("Enter a%d%d: ", i, j );
scanf("%f", &a[i][j]);
}
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i, j);
scanf("%f", &b[i][j]);
}
}
// adding corresponding elements of two arrays
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}
}
// Displaying the sum
printf("\nSum Of Matrix:");
for (i = 0; i < 2; ++i)
GSK 5
{
for (j = 0; j < 2; ++j)
{
printf("%f\t", result[i][j]);
if (j == 1)
printf("\n");
}
}
return 0;
}
Output:
GSK 6
6. C program to implement matrix multiplication
#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++)
GSK 7
{
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 printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
GSK 8
Output:
GSK 9
7. C Program to read a string value using scanf() function
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:
8. C Program to read a string using gets()
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); // read string
puts(name); // display string
return 0;
}
Output:
GSK 10
9. C Program to implement strlen()
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
printf("Length of string a = %d \n",strlen(a));
printf("Length of string b = %d \n",strlen(b));
return 0;
}
Output:
GSK 11
10. C Program to implement strcpy()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result; // comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
Output:
GSK 12
11. C program to implement to strcat()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "This is ", str2[] ="JNTUHUCER";
// concatenates str1 and str2 the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output:
GSK 13
12. C program to implement strcpy()
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "C programming";
char str2[20]; // copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
Output:
GSK 14
13. C program to implement strstr()
#include <stdio.h>
#include <string.h>
int main ()
{
char string[55] ="This is a test string for testing";
char *p;
p = strstr (string,"test");
if(p)
{
printf("string found\n" );
printf ("First occurrence of string test is %s",p);
}
else
printf("string not found\n" );
return 0;
}
Output:
GSK 15
14. C program to implement array of strings
#include<stdio.h>
int main()
{
int i;
char Array[3][6] = {"Black", "Blame", "Block"};
printf("String Array: \n");
for(i = 0; i < 3; i++)
{
printf("%s\n", Array[i]);
}
return 0;
}
Output:
GSK 16
15. C Program to implement structures
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
/*
s1 is a variable of Student type and
age is a member of Student
*/
[Link] = 22;
//using string function to add name
strcpy([Link], "Shiva");
strcpy([Link], "CSE");
[Link]='M';
//displaying the stored values
GSK 17
printf("Name of Student 1: %s\n", [Link]);
printf("Age of Student 1: %d\n", [Link]);
printf("Branch of Student 1: %s\n", [Link]);
printf("Gender of Student 1: %c\n", [Link]);
return 0;
}
Output:
GSK 18
16. C Program to implement structure with two structure variables
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
[Link]=101;
strcpy([Link], "Sonoo Jaiswal");//copying string into char array
[Link]=56000;
//store second employee information
[Link]=102;
strcpy([Link], "James Bond");
[Link]=126000;
//printing first employee information
printf( "employee 1 id : %d\n", [Link]);
printf( "employee 1 name : %s\n", [Link]);
printf( "employee 1 salary : %f\n", [Link]);
//printing second employee information
printf( "employee 2 id : %d\n", [Link]);
printf( "employee 2 name : %s\n", [Link]);
printf( "employee 2 salary : %f\n", [Link]);
GSK 19
return 0;
}
Output:
GSK 20
17. C Program to implement Array of structures
#include<stdio.h>
#include <string.h>
struct student
{
int rollno;
char name[10];
};
int main()
{
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
GSK 21
Output:
GSK 22
18. C program to implement unions
#include <stdio.h>
union Job
{
float salary;
int workerNo;
}j;
int main()
{
// when [Link] is assigned a value,
// [Link] will no longer hold 12.3
[Link] = 100;
printf("Number of worker = %d", [Link]);
[Link] = 12.3;
printf("Salary = %f\n", [Link]);
return 0;
}
Output:
GSK 23
19. C Program to understand the difference between union and structure
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output:
GSK 24
20.C program to implement pointers
#include<stdio.h>
int main()
{
int number=50;
int *p;
p=&number;//stores the address of number variable
// p contains the address of the number
//therefore printing p gives the address of number.
printf("Address of p variable is %p \n",p);
/*As we know that * is used to dereference a pointer
therefore if we print *p,
we will get the value stored at the address contained by p. */
printf("Value of p variable is %d \n",*p);
return 0;
}
Output:
GSK 25
21.C program to implement pointers to arrays
#include<stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%d\n", ptr);
printf("%d\n", &arr[0]);
return 0;
}
Output:
GSK 26
22. C program to implement pointer to structure
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Output:
GSK 27
23.C program to implement enum data types
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
Output:
GSK 28