Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Programming: Assignment: 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

COMPUTER PROGRAMMING

ASSIGNMENT: 2

IIT(ISM) DHANBAD

NAME: PRANAY DUTT DUBEY


ADMISSION NO: 20JE0703
SEC: E

1. Write a program that calculates the sum of squares of the


elements in an array.
Input: array is 1 3 5 2
Output: Sum of squares is 39
CODE:
#include<stdio.h>
int main()
{
int size, sum=0, a[size];
printf("Enter array size:");
scanf("%d",&size);
printf("Enter array elements:");
for(int i=0;i<size;i++)
{
scanf("%d",&a[i]);
sum=sum+(a[i]*a[i]);
}
printf("Array is ");
for(int i=0;i<size;i++)
printf("%d ",a[i]);
printf("\nSum of squares is %d\n",sum);
}

2. Write a program to merge two integer arrays. Also display


array in reverse order.
Input: First array is 1 3 6 44
Second array is 88 54 98 12
Output: Array in reverse order is 12 98 54 88 44 6 3 1

CODE:
#include <stdio.h>
int main()
{
int size1 ,size2 ,index=size1+size2-1 , arr1[size1] ,arr2[size2]
,arr[index+1] ;
scanf("%d%d",&size1,&size2);
printf("Enter array1 elements:");
for(int i=0;i<size1;i++)
{
scanf("%d",&arr1[i]);
arr[index--]=arr1[i];
}
printf("Enter array2 elements:");
for(int i=0;i<size2;i++)
{
scanf("%d",&arr2[i]);
arr[index--]=arr2[i];
}
printf("First array is ");
for(int i=0;i<size1;i++)
printf("%d ",arr1[i]);
printf("\nSecond array is ");
for(int i=0;i<size2;i++)
printf("%d ",arr2[i]);
printf("\nArray in reverse order is ");
for(int i=0;i<size2+size1;i++)
printf("%d ",arr[i]);
printf("\n");
}

3. Write a program that reads a two dimensional matrix and


displays the sum of its diagonal.
Input: The two dimensional matrix is
2 4 15
4 3 10
579
Output: The sum of diagonal is 14
CODE;
#include <stdio.h>
int main()
{
int r,c, arr[r][c], sum=0;
printf("Enter array dimensions:");
scanf("%d %d",&r,&c);
printf("Enter array elements:");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
if(i==j)
sum+=arr[i][j];
}
}
printf("Sum of diagonal is %d\n",sum);
}

4. Write a program that reads a matrix and displays the sum


of the elements below the main diagonal.
Input: The two dimensional matrix is
21 4 15
4 34 10
5 7 99
Output: The sum of diagonal is 16
CODE
#include <stdio.h>
int main()
{
int r,c, arr[r][c], sum=0;
printf("Enter array dimensions:");
scanf("%d %d",&r,&c);
printf("Enter array elements:");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
if(i>j)
sum+=arr[i][j];
}
}
printf("Sum of elements below main diagonal is %d\n",sum);
}

5.Write a program in c to concatenate first n characters of a


string to another string without using inbuilt functions.
Input: The first string is Indian
The second string is Institute
Output: Concatenation of first 3 elements of first string with
second string is InstituteInd
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[1000];
char s2[1000];
int l=0,n;
printf("The first string is ");
scanf("%s",s1);
printf("The second string is ");
scanf("%s",s2);
for(int i=0;s2[i]!='\0';i++)
l++;
printf("n=");
scanf("%d",&n);
int index=0;
for(int i=l;i<(l+n);i++)
s2[i]=s1[index++];
printf("Concatenation of first %d elements of first string to
second string is ",n);
for(int i=0;i<(l+n);i++)
printf("%c",s2[i]);
printf("\n");
}

6. Write a program to display the word Hello in the following


format:
H
HE
HEL
HELL
HELLO
CODE:
#include <stdio.h>
int main()
{
char s[]="HELLO";
for(int i=0;i<5;i++)
{
for(int j=0;j<=i;j++)
printf("%c ",s[j]);
printf("\n");
}
}
7. Write a program to display a given string in reverse order
without using inbuilt functions.
Input: The string is Indian
Output: After reverse the string is naidnI
CODE:
#include <stdio.h>
int main()
{
char ch[1000];
int l=0;
printf("The string is ");
scanf("%s",ch);
for(int i=0;ch[i]!='\0';i++)
l++;
printf("The reverse string is ");
for(int i=l-1;i>=0;i--)
printf("%c",ch[i]);
printf("\n");
}

8. Write a program to count the number of numerical digits,


upper case character, lower case characters and special
character in a given string.
Input: The string is Indian@#436ISM
Output: Number of digits: 3
Number of upper case character: 4
Number of lower case characters: 5
Number of special character: 2
CODE:
#include <stdio.h>
int main()
{
char s[10000];
printf("The string is ");
scanf("%s",s);
int up=0,low=0,dig=0,spe=0;
for(int i=0;s[i]!='\0';i++)
{
int a=s[i];
if(a>=65&&a<=90)
up++;
else if(a>=97&&a<=122)
low++;
else if(a>=48&&a<=57)
dig++;
else
spe++;
}
printf("Number of digits:%d\n",dig);
printf("Number of upper case character:%d\n",up);
printf("Number of lower case character:%d\n",low);
printf("Number of special character:%d\n",spe);
}

9. Write a program in c to find a given string is a palindrome or


not
Input: The string is madam
Output: The string is palindrome
Or
Input: The string is Indian
Output: The string is not a palindrome
CODE
#include<stdio.h>
int main()
{
char ch[10000];
int i;
printf("The string is ");
scanf("%s",ch);
for(i=0;ch[i]!='\0';i++);
int flag=0;
for(int j=0;j<i/2;j++)
{
if(ch[j]!=ch[i-j-1])
flag=1;
}
if(flag==0)
printf("The string is palindrome\n");
else
printf("The string is not palindrome\n");
}

10. Write a program to copy the last n characters of a


character array in another character array. Also convert the
lower case letters to upper case letters while copying and
display the result.
Input: The first string is Indian
The second string is Institute
Number of last character of first string to concatenate in
second string is 3
Output: After concatenation of last 3 elements of first string
with second string with uppercase conversion is InstituteIAN

CODE:
#include <stdio.h>
#include <ctype.h>
int main()
{
char s1[1000];
char s2[1000];
printf("The first string is ");
scanf("%s",s1);
printf("The second string is ");
scanf("%s",s2);
int l=0,l1=0;
for(int i=0;s2[i]!='\0';i++)
l++;
for(int i=0;s1[i]!='\0';i++)
l1++;
printf("n=");
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
s2[l+i]=toupper(s1[l1-n+i]);
printf("After concatenation of last %d elements of first string
with second string with uppercase conversion is ",n);
for(int i=0;i<(l+n);i++)
printf("%c",s2[i]);
printf("\n");
}

You might also like