0% found this document useful (0 votes)
7 views20 pages

Vimp Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

Vimp Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

MSBTE : PIC (22226)

V. IMP QUESTIONS
BRANCH:
CO/IF/AIML

1.Write a program to Print values of variables and their addresses.


Ans:
nclude<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=5;
b=10;
printf("\n Value of a=%d",a);
printf("\n Address of a=%u",&a);
printf("\n Value of b=%d",b);
printf("\n Address of b=%u",&b);
getch();
}

2.Write a program to convert temperature in Fahrenheit degrees to Centigrade degrees.


Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Fahrenheit =%f Temperature in Centigrade
=%f", fahrenheit, celsius);
getch();
}
3.Develop a program to find diameter, circumference and area of circle using function.
Ans:
#include<stdio.h>
#include<conio.h>
void circle(float r)
{
float diameter,circumference,area;
diameter=2*r;
printf("\n Diameter=%f",diameter);
circumference=2*3.14*r;
printf("\n Circumference=%f",circumference);
area=3.14*r*r;
printf("\n Area=%f",area);
}
void main()
{
float radius;
clrscr();
printf("\n Enter radius:");
scanf("%f",&radius);
circle(radius);
getch();
}

4.Write a program to accept the value of year as input from the keyboard & print whether it is a leap
year or not.
Ans: #include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf("Enter year");
scanf("%d",&year);
if(year%4==0) {
printf("Year %d is a leap year",year);
} else {
printf("Year %d is not a leap year",year);
}
getch();
}
5.Implement a program to demonstrate logical AND operator.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int j;
clrscr();
printf("Enter the values of i and j");
scanf("%d%d",&i,&j);
if(i==5 && j==5) {
printf("Both i and j are equal to 5");
} else {
printf("Both the values are different and either or both are not
equal to 5");
}
getch();
}

6.Write a program to swap two numbers using call be value.


Ans : #include<stdio.h>
#include<conio.h>
void swap(int a, int b) {
int temp;
temp=a;
a=b;
b=temp;
printf("Numbers after swapping no1=%d and no2=%d",a,b);
}
void main() {
int no1, no2;
clrscr();
printf("Enter the 2 numbers");
scanf("%d%d",&no1,&no2);
printf("Numbers before swapping no1=%d and no2= %d",no1, no2);
swap(no1,no2);
getch();
}

7.Write a program to swap two numbers using call be reference.


Ans : #include<stdio.h>
#include<conio.h>
void swap(int *a, int *b) {
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("Numbers after swapping no1=%d and no2=%d",a,b);
}
void main() {
int no1, no2;
clrscr();
printf("Enter the 2 numbers");
scanf("%d%d",&no1,&no2);
printf("Numbers before swapping no1=%d and no2= %d",no1, no2);
swap(&no1,&no2);
getch();
}

8.Develop a program to swap two numbers using pointer and add swaped numbers also
print their addition.
Ans:
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int x,y,sum;
printf("\n Enter value for x:");
scanf("%d",&x);
printf("\n Enter value for y:");
scanf("%d",&y);
swap(&x,&y);
printf("\nx=%d",x);
printf("\ny=%d",y);
sum=x+y;
printf("Sum of x+y = %d",sum);
}

9.Write a program to calculate sum of all the odd numbers between 1 to 20.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
inti,sum=0;
clrscr();
for(i=1;i<=20;i++)
{
if(i%2!=0)
{
sum=sum+i;
}
}
printf("Sum=%d",sum);
getch();
}

10.Write a function to print Fibonacci series starting from 0, 1.


Ans :
void Fibbo()
{
inta,b,c,limit,i;
printf("\n Enter number:");
scanf("%d",&limit);
a=0;
b=1;
printf("%d\t%d",a,b);
for(i=0;i<limit-2;i++)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}

11.Calculate factorial of a number using recursion.


Ans:
#include<stdio.h>
#include<conio.h>
int factorial(int no)
{
if(no==1)
return(1);
else
return(no*factorial(no-1));
}
void main()
{
intfact,no;
clrscr();
printf("\n Enter number");
scanf("%d",&no);
fact=factorial(no);
printf("\n Factorial number=%d",fact);
getch();
}

12.Write a C program to print following pattern using loop.


ANS.
1
22
333
4444
55555
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

13.Write a C program with comments to reverse the digit of integer number. For example the
number 12345 should be displayed as 54321.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{ int num, res=0,ans=0; clrscr();
printf("Enter the number");
scanf("%d", &num);
while(num!=0) { res=num%10; ans=ans*10+res; num=num/10;
}
printf("Reverse number is %d", ans);
getch();
}
14.Write a program to reverse the number 1234 (i.e. 4321) using function.
Ans:
#include<stdio.h>
#include<conio.h>
void findReverse();
void main()
{
findReverse();
}
void findReverse()
{
int num, res=0,ans=0;
clrscr();
printf("Enter the number");
scanf("%d", &num);
while(num!=0)
{
res=num%10;
ans=ans*10+res;
num=num/10;
}
printf("Reverse number is %d", ans);
getch();
}

15.Write a program to add, subtract, multiply and divide two numbers, accepted from user switch case.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch,add,sub,mul,div;
clrscr();
printf("\n1 for addition \n2 for substraction");
printf("\n3 for multiplication \n4 for division");
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add=a+b;
printf("Addition of a & b=%d",add);
break;
case 2:
sub=a-b;
printf("Substraction of a & b=%d",sub);
break;
case 3:
mul=a*b;
printf("Multiplication of two numbers=%d",mul);
break;
case 4:
div=a/b;
printf("Division of two numbers=%d",div);
break;
default:
printf("Invalid choice....");
}
getch();
}

16.Write a program using switch statement to check whether entered character is VOWEL
or CONSONANT
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter character:");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("\n Entered character is VOWEL");
break;
default:
printf("\n Entered character is CONSONANT");
}
getch();
}
17.Write a program to perform addition, subtraction, multiplication and division of two
integer number using function.
Ans:
#include<stdio.h>
#include<conio.h>
void add(int x,int y)
{
printf("\nAddition=%d",x+y);
}
void sub(int x,int y)
{
printf("\nSubtraction=%d",x-y);
}
void mult(int x,int y)
{
printf("\nMultiplication=%d",x*y);
}
void div(int x,int y)
{
printf("\nDivision=%d",x/y);
}
void main()
{
intx,y;
clrscr();
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
add(x,y);
sub(x,y);
mult(x,y);
div(x,y);
getch();
}

18.Write a program to accept two numbers from user and perform addition, subtraction,
multiplication and division operations using pointer.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,*ptr1,*ptr2,result;
clrscr();
printf("Enter no1:");
scanf("%d",&no1);
printf("\nEnter no2:");
scanf("%d",&no2);
ptr1=&no1;
ptr2=&no2;
result=*ptr1+*ptr2;
printf("\n Addition=%d",result);
result=*ptr1-*ptr2;
printf("\n Subtraction=%d",result);
result=*ptr1**ptr2;
printf("\n Multiplication=%d",result);
result=*ptr1/(*ptr2);
printf("\n Division=%d",result);
getch();
}

19.Write a program to sort elements of an array in ascending order.


Ans:
#include <stdio.h>

void main()
{
int arr1[100];
int n, i, j, tmp;

printf("\n\nsort elements of array in ascending order :\n ");


printf("----------------------------------------------\n");

printf("Input the size of array : ");


scanf("%d", &n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

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


{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}

20.Design a programme in C to read the n numbers of values in an array and display it in


reverse order.
Ans
#include<stdio.h>
#include<conio.h>
#define max 50
void main()
{ int a[max],i,n;
clrscr();
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter array element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Array elements in reverse order:");
for(i=n-1;i>=0;i--)
printf("\t%d",a[i]);
getch();
}

21.Write a program to accept marks of four subjects as input from user. Calculate and display total
and percentage marks of student.
Ans:
#include<stdio.h>
#include<conio.h>
void main() {
float marks[4];
float total=0.0, perc=0.0;
int i;
clrscr();
for(i=1;i<=4;i++) {
printf("Enter marks of subject %d",i);
scanf("%f%",&marks[i]);
}
for(i=1;i<=4;i++){
total=total+marks[i];
}
printf("Total is :%f",total);
perc=total/4;
printf("Percentage is %f",perc);
getch();
}

22.Write a program to accept the value of year as input from the keyboard & print whether it is a leap
year or not
ANS:
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf("Enter year");
scanf("%d",&year);
if(year%4==0) {
printf("Year %d is a leap year",year);
} else {
printf("Year %d is not a leap year",year);
}
getch();
}

23.Write a program to declare an array of 5 elements and display sum of all array elements.
Ans : #include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum=0;
clrscr();
printf("Enter array elements:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
sum=sum+a[i];
printf("\n Sum= %d",sum);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={1,2,3,4,5},i,sum=0; // Array initialization at the time of declaration
clrscr();
for(i=0;i<5;i++)
sum=sum+a[i];
printf("\n Sum= %d",sum);
getch();
}

24.Write a program to compute the sum of all elements stored in an array using pointers.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],sum=0,i,*ptr;
clrscr();
printf("\n Enter array elements:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
ptr=&a[0];
for(i=0;i<5;i++)
{
sum=sum+(*ptr);
ptr=ptr+1;
}
printf("\n Sum= %d",sum);
getch();
}

25.Write a program to find largest number from an array using pointer.


Ans:
#include<stdio.h>
#include<conio.h> void main()
{
int n,*ptr,i,largest=0; clrscr();
printf("Enter how many numbers u want::");
scanf("%d",&n); for(i=0;i<n;i++)
{
printf("\nEnter Number %d :: ",i+1);
scanf("%d",(ptr+i));
}
largest=*ptr;
for(i=1;i<n;i++) { if(*(ptr+i)>largest) largest=*(ptr+i);
}
printf("\nThe Largest Number is %d \n",largest);
getch();
}

26.Write a C program using pointer to read an array of characters and print them in reverse
order.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[10],*ptr; int l=0;
clrscr();
printf("Enter string:");
scanf("%s",str);
ptr=str;
while(*ptr!='\0')
{ l=l+1; ptr=ptr+1; }
while(l>0) { ptr=ptr-1;
printf("%c",*ptr);
l=l-1;
}
getch();
}

27.Write a program to add two 3 x 3 matrices. Display the addition.


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{ int a[3][3],b[3][3],c[3][3],i,j; clrscr();
printf("Enter first matrix elements:\n");
for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]);
}
}
printf("\nEnter second matrix elements:\n");
for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j];
}
printf("\n\nAddition of two matrices is:\n");
for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}

28.Write a program to read two strings and find whether they are equal or not.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[20],st2[20];
printf(“enter string 1”);
scanf(“%s”,st1);
printf(“enter second string”);
scanf(“%s”,st2);
if(strcmp(st1,st2)==0)
printf(“\nboth strings are equal”);
else
printf(“\nstrings are not equal”);
}

29.Write a program to accept a string as input from user and determine its length. [Don’t use built in
library function strlen()]
Ans:
#include<stdio.h>
#include<conio.h>
void main(){
char str[50];
int i, len=0;
clrscr();
printf("Enter a string");scanf("%s",&str);
for(i=0; str[i]!='\0'; i++){
len++;
}
printf("The length of string is %d",len);
getch();
}

30.Write a program to print reverse of a entered string using pointer.


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[10],*ptr;
int l=0;
clrscr();
printf("Enter string:");
scanf("%s",str);
ptr=str;
while(*ptr!='\0')
{
l=l+1;
ptr=ptr+1;
}
while(l>0)
{
ptr=ptr-1;
printf("%c",*ptr);
l=l-1;
}
getch();
}

31.Implement a program to demonstrate concept of pointers to function.


Ans :
Pointer to function:
include<stdio.h>
int sum(int x, int y)
{
return x+y;
}
int main()
{
int s;
int(*fp)(int, int);
fp = sum;
s = fp(10,12);
printf(“Sum = %d”,s);
return 0;
}

32.Write a program to find factorial of a number using recursion


Ans:
#include<stdio.h>
#include<conio.h>
int factorial(int no)
{ if(no==1) return(1);
else return(no*factorial(no-1));
}
void main()
{ int fact,no;
clrscr();
printf("\n Enter number: ");
scanf("%d",&no);
fact=factorial(no);
printf("\n Factorial number=%d",fact);
getch();
}

33.Write a C program to declare structure employee having data member name, age,
designation and salary. Accept and display information of 1 employee.
Ans:
#include<stdio.h>
#include<conio.h>
struct employee
{ char name[20],designation[10];
int age;
long salary;
}
void main()
{ int i; struct employee e; clrscr();
printf("\n Enter name:");
scanf("%s",&e.name);
printf("\n Enter age:");
scanf("%d",&e.age);
printf("\n Enter designation:");
scanf("%s",&e.designation);
printf("\n Enter salary:");
scanf("%ld",&e.salary);
printf("\n\nEmployee's data is:");
printf("\n Name=%s",e.name);
printf("\n Age=%d",e.age);
printf("\n Designation=%s",e.designation);
printf("\n Salary=%ld",e.salary);
getch();
}

34.Write a program to declare structure employee having data member name, age, street
and city. Accept data for two employees and display it.
Ans:
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[10],street[10],city[10];
int age;
};
void main()
{
int i;
struct employee e[2];
clrscr();
for(i=0;i<2;i++)
{
printf("\n Enter name:");
scanf("%s",&e[i].name);
printf("\n Enter age:");
scanf("%d",&e[i].age);
printf("\n Enter street:");
scanf("%s",&e[i].street);
printf("\n Enter city:");
scanf("%s",&e[i].city);
}
for(i=0;i<2;i++)
{
printf("\n Name=%s",e[i].name);
printf("\n Age=%d",e[i].age);
printf("\n Street=%s",e[i].street);
printf("\n City=%s",e[i].city);
}
getch();
}

35.Explain recursion with suitable example. List any two advantages.


Ans:
Recursion means a function calls itself repetitively. A recursive
function contains a function call to itself inside its body.
Example:
#include<stdio.h>
#include<conio.h>
int factorial(int N);
void main()
{
int N,fact;
clrscr();
printf("Enter number:");
scanf("%d",&N);
fact=factorial(N);
printf("\n Factorial is:%d",fact);
getch();
}
int factorial(int N)
{
if(N==1)
return(1);
else
return(N*factorial(N-1));
}
Advantages:
Reduces length of the program
Reduces unnecessary calling of a function.
Useful when same solution is to be applied many times

36.Write a program to accept ten numbers and print average of it.


(Note: Program without array shall be considered).
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,sum=0;
float avg;
clrscr();
printf("Enter numbers:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
sum=sum+a[i];
avg=sum/10;
printf("\n Average =%f", avg);
getch();
}
37.If the value of a number (N) is entered through keyboard. Write a program using recursion to
calculate and display factorial of number (N).
ANS:
#include<stdio.h>
#include<conio.h>
int factorial(int N);
void main()
{
int N,fact;
clrscr();
printf("Enter number:");
scanf("%d",&N);
fact=factorial(N);
printf("\n Factorial is:%d",fact);
getch();
}
int factorial(int N)
{
if(N==1)
return(1);
else
return(N*factorial(N-1));
}

You might also like