C Assignment
C Assignment
C Assignment
1. write a C program to read N integers and store them in an array A, and sum
of all these elements using pointers. output the given array and the
computed sum with suitable heading
#include <stdio.h>
int main()
{
int n, *p=0, s= 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
p = &a[0];
printf("Enter the elements of the array: \n");
for (int i = 0; i < n; i++) {
scanf("%d",p);
s += *p;
p++;
}
printf("The elements of the array are: \n");
p= &a[0];
for (int i = 0; i < n; i++) {
printf("%d ", *p);
p++;
}
printf("\nThe sum of the elements is: %d", s);
return 0;
}
Output: -
C ASSIGNMENT
int main() {
int b, d = 0, base = 1, rem;
printf("Enter a binary number: ");
scanf("%d", &b);
while (b > 0) {
rem = b% 10;
d += rem * base;
b /= 10;
base *= 2;
}
printf("Decimal equivalent: %d", d);
return 0;
}
Output: -
3. write a program to accept N integer number and store then in an array AR.
#include <stdio.h>
int main() {
int n, a[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The elements of the array are: \n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;}
C ASSIGNMENT
Output: -
surface=2*(a*b+b*c+c*a);
printf("the surface area of the cube is %d\n",surface);
volume=a*a*a;
printf("the volume of the cube is %d",volume);
}
Output:-
10. write a program to accept a decimal number and convert it to binary and
count the number of 1’s in the binary number.
#include <stdio.h>
#include <math.h>
int main() {
int b, d = 0, base = 1, rem,count=0;
printf("Enter a binary number: ");
scanf("%d", &b);
while (b > 0) {
rem = b% 10;
d += rem * base;
b /= 10;
base *= 2;
count++;
}
printf("Decimal equivalent: %d\n", d);
printf("the count is %d\n",count);
return 0;
}
C ASSIGNMENT
Output: -
11. write a program to find whether a given year is leap year or not.
#include<stdio.h>
int main()
{
int year;
printf("enter year:");
scanf("%d",&year);
if(year%4==0)
printf("leap year");
else
printf("not leap year");
}
Output:-
12. write a program to swap the contents of two numbers using bitwise XOR
operation. Don’t use either the temporary variable or arithmetic operators.
#include<stdio.h>
int main()
{
int n1=10,n2=20;
printf("before swaping is n1=%d and n2=%d\n",n1,n2);
n1=n1^n2;
n2=n1^n2;
n1=n1^n2;
printf("after swaping is n1=%d and n2=%d\n",n1,n2);
}
C ASSIGNMENT
Output:-
15. write a program to accept two strings and concatenate them. That is, the
second string is appended to the end of the first string.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100],s2[100],ch;
printf("\n enter string s1:");
gets(s1);
printf("\n enter string s2:");
gets(s2);
printf("\n string s1 is:%s",s1);
printf("\n string s2 is:%s",s2);
strcat(s1,s2);
printf("\n string s1 is:%s",s1);
printf("\n string s2 is:%s",s2);
}
Output: -
16. write a program to find the length of the string without using the built-in
function.
#include<stdio.h>
C ASSIGNMENT
#include<string.h>
int main()
{
char s[100],ch;
int ls=0,i=0;
printf("\n enter any string:");
gets(s);
ch=s[0];
while(ch!='\0')
{
ls++;
i++;
ch=s[i];
}
printf("\n length of the string %s is %d",s,ls);
}
Output: -
20. write a program to accept two matrices and find the sum and difference of
the matrices.
#include<stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
printf("enter the elements in array of a:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("the elements in a:\n");
for(i=0;i<2;i++)
C ASSIGNMENT
{
for(j=0;j<2;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("enter the elements in array of b:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("b[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
printf("the elements in b:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",b[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("the sum of two matrices:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
for(i=0;i<2;i++)
{
C ASSIGNMENT
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("the difference of two matrices:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
}
Output:-