ASSIGNMENT : 4
1 Display this kind of output on screen.
C
CP
CPR
. .
CPROGRAMING
. .
CPR
CP
C
2 Write a program which will take 10 numbers from user and stored it in
the array. It will print all the numbers, their sum and average of it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] , i ;
float avg , sum=0 , no ;
clrscr();
printf("Enter Limit of
Array : ");
scanf("%f",&no);
for(i=0 ; i<no ; i++)
{
printf("\nEnter
Element %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\nEnter
Elements in Array are : ");
for(i=0 ; i<no ; i++)
{
printf("%d
",a[i]);
sum=sum+a[i];
}
avg = sum/no;
printf("\n\nSum = %.2f
\n Average = %.2f",sum,avg);
getch();
}
3 Write a program to find binary of given number.
#include<stdio.h>
#include<conio.h>
void main()
{
long no , decino , rem , base=1 , binary = 0 ;
clrscr();
printf("\nEnter a
Decimal Integer : ");
scanf("%ld",&no);
decino=no;
while(no>0)
{
rem = no % 2 ;
binary = binary +
rem * base ;
no = no / 2 ;
base = base * 10 ;
}
printf("\n\nEntered
number is : %d",decino);
printf("\n\nIts Binary
number is : %ld",binary);
getch();
}
4 Write a program to sort and array.
#include<stdio.h>
#include<conio.h>
void main()
{
int size , a[10] , i , j ,
temp ;
clrscr();
printf("Enter Limit of
Array :
");
scanf("%d",&size);
for(i=0 ; i<size ; i++)
{
printf("\nEnter
Element %d : ",i+1);
scanf("%d",&a[i]);
}
for(i=0 ; i<size ; i++)
{
for(j=0 ; j<size
; j++)
{
if(a[j]>a[j+1])
{
temp
= a[j] ;
a[j]
= a[j+1] ;
a[j+1]
= temp ;
}
}
}
printf("\nSorted
Elements \n ");
for(i=0 ; i<size ; i++)
{
printf("\n%d",a[i]);
}
getch();
}
5 Write a program to search an element from the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] , size , i ,
search , flag ;
clrscr();
printf("\nEnter number
of Elements : ");
scanf("%d",&size);
for(i=0 ; i<size ; i++)
{
printf("Enter
Element %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n\n\nEnter
value you want to find : ");
scanf("%d",&search);
flag=0;
for(i=0 ; i<size ; i++)
{
if(a[i]==search)
{
flag=1;
printf("\n%d is found at location %d",search,i+1);
break;
//remove break if you want to search same elements like 11, 11,11 at different places
}
}
if(flag==0)
{
printf("\n%d
is not found in the array
",search);
}
getch();
}
6 Write a program to find addition of two matrices of 3*3.
#include<stdio.h>
#include<conio.h>
void main()
{
int first[10][10] ,
second[10][10] , sum[10][10] ;
int m , n , i , j ;
clrscr();
printf("\nEnter the
Number of Rows in Matrix : ");
scanf("%d",&m);
printf("\nEnter the
Number of columns in Matrix : ");
scanf("%d",&n);
printf("\nEnter the
Elements of 1st Matrix : \n");
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n ;
j++)
{
scanf("\t%d",&first[i][j]);
}
}
printf("\nEnter the
Elements of 2nd Matrix : \n");
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n ;
j++)
{
scanf("\t%d",&second[i][j]);
}
}
printf("\n\nSum of
entered Matrix : \n");
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n
;j++)
{
sum[i][j] =
first[i][j] + second[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
getch();
}
7 Write a program to find multiplication of two matrices of 3*3.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10] , b[10][10] ,
mul[10][10];
int m , n , p , q , i , j ,
k , sum=0;
clrscr();
printf("\nEnter the
Number of Rows in Matrix : ");
scanf("%d",&m);
printf("\nEnter the
Number of columns in Matrix : ");
scanf("%d",&n);
printf("\nEnter the
Elements of 1st Matrix : \n");
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n ;
j++)
{
printf("Enter the Element %d :
",i+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the
number of Rows in 2nd Matrix : \n");
scanf("%d",&p);
printf("Enter the
number of Columns in 2nd Matrix : ");
scanf("%d",&q);
if(n!=p)
{
printf("\nCan
not Multiplied with each other \n");
}
else
{
printf("\nEnter
the elements of the 2nd Matrix :
\n");
}
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n ;
j++)
{
printf("Enter
the element %d : ",i+1);
scanf("%d",&b[i][j]);
}
}
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<q ;
j++)
{
for(k=0 ;
k<p ; k++)
{
sum
= sum + a[i][k]*b[k][j];
}
mul[i][j] =
sum;
sum = 0;
}
}
printf("\nMultiplication
of entered of Matriced : \n");
for(i=0 ; i<m ; i++)
{
for(j=0 ; j<n ;
j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
}
8 Take two strings from the user and check whether the string is
palindrome or not
#include<stdio.h>
#include<conio.h>
void main()
{
int len=0 , i , j , flag=0
;
char s[10];
clrscr();
printf("\nEnter
value :
");
gets(s);
puts(s);
while(s[len]!=NULL)
{
len++;
}
for(i=0 , j=len-1 ;
i<len/2 ; i++ , j--)
{
if(s[i]! = s[j])
flag=1;
break;
}
if(flag==1)
{
printf("\n\nNumber
is not Palindrome");
}
else
{
printf("\n\nNumber
is Palindrome");
}
getch();
}
9 Write a program to find sum, average of two numbers passed to user
defined functions called sum(int,int) and average(int,int)
#include<stdio.h>
#include<conio.h>
int sum(int , int);
int average(int ,int);
void main()
{
int a , b , add , avg ;
clrscr();
printf("\n\nEnter
value of 1st Number : ");
scanf("%d",&a);
printf("\n\nEnter
value of 2nd Number : ");
scanf("%d",&b);
add = sum(a,b);
printf("\n\nSum =
%d",add);
avg=average(a,b);
printf("\n\n\nAverage =
%d",avg);
getch();
}
int sum(int a , int b)
{
int sum = a + b ;
return(sum);
}
int average(int a , int b)
{
int avg = (a + b)/2;
return(avg);
}
10 Write a program to print
factorial of a given number by recursive user defined function fact(int).
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int no , f ;
clrscr();
printf("\n Enter a
Number to find Factorial : ");
scanf("%d",&no);
f = fact(no);
printf("\n %d ! = %d\n",no,f);
getch();
}
int fact(int no)
{
if(no==0)
{
return 1;
}
else
{
return
(no*fact(no-1));
}
}
11 Write a program to print Fibonacci series using recursive UDF.
#include<stdio.h>
#include<conio.h>
void fibo(int n)
{
int c , a=0 , b=1 , i ;
printf("%d\t%d\t",a,b);
c = a + b ;
printf("%d\t",c);
for(i=0 ; i<n ; i++)
{
a = b ;
b = c ;
c = a + b ;
printf("%d\t",c);
}
}
void main()
{
clrscr();
printf("\t\t\t\tFibonacci
Series \n\n\n");
fibo(7);
getch();
}
12 Write a program to find length of the given string (without including
string.h).
#include<stdio.h>
#include<conio.h>
void main()
{
int i , j , len=0;
char s[10] , s2[10];
clrscr();
printf("\nEnter the
data you want to check its length : ");
gets(s);
puts(s);
while(s[len]!='\0')
{
len++;
}
for(i=0 , j=len-1 ;
i<len ; i++ , j--)
{
s2[j]=s[i];
}
printf("%s",s2);
printf("\n");
printf("\nString
length is %d",len);
getch();
}
14 Write a program to convert lowercase string to uppercase string
(without including string.h).
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
clrscr();
printf("\nEnter a
String in Lower Case : ");
gets(str);
printf("\nYour String in Upper Case : %s",strupr(str));
getch();
}
15 Write a program which will accept two strings from the user and print
the message that the strings are same or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20] , str2[20] ,
check ;
clrscr();
printf("\nEnter the
First String : ");
gets(str1);
printf("\nEnter the
Second String : ");
gets(str2);
check = strcmp(str1,str2);
if(check==0)
{
printf("\nString
is Same");
}
else
{
printf("\nString
is not Same");
}
getch();
}
16 Write a program which take a lowercase string from the user and print
its length and uppercase string.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[80];
int length;
clrscr();
printf("\nEnter the
String in Lower Case : ");
gets(str);
length = strlen(str);
printf("\n\nYour
String in Upper Case :
%s",strupr(str));
printf("\n\n\nYour
String in Length : %d",length);
getch();
}
17 Write a program that uses function digit(N,k) that return the value
of the kth digit from the right of the number N. For eg. The function call
digit (254693,2) should return 9
18 Program to find if the given no. is prime or not. The function should
accept the number as argument and return if the no. is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int no , i , a=0 ;
clrscr();
printf("\n\nEnter the
Number : ");
scanf("%d",&no);
for(i=2 ; i<no ; i++)
{
if(no%i==0)
{
a++;
}
}
if(a==0)
{
printf("\n\n%d
is Prime Number ",no);
}
else
{
printf("\n\n%d
is not Prime Number ",no);
}
getch();
}