0% found this document useful (0 votes)
60 views10 pages

C Assignment

The document provides 20 C programming problems and their solutions involving arrays, pointers, strings, matrices, and other basic programming concepts. Each problem is presented with a description of the task and the code required to solve it. The problems cover a range of fundamental programming tasks like reading input from the user, storing values in arrays, performing arithmetic operations, conditional checking, loops, functions, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

C Assignment

The document provides 20 C programming problems and their solutions involving arrays, pointers, strings, matrices, and other basic programming concepts. Each problem is presented with a description of the task and the code required to solve it. The problems cover a range of fundamental programming tasks like reading input from the user, storing values in arrays, performing arithmetic operations, conditional checking, loops, functions, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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

2. write a program to convert the given binary number into decimal.


#include <stdio.h>
#include <math.h>

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

4. write a program to generate Fibonacci sequence


#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: \n");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
Output:-
C ASSIGNMENT

6. write a program to accept an integer and reverse it.


#include<stdio.h>
int main()
{
int n,rev=0,d;
printf("enter the value of n:");
scanf("%d",&n);
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
printf("the reverse number is %d",rev);
}

Output: -

9. write a program to compute the surface area and volume of a cube.


#include<stdio.h>
int main()
{
int a,b,c,surface,volume;
printf("enter a value:");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
printf("enter c value:");
scanf("%d",&c);
C ASSIGNMENT

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:-

You might also like