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

C Lab U

Some programming questions and answers

Uploaded by

udaybehra219
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C Lab U

Some programming questions and answers

Uploaded by

udaybehra219
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.Write a Program to add two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
clrscr();
printf("Enter two numbers=");
scanf("%d%d",&a,&b);
sum=a+b;
printf("%d+%d=%d",a,b,sum);
getch();
}
2.Write a program to check Even or Odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is an even number");
}
else
{
printf("%d is an odd number");
}
getch();
}
3.Write a program to find largest number among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3,largest;
clrscr();
printf("Enter three numbers:");
scanf("%d%d%d",&num1,&num2,&num3);
largest=num1;
if(num2>largest)
largest=num2;
else if(num3>largest)
largest=num3;
printf("%d is the largest number among the given three numbers",largest);
getch();
}
4.Write a Program to calculate Simple Interest when the values of Principal,
Rate and Time are given.
#include<stdio.h>
#include<conio.h>
void main()
{
float principal,rate,time,si;
clrscr();
printf("Enter the principal amount:");
scanf("%f",&principal);
printf("Enter the Rate:");
scanf("%f",&rate);
printf("Enter the Time:");
scanf("%f",&time);
si=principal*rate*time/100;
printf("Simple intrest=%f",si);
getch();
}
5.Write a Program to input temperature into celcius and convert it into
fahrenheit.
#include<stdio.h>
#include<conio.h>
void main()
{
float fohrenhiet,celcius;
clrscr();
printf("Enter the Temprature in celcius:");
scanf("%f",&celcius);
fohrenhiet=celcius*9/5+32;
printf("Temprature in fohranhite=%f",fohrenhiet);
getch();
}
6.Write a Program to determine whether an input Year is Leap Year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year:");
scanf("%d",&year);
if(year%4==0)
printf("%d is a leep year",year);
else
printf("%d is not a leep year",year);
getch();
}
7.Write a Program to display the Table of a number input from
keyboard in the following format: n x 1 = n e.g:5 x 1 = 5 5 x 2 = 10

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d*%d=%d",n,i,n*i);
}
getch();
}
8.Write a program to calculate the Factorial of a number input from Keyboard
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d=%d\n",n,fact);
getch();
}
9.Write a program to accept the two number and display multiplication
without using multiplication sign
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial of %d=%d\n",n,fact);
getch();
}
10.Write a program to accept the number and check number is prime
number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c=0,i;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
getch();
}
11.Write a program to accept the number and check number is perfect
number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,i;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
printf("%d is a perfect number",n);
else
printf("%d is not a perfect number",n);
getch();
}
12.Write a program to accept the number and check number is
palindrome number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t,s=0,i,k;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
t=n;
while(n>0)
{
k=n%10;
s=s*10+k;
n=n/10;
}
if(s==t)
printf("%d is a palindrom number",t);
else
printf("%d is not a palindrome number",t);
getch();
}
13.Write a program to accept the number and check number is
Armstrong number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t,s=0,i,k;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
t=n;
while(n>0)
{
k=n%10;
s=s+k*k*k;
n=n/10;
}
if(s==t)
printf("%d is a armstrong number",t);
else
printf("%d is not a armstrong number",t);
getch();
}
14.Write a program to display palindrome between 100 to 1000
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t,s,i,k;
clrscr();
for(i=100;i<=1000;i++)
{
s=0;
n=i;
while(n!=0)
{
k=n%10;
s=s*10+k;
n=n/10;
}
if(s==i)
printf("%d\n",i);
}
getch();
}
15.Write a program to accept the 5 no in array and display sum
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,s=0;
clrscr();
printf("Enter the array element:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
s=s+a[i];
}
printf("Sum=%d\n",s);
getch();
}
16.Write a program to accept the 5 no in array and display max
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,max;
clrscr();
printf("Enter the array element:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=0;i<5;i++)
{
if(a[i]>max)
max=a[i];
}
printf("max=%d\n",max);
getch();
}
17.Write a program to accept the 3 x 3 in matrix and display left and right
diagonal
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],j,i,max;
clrscr();
printf("Enter the array element:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j||i+j==2)
printf("%d\t",a[i][j]);
else
printf("\t");
}
printf("\n");
}
getch();
}
18.Write a program to accept the 3 x 3 in two diff matrix and display
multiplication in 3rd matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
int matrix1[3][3],matrix2[3][3],result[3][3];
clrscr();
printf("Enter 1st matrex elements:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter 2nd matrix elements:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrix2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=0;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
result[i][j]+=matrix1[i][k]*matrix2[k][j];
}}}
printf("Resultent matrix =\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}
getch();
}
19.Write a program Accept the string and display in character wise
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
int i;
clrscr();
printf("Enter a string:");
scanf("%[^\n]",str);
printf("Character wise display of string:\n");
for(i=0;i<strlen(str);i++)
{
printf("Charater %d:%c\n",i+1,str[i]);
}
getch();
}

20.Write a program Accept the string and count how many characters are
vowel or consonants
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char str[100];
int vc=0,cc=0,c,i;
clrscr();
printf("Enter a string:");
scanf("%[^\n]",str);
for(i=0;i<strlen(str);i++)
{
c=tolower(str[i]);
if(c>='a'&&c<='z')
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
vc++;
}
else
{
cc++;
}
}
}
printf("Input string:%s\n",str);
printf("Vowels Character:%d\n",vc);
printf("Consonants Character:%d\n",cc);
getch();
}

21.Write a program Accept the string and count how many


characters upper case, lower case, digit and extra character
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char str[100];
int uc=0,lc=0,dc=0,ec=0,i;
clrscr();
printf("Enter a string:");
scanf("%[^\n]",str);
for(i=0;i<strlen(str);i++)
{
if(isupper(str[i]))
uc++;
else if(islower(str[i]))
lc++;
else if(isdigit(str[i]))
dc++;
else
ec++;
}
printf("Input string:%s\n",str);
printf("Uppercase Character:%d\n",uc);
printf("Lower Character:%d\n",lc);
printf("Digits:%d\n",dc);
printf("Extra Characters:%d\n",ec);
getch();
}
22.Write a Program in C to swap two numbers using Call by Value and Call
by Address.
CALL BY VALUE:-
#include<stdio.h>
#include<conio.h>
void swap_by_value(int x,int y);
void main()
{
int a,b;
clrscr();
printf("Enter to number :");
scanf("%d",&a);
scanf("%d",&b);
printf("before swapping:a=%d,b=%d\n",a,b);
swap_by_value(a,b);
printf("After swapping:a=%d,b=%d\n",a,b);
getch();
}
void swap_by_value(int x,int y)
{
int t=x;
x=y;
y=t;
printf("Inside swap_by_value:x=%d,y=%d\n",x,y);
}

CALL BY ADDRESS:-
#include<stdio.h>
#include<conio.h>
void swap_by_value(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("Enter to number :");
scanf("%d",&a);
scanf("%d",&b);
printf("before swapping:a=%d,b=%d\n",a,b);
swap_by_value(&a,&b);
printf("After swapping:a=%d,b=%d\n",a,b);
getch();
}
void swap_by_value(int* x,int* y)
{
int t=*x;
*x=*y;
*y=t;
printf("Inside swap_by_value:x=%d,y=%d\n",*x,*y);
}
23.Write a program to calculate the Factorial of a number input from
Keyboard using Recursive method.
#include<stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int n,a;
clrscr();
printf("Enter number:");
scanf("%d",&n);
a=factorial(n);
printf("your sum=%d",a);
getch();
}
int factorial(int n)
{
if(n==1)
return n;
else
return(n*factorial(n-1));
}

24.Write a Program in C to read Name, Roll No, and Percentage of five


Students and display them using Array of Structures.

You might also like