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

C Program Solution

Uploaded by

farhanshafaut
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)
26 views

C Program Solution

Uploaded by

farhanshafaut
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/ 11

Write down a C program to print the day of a week using switch...case.

#include<stdio.h>

int main(){
int n;
printf("Enter any number (1-7) for the day of a week : ");
scanf("%d",&n);

switch(n)
{
case 1:
printf("Friday\n");
break;
case 2:
printf("Saturday\n");
break;
case 3:
printf("Sunday\n");
break;
case 4:
printf("Monday\n");
break;
case 5:
printf("Tuesday\n");
break;
case 6:
printf("Wednesday\n");
break;
case 7:
printf("Thursday\n");
break;

default:
printf("Error Input\n");
}

return 0;
}

Write down a C program to check whether a number is even or odd using switch...case.

#include<stdio.h>
int main(){
int n;
printf("Enter Any Number : ");
scanf("%d",&n);

switch(n%2)
{
case 0:
printf("Number is Even");
break;

default:
printf("Number is Odd");
break;
}
return 0;
}

Write down a C program to create a calculator using switch...case. The calculator will take
two integers and one operator (+, -, *, /, %) as input.

#include<stdio.h>
int main(){
int a,b,r;
float p;
char opt;
printf("Choose an Operator [+,-,*,/,%]: ");
scanf("%c",&opt);
printf("Enter 1st Number : ");
scanf("%d",&a);
printf("Enter 2nd Number : ");
scanf("%d", &b);
switch(opt)
{
case '+' : r =(a + b);
printf("Sum of two number : %d",r);
break;
case '-' :
printf("Subtraction of two number : %d",r);
break;
case '%' : p =(a % b);
printf("Modulo of two number : %.2f",p);
break;
case '*' : r =(a * b);
printf("Multiplication of two number : %d",r);
break;
case '/' : p =(a / b);
printf("Division of two number : %.2f",p);
break;
default:
printf("Wrong Input");
break;
}

return 0;
}

Write down a C program to convert temperature from Fahrenheit to Celsius and Celsius
to Fahrenheit using switch...case.

#include<stdio.h> //c = (n-32) * 5/9


int main(){ //f = n * 9 / 5 +32
int n,p;
char temp;
printf("Choose an Temp : ");
scanf("%c",&temp);
printf("Input Number");
scanf("%d",&n);

switch(temp)
{
case 'c':
p = (n-32) * 5/9;
printf("Temperature is %d degree celsius ",p);
break;
case 'f':
p = n * 9 / 5 +32;
printf("Temperature is %d degree fahrenheit ",p);
break;
default:
printf("Error");
break;
}
return 0;
}

Write a program in C to read 10 numbers from the keyboard and find their
average. (Easy)

#include<stdio.h>
int main(){
int sum = 0,i,n;
float avg;

printf("Input the 10 numbers : \n");


for (i=1;i<=10;i++){
printf("Number-%d :", i);
scanf("%d", &n);
sum = sum + i;

}
avg = sum /10.0;
printf("SUM = %d\n",sum);
printf("AVERAGE = %.2f",avg);
return 0;
}

Write a C program to display the n terms of odd natural numbers and their sum. (Easy)

#include<stdio.h>
int main(){
int n,sum=0,i;
printf("Enter Number : ");
scanf("%d",&n);

for (i=1;i<n;i=i+2)
{
printf("%d\n",i);
sum = sum + i;
}
printf("The Sum of Odd Numbers : %d",sum);
return 0;
}

Write a program in C to display the multiplication table for a given integer.(Easy)

#include<stdio.h>
int main()
{
int n,i,r;
printf("Give An Integer for multiplication table : ");
scanf("%d",&n);

for (i=1;i<=10;i++)
{
r = n * i;
printf("Table : %d * %d = %d\n",n,i,r);
}
return 0;
}

Write a C program to count number of digits in a number using while loop.(Medium)

#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
while (n != 0)
{
n =n / 10; //10 1 0
count++;
}

printf("Number of digits: %d", count);


}

Write a C program to calculate the product of the digits of a number.(Medium)

#include <stdio.h>

int main()
{
int n,rem,prod=1;
printf("ENTER ANY NUMBER : ");
scanf("%d",&n);

while(n!=0)
{
rem = n%10;
prod= prod * rem;
n = n/10; //

}
printf("PRODUCT OF DIGITS : %d",prod);
getch ();
}

Write a C program to check whether a number is palindrome or not.(Medium)

#include<stdio.h>
int main(){
int n,rem,reverse=0,original;
scanf("%d",&n);
original = n;

while(n>0)
{
rem=n%10;
reverse = reverse * 10 + rem;
n = n/10;
}
if (original == reverse)
printf("Palindrome Number");
else
printf("Not Palindrome");
return 0;
}

Write a C program to check whether a number is Armstrong or not.(Medium)

#include <stdio.h>

int main() {
int n,arm=0,r,c;
printf("Enter Any Number : ");
scanf("%d",&n);
c = n;
while(n>0){
r = n % 10;
arm = (r*r*r) + arm;
n = n / 10;

}
if(c == arm)
printf("Armstrong Number");
else
printf("Not");

return 0;
}

Write a program in C to make such a pattern with a number that will repeat a
number in a row. The pattern like : (Hard)

#include<stdio.h>
int main(){
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

printf("%d",i);

}
printf("\n");
}

return 0;
}

Write a program in C to make such a pattern like a square using stars. The
pattern like : (Hard)

#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);

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

printf("*");

}
printf("\n");

return 0;
}
Write a program in C to make such a pattern like Hollow Triangle using stars. The
pattern like : (Hard)

#include <stdio.h>

int main() {
int i, j;

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


{
for(j=1; j<=i; j++)
{
if(j==1 || j==i || i==9)
{
printf("*");
}
else {
printf(" ");
}
}
printf("\n");
}

return 0;
}
Write a program in C to make such a pattern like Hollow Box using stars. The
pattern like : (Hard)

#include <stdio.h>

int main() {
int i, j;

for (i = 1; i <= 7; i++) {


for (j = 1; j <= 7; j++) {
if (i == 1 || i == 7 || j == 1 || j == 7) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}

return 0;
}

Question— It seems like the year of 2013 came only yesterday. Do you know a curious fact?
The year
of 2013 is the first year after the old 1987 with only distinct digits. Now you are suggested
to solve the following problem: given a year number, find the minimum year number which
is strictly larger than the given one and has only distinct digits. Input The single line
contains integer y (1000 ≤ y ≤ 9000) — the year number.
Output: Print a single integer — the minimum year number that is strictly larger than y
and all its
digits are distinct. It is guaranteed that the answer exists.
Sample Input Output:
–input–
1987
–output–
2013
–Input–
2013
–Output–
2014

//----FUNCTION-----
Write a C program to check whether a number is even or odd using function.

#include<stdio.h>
void even_odd(int i);

int main(){
int n;
scanf("%d",&n);

even_odd (n);

return 0;
}
void even_odd(int n)
{

if(n%2 == 0)
printf("Number is Even");
else
printf("Number is odd");
}

Write a C program to check whether a number is Prime or Not Prime using function.

#include<stdio.h>
void isprime()
{
int n,prime=1;
scanf("%d",&n);
for (int i=2;i<n/2;i++)
{
if (n%i==0)
{
prime=0;
break;
}
}
if (prime)
{
printf("Prime Number");
}
else
printf("Not Prime");
}

int main(){
isprime();

return 0;
}

You might also like