0% found this document useful (0 votes)
54 views

Assignment of C

The document contains 16 questions related to C programming. Each question provides a code snippet to demonstrate a programming concept such as data types, operators, control flow, functions etc. The questions cover basic to intermediate level C programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Assignment of C

The document contains 16 questions related to C programming. Each question provides a code snippet to demonstrate a programming concept such as data types, operators, control flow, functions etc. The questions cover basic to intermediate level C programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Question – 1 :- Write down the data type used in c along with range,

size (in bytes), and format specifies.


Data Types :- In a programming language the data type is the collection of data with values
having fixed meaning as well as characteristics. Some of them are an integer, floating point,
character, etc. Usually, programming languages specify the range values for given data-
type.ANSI C language supports three classes of data types:

‘C’‘C’
Data Types
Data Types

User Defined Type Built-in Data Types Derived Data Types

Structure Array

Union Function

Class Pointers

Enumeration Referance

Integral Type Void Floating Type

int char
float double

Size, Range and Format Specifier Of Data Type :-

Data Type Format Specifier Size (bytes) Range


char %c 1 -128 to 127
Int %d 2 -32768 to 32767
float %f 4 3.4e -38 to 3.4e +38
Double %lf 8 1.7e -308 to 1.7e +308
long double %Lf 10 3.4e -4932 to 1.1e +4932
long int %ld 4 -2147483648 to 2147483647
Question-2- WAP to show working of postfix and prefix
increment/decrement operators
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x);
printf("Initial value of y = %d\n\n", y);
y = ++x;
printf("After incrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
y = --x;
printf("After decrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
getch();
}

Question :3- WAP to convert temperature in Celsius when input is in


Fahrenheit.
#include<stdio.h>
#include<conio.h>
Void main
{
float f,c;
printf(“Enter Temparature in Fahrenheit\n”);
scanf(“%d”,f);
c=((f-32)/9*5;
printf(“Temparature in Centigrate\n”);
getch();
}

Question:-4- WAP to calculate sum of n natural numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
Int n, i, sum = 0;

printf("Enter any positive number\n");


scanf("%d", &n);

for(i = 1; i <=n; i++)


{
sum = sum + i;
}
printf("Sum of Natural Numbers = %d", Sum);
getch();
}

Question:-5- WAP to interchange the values of 2 variables without


using any third variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
printf("Input value for x & y: \n");
scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y: %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("After swapping the value of x & y: %d %d",x,y);
getch();
}

Question:-6- WAP to find greatest of three numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf("Enter any three numbers=");
scanf("%d %d %d", &a, &b, &c);

if (a > b && a > c)


{
printf("a is Greater");
}
else if (b > a && b > c)
{
printf("b is Greater");
}
else if (c > a && c > b)
{
printf("c is Greater");
}
Else
{
printf("Wrong Input");
}
getch();
}

Question:-7- WAP to find the roots of quadratic equation.


# include<stdio.h>
# include<conio.h>
# include<math.h>
main ()
{
float a,b,c,r1,r2,d;
printf (“enter the values of a b c”);
scanf (“ %f %f %f”, &a, &b, &c);
d= b*b – 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf (“The real roots = %f %f”, r1, r2);
}
else if (d= =0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“roots are equal =%f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ();
}

Question:-8- WAP to calculate simple interest & compound interest.


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

int main()
{
float p, t, r, si, ci;
clrscr();
printf("Enter principal amount =");
scanf("%f", &p);
printf("Enter time in year =");
scanf("%f", &t);
printf("Enter rate in percent =");
scanf("%f", &r);
si = (p * t * r)/100.0;
ci = p * (pow(1+r/100, t) - 1);
printf("Simple Interest = %f", si);
printf("Compound Interest = %f", ci);
getch();
}

Question:-9- WAP to check whether a number is divisible by 5 and 7 or


not.
#include<stdio.h>
#include<conio.h>
void main()
{
  int num,f,m;
  printf("Enter any number");
  scanf("%d",&num);
  if(num%5==0)
 {
   f=1;
 }
    if(num%7==0)
 {
    m=1;
 }
if(f==1 && m==1)
 {
printf("The number %d is divisible by 5 and 7",num);
 }    
else if(f==1)
 {
printf("The number %d is divisible by 5 but not by 7",num);
}
else if(m==1)
{
printf("The number %d is divisible by 7 but not by 5",num);
}    
else
{
    printf("\nThe number %d is neither divisible by 5 nor by 7",num);
}
getch();
}

Question:-10- WAP to check whether a character is vowel or consonant


Soluton:-
#include<stdio.h>
#include<conio.h>
void main()
{
char n;
printf(“Enter any character”);
scanf(“%c” &n);
if(n==’a’|| n==’e’|| n==’i’|| n==’o’|| n==’u’|| n==’A’|| n==’E’|| n==’I’|| n==’O’|| n==’U’)
{
printf(“Character is vowel”);
}
else
{
printf(“Character is consonant);
}
getch();
}

Question:-11- WAP to check whether a character is alphabet or digit or


special character
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter any character= ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
getch();
}

Question:-12- WAP to input all sides of a triangle and check whether


triangle is valid or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int side1, side2, side3;
printf("Enter three sides of triangle: \n");
scanf("%d%d%d", &side1, &side2, &side3);
if((side1 + side2) > side3)
{
if((side2 + side3) > side1)
{
if((side1 + side3) > side2)
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
}
else
{
printf("Triangle is not valid.");
}
}
else
{
printf("Triangle is not valid.");
}
getch();
}

Question:-13- WAP to check whether the triangle is equilateral,


isosceles or scalene triangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int side1, side2, side3;
printf("Enter sides of triangle:");
scanf("%d%d%d",&side1,&side2,&side3);
if(side1 == side2 && side2 == side3)
printf("The Given Triangle is equilateral");
else if(side1 == side2 || side2 == side3 || side3 == side1)
printf("The given Triangle is isosceles");
else
printf("The given Triangle is scalene");
getch();
}

Question:-14- WAP to demonstrate working of traffic lights


#include <stdio.h>
#include<conio.h>
void main()
{
char colour;
printf ("Enter the colour of the traffic light= ");
scanf ("%c", &colour);
switch (colour)
{
case 'R':
case 'r':
printf ("STOP");
break;

case 'Y':
case 'y':
printf ("WAIT");
break;
case 'G':
case 'g':
printf ("GO");
break;
default:
printf ("Wrong Input");
}
getch();
}

Question:-15- If a five-digit no. is given –


a. WAP to calculate the sum of its digits.
b. WAP to reverse the number.
c. WAP to calculate sum of its first & last digit.
d. WAP to print a new number by adding one to each of its digits.
(a)
#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,b,c,d,e;
printf("Enter a five digit number : ");
scanf("%d", &num);
e=num%10;
d=(num/10)%10;
c=(num/100)%10;
b=(num/1000)%10;
a=(num/10000);
printf("%d is the sum of the digits of the number %d.", a+b+c+d+e, num);
getch();
}

(b)
#include<stdio.h>
#include<conio.h>
void main()
{
int n, rev = 0, remainder;
printf("Enter integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
getch();
}

(c)
#include <stdio.h>
#include<conio.h>
void main()
{
int n, sum=0, firstDigit, lastDigit;
printf("Enter number to find sum of first and last digit = ");
scanf("%d", &n);
lastDigit = n % 10;
while(n >= 10) {
n = n / 10;
}
firstDigit = n;
sum = firstDigit + lastDigit;
printf("Sum of first and last digit = %d", sum);
getch();
}

(d)
#include<stdio.h>
#include<conio.h>
void main()
{
int num, sum, i, number, count=0, n=1;
printf("Enter N Digit's Number: ");
scanf("%d", &num);
number = num;
while(number!=0)
{
number = number/10;
count = count + 1;
}
for(i=1;i<count;i++)
{
n = n * 10;
n = n + 1;
}
sum = num + n;
printf("Output: %d", sum);
getch();
}

Question:-16- WAP to Make a Simple Calculator Using switch...case


#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int a, b, result;
printf("Enter an Operator (+, *, *, /): ");
scanf("%c", &ch);
printf("Enter two operands: \n");
scanf("%d %d", &a, &b);

switch(ch)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
printf("Result = %d", result);
getch();
}

Question:-17- WAP to calculate factorial of a number


#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,num;
printf("Enter any number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
getch();
}

Question:-18- WAP to Check Whether a Number is Palindrome or Not


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("Number is palindrome\n");
else
printf("Number is not palindrome\n ");
getch();
}

Question:-19- WAP to Display Prime Numbers Between Two Intervals


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, i, j, f;
printf("Enter starting number= ");
scanf("%d", &a);
printf("Enter last number= ");
scanf("%d", &b);
printf("Prime numbers between %d and %d are: ", a, b);
for (i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;
f = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0) {
f = 0;
break;
}
}
if (f== 1)
printf("%d ", i);
}
getch();
}

Question:20- WAP to Display Armstrong Number Between Two


Intervals
# include<stdio.h>
#include<conio.h>
void main( )
{
int a, b, c, d, sum, temp, i, num ;
printf(" Enter Begning of Interval =\n ") ;
scanf("%d ",& a) ;
printf("Enter End of Interval =\n ") ;
scanf("%d ",& b) ;
for ( i = a; i <=b; i++ );
{
sum = 0 ;
num = i ;
do
{
d = num % 10 ;
num = num / 10 ;
c=d*d*d;
sum = sum + c ;
}
while ( num > 0);
if( sum == i )
printf("%d " ,i) ;
}
getch();
}

Question:-21- WAP to Convert Binary Number to Decimal and vice-


versa
Binary To Decimal:-

#include <stdio.h>
#include <conio.h>
void main()
{
int num, bnum, dnum = 0, base = 1, rem;
printf (" Enter a binary number = \n");
scanf (" %d", &num);

bnum = num;

while ( num > 0)


{
rem = num % 10;
dnum = dnum + rem * base;
num = num / 10;
base = base * 2;
}

printf ( " The binary number is %d ", bnum);


printf (" The decimal number is %d ", dnum);
getch();
}
Decimal To Binary:-

#include<stdio.h>
#include<conio.h>
void main()
{
int num, n, rem, bin = 0, p = 1;
printf("Enter any decimal number = :");
scanf("%d", &num);
n = num;
while (n > 0)
{
rem = n % 2;
bin = rem * p;
p *= 10;
n /= 2;
}
printf("Binary =", bin);
getch();
}

Question:-22-WAP to Check Prime or Armstrong Number Using User-


defined Function
#include<stdio.h>
#include<math.h>
int checkPrime(int num);
int checkArmstrong(int num);
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (checkPrime(num) == 0){
printf("%d is a Prime Number.\n", num);
}
else{
printf("%d is not a Prime Number.\n", num);
}
if (checkArmstrong(num) == 0){
printf("%d is an Armstrong Number.\n", num);
}
else{
printf("%d is not an Armstrong Number.\n", num);
}
return 0;
}
int checkPrime(int num)
{
int i, count = 0;
for (i = 2; i <= num/2; i++)
{
if (num % i == 0)
{
count = 1;
break;
}
}
if (num == 1)
count = 1;
return count;
}
int checkArmstrong(int num)
{
int lastdigit = 0, power = 0, sum = 0;

int n = num;
while (n != 0)
{
lastdigit = n % 10;
power = lastdigit * lastdigit * lastdigit;
sum = sum + power;
n = n / 10;
}
if (sum == num)
return 0;
else
return 1;
}

Question:-23- WAP to Find Transpose of a Matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, i, j, matrix[10][10], transpose[10][10];
printf("Enter rows and columns :\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (i= 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < m;i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i< n; i++) {
for (j = 0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
getch();
}

Question:-24- WAP to Multiply to Matrix Using Multi-Dimensional


Arrays
# include<stdio.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], mul[10][10], i, j, k, r, c, sum ;
printf(" Enter the Numbers of Row : ") ;
scanf("%d ",& r) ;
printf("\n Enter the Number of Coloumn : ") ;
scanf("%d ",& c) ;
printf("\n Enter the Element of First Matrix : \n") ;
for ( i = 0 ; i < r ; i++)
{
for ( j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& a[i][j]) ;
}
}
printf("\n Enter the Element of Second Matrix : \n") ;
for ( i = 0 ; i < r ; i++)
{
for ( j = 0 ; j < c ; i++)
{
printf("\n Enter the Element [%d] [%d] : " ,i, j) ;
scanf("%d ",& b[i][j]) ;
}
}
printf("\n\n Element in the first Matrix are : \n") ;
for ( i = 0 ; i < r ; i++)
{
for ( j = 0 ; j < c ; i++)
{
printf("\t %d ", a[i][j]) ;
}
printf(" \n ") ;
}

printf("\n\n Element in Second Matrix are: \n") ;


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

for ( i = 0 ; i < r ; i++)


{
for ( j = 0 ; j < c ; i++)
{
sum = 0 ;
for ( k = 0 ; k < r ; k++)
{
sum = sum + ( a[i][k] * b[k][j] ) ;}
mul[i][j] = sum ;}
}

printf("\n\n Multiplicaiton of Two Matrix are : \n") ;


for ( i = 0 ; i < r ; i++)
{
for ( j = 0 ; j < c ; i++)
{
printf("\t %d ", mul[i][j]) ;
}
printf(" \n ") ;
}
getch() ;
}

Question:-25- WAP to Find Largest Element of an Array


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
double arr[100];
printf("Enter the number of elements = ");
scanf("%d", &n);

for (int i = 0; i < n; ++i) {


printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf", arr[0]);
getch();
}

Question:-26- WAP to Calculate Average Using Arrays


#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}

avg = sum / n;
printf("Average = %.2f", avg);
getch();
}

Question:-27- WAP to Generate Fibonacci series


#include<stdio.h>
#include<conio.h>
void main()
{

int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Question:-28- WAP to calculate factorial using recursion


#include<stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n)


{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Question:-29- WAP to sort element of an array


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
int length = sizeof(arr)/sizeof(arr[0]);
printf("Elements of array = \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("\n");
printf("Elements of array sorted in ascending order = \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
getch();
}

Question:-30- WAP to Store Information of a Student Using Structure


#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[100];

int main()
{
int i,n;
struct student s[100];

printf("Enter total of students:");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\n Enter information of student %d:\n",i+1);
printf("Enter name: ");
scanf("%s", s[i].name);

printf("Enter roll number: ");


scanf("%d", &s[i].roll);

printf("Enter marks: ");


scanf("%f", &s[i].marks);
}

printf("Displaying Information:\n");
for(i=0;i<n;i++)
{
printf("\n %d no. student info\n",i+1);
printf("\t Name:%s\n ",s[i].name);
printf("\t Roll number: %d\n",s[i].roll);
printf("\t Marks: %.1f\n\n",s[i].marks);
}

return 0;
}

Question:-31- WAP to Store Information of a Student Using Union


#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;

int main() {
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);

printf("Enter roll number: ");


scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);

printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}

Question:-32- Calculate sum of following series


1. 1^2+2^2+3^2+4^2+……. N^2
2. 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
3. 1+ 1/2 + 1/3 + 1/4 + 1/5 + ……. 1/N

1-
#include <stdio.h>
 
int main()
{
int number, i;
int sum = 0;
 
printf("Enter maximum values of series number: ");
scanf("%d", &number);
sum = (number * (number + 1) * (2 * number + 1 )) / 6;
printf("Sum of the above given series : ");
for (i = 1; i <= number; i++)
{
if (i != number)
printf("%d^2 + ", i);
else
printf("%d^2 = %d ", i, sum);
}
return 0;
}

2-
#include<stdio.h>
#include<conio.h>
Void nain ()
{
Int num=1, count, limit;
Float sum=0.0,fact;
Printf(“enter the number of terms\n”);
Scanf(“d”,&limit);
While(num<=limit)
{
fact=1;
for(count=1;count<=num;count++)
{
Fact=fact*count;
}
Sum=sum+(num/fact);
num++;
}
printf(“sum of %d terms of series is %f\n”,limit,sum);
getch();
}

3-
#include <stdio.h>
double sum(int n)
{
  double i, s = 0.0;
  for (i = 1; i <= n; i++)
      s = s + 1/i;
  return s;
}
 
int main()
{
    int n = 5;
    printf("Sum is %f", sum(n));
    return 0;
}

Question:-33- WAP to concatenate two strings


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("After Concatenate = %s",a);
getch();
}

Question:-34- WAP to reverse a string


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[40];
printf (" Enter a string to be reversed =\n ");
scanf ("%s", str);
printf (" \n After the reverse of a string: %s ", strrev(str));
getch();
}

Question:-35- WAP to demonstrate Passing pointers to functions

#include<stdio.h>
#include<conio.h>
void Swap(int *x, int *y)
{
int Temp;
Temp = *x;
*x = *y;
*y = Temp;
}
int main()
{
int a, b;
printf ("Please Enter 2 Integer Values : ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping A = %d and B = %d", a, b);
Swap(&a, &b);
printf(" \nAfter Swapping A = %d and B = %d \n", a, b);
}

Question:-36- WAP to demonstrate working of file operations (READ &


WRITE) in a file

Question:-37- WAP to draw a line using computer graphics tools

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gdriver = DETECT, gmode;
int x1 = 200, y1 = 200;
int x2 = 300, y2 = 300;
clrscr();
initgraph(&gdriver, &gmode,"C:\\TURBOC3\\BGI");
line(x1, y1, x2, y2);
getch();
closegraph();
}

You might also like