Vimp Programs
Vimp Programs
V. IMP QUESTIONS
BRANCH:
CO/IF/AIML
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();
}
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();
}
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();
}
void main()
{
int arr1[100];
int n, i, j, tmp;
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();
}
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();
}
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();
}
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();
}