Sie sind auf Seite 1von 11

Computer Science - I Lab.

1.

Create a one dimensional integer array to accept 10 numbers and display


their sum?

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

int main()
{
int n,i,a[200],sum=0;
printf("Enter the size of the array \n\n");
scanf("%d", &n);
printf("\nEnter the elements of array\n\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
sum+=a[i];

printf("\n\n Sum=%d\n\n", sum);


return 0;
}

2.

Write a program to find out the largest and smallest number in an entered array without using
sorting.

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

int main()
{
int n,i,a[200],min,max;
printf("Enter the size of the array \n\n");
scanf("%d", &n);
printf("\nEnter the elements of array\n\n");
for(i=0;i<n;i++)

{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
else if(a[i]>max)
max=a[i];
}

printf("\n\n min = %d and max= %d\n\n", min, max);


return 0;
}

3 WAP to find the given number is Perfect/Abundant/Deficient number?

#include <stdio.h>
int main()
{
int n,i,sum=0;
printf("UPES C-Programming\n");
printf(" Enter the value of n\n\n");
scanf("%d",&n);
printf("the factors are\n");
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum+=i;
printf("%d\t",i);
}
}
printf("\n\n----------------------------\n\n");
if(sum==n)
printf("%d is perfect number \n", n);
else if(sum<n)
printf("It is Deficient number \n");
else
printf("It is Abendent \n");

return 0;
}

4 Write a menu driven program to perform an operation multiplication, addition and subtraction on 2-D
matrices entered through user as per user choice.
#include<stdio.h>
#include<stdlib.h>
void addition();
void subtraction();
void multiplication();
int n,i,j,k,a[20][20],b[20][20],res[20][20],ch;
int main()
{
printf("Enter the size of the matrix \n\n");
scanf("%d", &n);
printf("\nEnter the elements of first matrix \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of second matrix \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Enter your choice\n\n ");
printf("1. for addition\n");
printf("2. for subtraction\n");
printf("3. for multiplication\n");
scanf("%d",&ch);

switch(ch)
{
case 1: addition();
break;
case 2: subtraction();
break;

case 3: multiplication();
break;
}
printf("Resultent matrix is\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",res[i][j]);
}
printf("\n");
}
return 0;
}

// ************************************************************
void addition()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=a[i][j]+b[i][j];
}
}
}

// ************************************************************
void subtraction()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=a[i][j]-b[i][j];
}
}
}
// ************************************************************
void multiplication()
{
for(i=0;i<n;i++)

{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
res[i][j]+=a[i][k]*b[k][j];
}
}
}
}

5. Design a 2-D matrix to accept 8 numbers through user and find out the sum of each row
and print the same as last column of the same matrix?
#include<stdio.h>
#include<stdlib.h>
int n,i,j,k,a[20][20],sum=0;
int main()
{
printf("Enter the size of the matrix \n\n");
scanf("%d", &n);
printf("\nEnter the elements of first matrix \n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
a[i][j]=sum;
sum=0;
}

for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
printf("%d\t",a[i][j]);

}
printf("\n");
}

return 0;
}

6. Write a program to enter a string through user and find whether the entered string is palindrome or
not.
#include<stdio.h>
#include<stdlib.h>
int i,n,j=0,len, flag;
char c[30];
int main()
{
int i;
char str[20];
printf("Enter the string \n\n");
for(i=0; ;i++)
{
scanf("%c",&str[i]);
if(str[i]=='\n')
break;
}
n=len=i;
printf("Length of the string is %d\n",len);

for(i=0;i<len;i++)
c[i]=str[i];
printf("the cpoied string is\n\n");
for(i=0;i<len;i++)
printf("%c",c[i]);
printf("\n\n");

printf("the reversed string is\n\n");


for(i=len-1;i>=0;i--)
c[j++]=str[i];

for(i=0;i<=len;i++)
printf("%c",c[i]);

printf("\n\n");
printf("Palindrome or not \n\n");
if(n%2==0)
flag=0;
else
{
j=n-1;
for(i=0;i<=n/2;i++)
{
if(str[i]==c[j--])
{
flag=1;
}
else
{
flag=0;
break;
}
}
}

if(flag==1)
printf("It is a palindrome");
else
printf("It is not a palindrome");

return 0;
}

7. Write a program which will change the case of entered string.


#include<stdio.h>
#include<stdlib.h>
int i,n,len;
char c[30];
int main()
{
char str[20];
printf("Enter the string \n\n");
for(i=0; ;i++)
{
scanf("%c",&str[i]);
if(str[i]=='\n')

break;
}
len=i;

for(i=0;i<len;i++)
{
if(str[i] >='A' && str[i] <= 'Z')
str[i]+= 'a'-'A';
else if(str[i] >='a' && str[i] <= 'z')
str[i]-= 'a'-'A';
}
printf("the cpoied string is\n\n");
for(i=0;i<len;i++)
printf("%c",str[i]);
printf("\n\n");

return 0;
}
8. WAP to find the no of ovals, Consonants, digits, words from the given text?
#include<stdio.h>
#include<stdlib.h>
int i,word=1,cons=0,digit=0,oval=0,len;
int main()
{
char str[20];
printf("Enter the string \n\n");
for(i=0; ;i++)
{
scanf("%c",&str[i]);
if(str[i]=='\n')
break;
}
len=i;

for(i=0;i<len;i++)
{
if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' ||str[i]=='i' || str[i]=='I' ||str[i]=='o' || str[i]=='O'
||str[i]=='u' || str[i]=='U')
{

oval++;
}
else if(str[i]=='0' || str[i]=='1'|| str[i]=='2'|| str[i]=='3'||str[i]=='4' || str[i]=='5'|| str[i]=='6'||
str[i]=='7'||str[i]=='8' || str[i]=='9')
{
digit++;
}
else if(str[i]==' ')
word++;
else
cons++;
}

printf("\n The number of ovals= %d\n",oval);


printf("\n The number of digits= %d\n", digit);
printf("\n The number of words= %d\n", word);
printf("\n The number of consonents= %d\n", cons);
return 0;
}
9. WAP to sort the names of students?
#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
puts("Enter the strings to be sorted");
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}

10. Design a program, which uses pointers to perform the following actions, addition, subtraction,
multiplication and division of given 2 numbers.
#include<stdio.h>
#include<stdlib.h>
float *p,*q,a,b, res;
int ch;
int main()
{
printf("Enter the values of a and b \n\n");
scanf("%f%f",&a,&b);
printf("Enetr your choice\n");
printf("1. for addition\n");
printf("2. for subtraction\n");
printf("3. for multiplication \n");
printf("4. for division \n");
scanf("%d",&ch);
p=&a;
q=&b;
switch(ch)
{
case 1: res= *p+*q;
break;
case 2: res= *p-*q;
break;
case 3: res= *p *(*q);
break;
case 4: res= *p/(*q);
break;
}
printf("Result= %f\n\n", res);
return 0;
}

11. Read the minutes from the keyboard and find out the no. of hours, mins , days ?
(Ex 1210 mins are displayed as 0 days,20 hrs,10 min)
#include<stdio.h>
int main()
{
int m=0,n,r,h=0,s=0,t;
printf("Enter the time in seconds\n\n");
scanf("%d", &t);
n=t;

if(t>3600)
{
h=t/3600;
n=t%3600;
}
if(n>60)
{
m=n/60;
s=n%60;
}
else
{
m=n;
s=0;
}

printf("Hours=%d\n", h);
printf("Minutes=%d\n", m);
printf("Second=%d\n", s);

return 0;
}

Das könnte Ihnen auch gefallen