0% found this document useful (0 votes)
97 views43 pages

Assignment of C Language: Submitted by NAME: Manish Kumar Class: Cse CSF ROLL NO.: 190111006

The document contains the details of 21 C programming assignments submitted by a student named Manish Kumar of class CSE CSF with roll number 190111006. Each assignment contains the problem statement, sample code with input/output and description of the program. The programs include calculating area of triangle, finding sum of digits, grade calculation based on marks, pattern printing, matrix addition, decimal to binary conversion, string manipulation, palindrome checking, prime factorisation and file handling operations.

Uploaded by

Kunal Ranjan
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)
97 views43 pages

Assignment of C Language: Submitted by NAME: Manish Kumar Class: Cse CSF ROLL NO.: 190111006

The document contains the details of 21 C programming assignments submitted by a student named Manish Kumar of class CSE CSF with roll number 190111006. Each assignment contains the problem statement, sample code with input/output and description of the program. The programs include calculating area of triangle, finding sum of digits, grade calculation based on marks, pattern printing, matrix addition, decimal to binary conversion, string manipulation, palindrome checking, prime factorisation and file handling operations.

Uploaded by

Kunal Ranjan
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/ 43

ASSIGNMENT

Of
C language
SUBMITTED BY
NAME : Manish Kumar
CLASS : CSE CSF
ROLL NO. : 190111006

1
Program 1: WAP to calculate area of a triangle. ( area =
base*height/2) .
#include<stdio.h>
{ void main() int
b,h, area;
scanf(“%d”,&b);
printf(“Enter base :”);
scanf(“%d”,&h); area=((b*h)/2); printf(“Enter
height :”);
} printf(“Area=%d”,area);
Output:

2
Program 2: Write a program to print sum of all the digits of
number.

#include<stdio.h>
void main()
{
int i,n,sum=0; printf(“Enter
Number :”);
scanf(“%d”,&n);
while(n>0)
{
i=n%10;
sum=sum+i;
n=n/10;
}
printf(“\nSum=%d”,sum);

}
Output:

3
Program 4: To take input of 5 subject marks & display the
grade according to the below table:
Marks Average Grade
>=90 A+
>=80 & <90 A
>=70 & <80 B+
>=60 & <70 B
>=50 & <60 C
<50 F

#include <stdio.h>
void main()
{
int s1,s2,s3,s4,s5;
float avg=0.0;
printf("Enter marks of 5 subjects\n");
scanf("%d\n%d\n%d\n%d\n%d\n",&s1,&s2,&s3,&s4,&s5);
avg=(s1+s2+s3+s4+s5)/5;
if(avg>=90.0)
{
printf("A+");

4
}
else if(avg>=80.0 && avg<90.0)
{
printf("A");
}
else if(avg>=70.0 && avg<80.0)
{
printf("B+");
}
else if(avg>=60.0 && avg<70.0)
{
printf("B");
}
else if(avg>=50.0 && avg<60.0)
{
printf("C");
}
else
{
printf("F");
}

5
}
Output:

Program 6: WAP to print the following pattern 1 3 7 13 21...


till 100

#include<stdio.h>
void main()
{ int i,j;
i=1;j=1;
while(i<=100)
{
printf(“%d”,i); i=i+2*j;
j++
}

6
}
Output:

Program 7:To print the following patterns.


A)*
**
***
****
*****

#include<stdio.h>
void main()

7
{
int i,j,k;
for(i=1;i<=5;i++)
{

for(j=5;j>=i;j--)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
}

Output:

B) 1
1 2
1 2 3
1 2 3 4

8
1 2 3 4 5

#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d",j);
}
printf("\n");
}
}
Output:

C)
54321
5432
543
54

9
5
#include<stdio.h>
Void main()
{
int i ,j;
for (i=1; i<=5; ++i)
{
for (j=5; j>=i; --j)
{
printf("%d ",j);
}
printf("\n");
}

Output:

10
D)
A
AB
ABC
ABCD
ABCDE
#include<stdio.h>

int main()
{
int i,j,n;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",(char)(j+64));
}

printf("\n");
}
}

Output:

11
E)
*****
****
***
**
*
#include<stdio.h> void
main()
{ int j,i;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}

Output:

12
F)
ABCDE
ABCD
ABC
AB
A
#include<stdio.h>

int main()
{ int
i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i+1;j++)
{
printf("%c",(char)(j+64));
}
printf("\n");
}
}

13
Output:

G)
*
***
*****
*******
*********
#include<stdio.h>
Void main()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");

14
}
}

Output:

15
Program 9 : WAP to search an element from an array
#include<stdio.h>
void main()
{
int a[100]; int n,i,j,s,x; int
c=0;
printf(“Enter array size:”);
scanf(“%d”,&n); printf(“Enter
array:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter element to be searched:”);
scanf(“%d”,&s);
for(j=0;j<n;j++)
{
if(s==a[j])

16
{ c++;
x=j;
}
}
if(c==1)
{
printf(“Element %d found at %d index”,s,x);
}
else if(c>1)
{
printf(“Element %d found”,s);
printf(“1st index %d\n Element found more than ONCE ”,j);
}
else
{
printf(“Element %d not found”,s);
}
}

17
Output:

Program 10:WAP to add two matrices


#include <stdio.h>
void main()
{

18
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows : "); scanf("%d",
&r);
printf("Enter the number of columns : ");
scanf("%d", &c);
printf("\nEnter elements of 1st 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]);
}
printf("Enter elements of 2nd 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", &b[i][j]);
}

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


for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");

19
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
if (j == c - 1)
{
printf("\n\n");
}
}

}
Output:

20
Program 11: WAP to convert a decimal number into binary
form
#include<stdio.h>
void main()
{
long a[100]; long n,i,j;
long c=0; printf(“Enter
number:”);
scanf(“%ld”,&n);
while(n>0)
{
j=n%2; n=n/2; a[c]=j; c++; } for(i=(c-1);i>=o;i--)
{
printf(“%ld\t”,a[i]);
}
}

Output:

21
Program 13: WAP to convert a string into upper case
#include<stdio.h>
#include<string.h> #include<ctype.h>
void main()
{
char a[100]; int i; printf("Enter
string:"); scanf("%s",&a);
for(i=0;i<strlen(a);i++)
{
a[i]=toupper(a[i]);
}
printf("Upper case: %s",a);
}

22
Output:

Program 14: WAP to find the occurrence of a character in a


string
#include<stdio.h>
#include<string.h>

void main()
{
char a[100]; int c[26]={0};
int i,index; printf("Enter
sentence:");
scanf("%s",&a);
for(i=0;i<strlen(a);i++)

23
{
index=a[i]-97;
c[index]= c[index]+1;
}
for(i=0;i<26;i++)
{
if(c[i]>=1)
{
printf("\n%c = %d",(i+97),c[i]);
}
}
}

Output:

24
Program 15:WAP to check whether a string is palindrome or
not.

#include<stdio.h> #include<string.h>
void main()
{

25
char a[100]; char b[100];
printf(“Enter sentence:”);
scanf(“%s”,&a); strcpy(b,a);
strrev(b); if(strcmp(a,b)==0)
{
printf(“string is PALINDROME\n”);
}
else {
printf(“string is not PALINDROME\n”);
}
}

26
Output:

Program 17: WAP to print prime factor of a given number using


function. [Ex : 100 = 2 2 5 5].
#include <stdio.h> void
Find_Prime(int Number)
{
int i, Count = 0;

for (i = 2; i <= Number/2; i++)


{
if(Number%i == 0)
{
Count++;

27
}
}
if(Count == 0 && Number != 1 )
{
printf("\n %d is a factor", Number);
}
}
void Find_Factors(int Number)
{
int i;

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


{
if(Number % i == 0)
{
Find_Prime(i);
}
}
}
void main()
{
int i, j, Number, count;

printf("\n Enter any number to Find it's Prime Factors : ");


scanf("%d", &Number);

28
printf("\n Prime Factors of a Given Number are : \n");
Find_Factors(Number);

Output:

Program 18: WAP to find sum of all elements of an array using


pointer. #include<stdio.h>
Void main()
{
int numArray[10];
int i, sum = 0;
int *ptr;

29
printf("\nEnter array elements : ");
for (i = 0; i < 10; i++)
{
scanf("%d", &numArray[i]);
}
ptr = numArray;
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
Output:

30
Program 19: WAP to store student information (roll no, name,
marks) and display using structure.
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[10];

31
Void main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: "); scanf("%s",
s[i].firstName); printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName); printf("Marks:
%.1f", s[i].marks);
printf("\n");
}
}

32
Output:

33
Program 20 : WAP to copy one file content(abc.txt) into
another file (test.txt). #include<stdio.h>
#include<stdlib.h> int main()
{
FILE *fp1, *fp2;
char ch;

fp1 = fopen("abc.txt", "r");


fp2 = fopen("test.txt", "w");

while (1) {
ch = fgetc(fp1);

if (ch == EOF)
break; else
putc(ch, fp2);
}

printf("File copied Successfully!");


fclose(fp1);
fclose(fp2);
}

Output:

34
Program 21: WAP to count number of lines, words and
characters in a file. #include <stdio.h> #include
<stdlib.h> int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
printf("Enter source file path: ");
scanf("%s", path); file =
fopen(path, "r");
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read
privilege.\n");

exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)

35
{
characters++; if (ch
== '\n' || ch == '\0')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if (characters > 0)
{
words++;
lines++;
}
printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words); printf("Total
lines = %d\n", lines);
fclose(file);
return 0;
}

36
Output:

Program 23: Write a menu driven program to perform


following using functions.
1. Sort Array Elements using Bubble Sort
2. Sort Array Elements using Selection Sort
3. Sort Array Elements using Insertion Sort
4. Exit
#include<stdio.h>
int main()
{

37
int ch,arr[50],i,temp,j;
printf("Enter array elements\n");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
printf(" Menu Driven Sorting Program\n"); printf("
Enter 1 to perform Bubble Sort\n Enter 2 to perform
Selection Sort\n Enter 3 to perform Insertion Sort\n Enter
your choice : "); scanf("%d",&ch);
switch(ch)
{ case
1: int i,
j;
for (i = 0; i < 10-1; i++)
for (j = 0; j < 10-i-1; j++)
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j]= arr[j+1];
arr[j+1]= temp;
}
printf(" Sorted array : ");
for(i=0; i < 10; i++)
{

38
printf("%d", arr[i]);
printf("\t"); }
break;
case 2: int
min_idx;
for (i = 0; i < 10-1; i++)
{
min_idx = i;
for (j = i+1; j < 10; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;

temp = min_idx;
min_idx = i;
i = temp;
}

}
}
printf("Sorted Array : ");
for (i=0; i < 10; i++)
{
printf("%d ", arr[i]);
printf("\t");

39
}
break; case
3: int key;
for (i = 1; i < 10; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
printf("Sorted Array : ");
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
printf("\t");
}
break;

}
default:
printf("Wrong Choice");
}
}

40
Output:

Program 24: WAP read a string value from user and remove
the spaces between two words of its content and display the

41
resultant string. #include <string.h> #include<stdio.h> int
main()
{
char s[1000];
int i,k=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
s[i]=s[i+k];
if(s[i]==' '|| s[i]=='\t')
{
k++;
i--;
}
}
printf("Resultant string : %s", s);
return 0;
}

Output:

42
43

You might also like