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

C Programming Questions

The document contains 15 questions asking to write C programs to perform various tasks like checking if a number is prime, finding the greatest of three numbers, converting between number bases, etc. For each question, the student has provided the code for the C program to achieve the task.

Uploaded by

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

C Programming Questions

The document contains 15 questions asking to write C programs to perform various tasks like checking if a number is prime, finding the greatest of three numbers, converting between number bases, etc. For each question, the student has provided the code for the C program to achieve the task.

Uploaded by

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

LAB - COPY

Name: Kheraj Raj Basumatary


Roll No. 202002031019
Branch: ECE
Sub Code: UCSE271
DATE: 20th Aug, 2021
Q.1 : Write a program that will print an output “Congratulation you PASSED!” if the student
grade is more than or equal to 60.
#include <stdio.h>
int main(){
int grade;
printf (" Enter the grade you got out of 100: ");
scanf ("%d", &grade);

if (grade >= 60){


printf ("\n CONGRATULATIONS YOU'VE PASSED THE TEST!!");
}
else if (grade <= 60){
printf ("\n Sorry, you're grade did not met the passing criteria"
);
}
else if (grade > 100)
{
printf ("\n Enter valid number!");
}
else {
printf ("\n Invalid request!");
}

return 0;
}
S

Q.2 : Write a program that will ask for a price. If the price is greater than 1000, compute a
10% discount from the original price and display the computed price.
#include <stdio.h>

int main(){
int price, discount, new_price;
printf (" Enter the price: ");
scanf ("%d", &price);

if (price > 1000){


printf (" You'll have flat discount of 10 percent");
discount = price * 10/100;
new_price = price - discount;
printf ("\n You'll now have to pay only %d !", new_price);
}
else if (price < 1000){
printf (" Sorry, there will be no discounts");
printf ("\n The price is %d", price);
}
else {
printf (" Invalid req!");
}

return 0;
}
Q.3 : Write a program to check if the entered calendar year is leap year or not.

#include <stdio.h>
int main()
{
int year;

printf(" Enter a year: ");


scanf("%d", &year);

if ((year % 400) == 0)
printf("\n %d is a leap year \n", year);

else if ((year % 100) == 0)


printf("\n %d is a not leap year \n", year);

else if ((year % 4) == 0)
printf("\n %d is a leap year \n", year);

else
printf("\n %d is not a leap year \n", year);
return 0;
}
Q.4 : Write a program to determine if it is a Negative or Positive integer.
#include <stdio.h>
int main()
{
int num;

printf(" Enter the number: ");


scanf("%d", &num);

if (num >= 0){


printf("%d is a positive number \n", num);}

else if (num == 0){


printf(" 0 is neither positive or negative");}

else{
printf("%d is a negative number \n", num);}

return 0;
}
Q.5 : Write a program to find the greatest number among three input numbers.
#include <stdio.h>

int main()
{
int num1, num2, num3;

printf(" Enter the three numbers: \n");


scanf(" %d %d %d", &num1, &num2, &num3);

if (num1 > num2)


{
if (num1 > num3)
{
printf(" %d is the greatest among three \n", num1);
}
else
{
printf(" %d is the greatest among three \n", num3);
}
}
else if (num2 > num3)
printf(" %d is the greatest among three \n", num2);
else
printf(" %d is the greatest among three \n", num3);

return 0;
}
Q.6 : Enter any number between 1-7 and map this to week day.
#include <stdio.h>
int main()
{
int week;

printf("Enter week number (1-7): ");


scanf("%d", &week);
if(week == 1)
{
printf("Monday");
}
else if(week == 2)
{
printf("Tuesday");
}
else if(week == 3)
{
printf("Wednesday");
}
else if(week == 4)
{
printf("Thursday");
}
else if(week == 5)
{
printf("Friday");
}
else if(week == 6)
{
printf("Saturday");
}
else if(week == 7)
{
printf("Sunday");
}
else
{
printf("Invalid Input! Please enter week number between 1-7.");
}
return 0;
}
Q.7 : Program to check if the entered letter is vowel or consonant.
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c =


= 'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c =


= 'U');

if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Q.8 : Program to print the following pattern.

a) *****
*****
*****
*****
*****
#include <stdio.h>
int main()
{
int nrows, count1, count2;
printf("Enter the number of rows");
scanf("%d",&nrows);
for(count1 = 0; count1 < nrows; count1++)
{
for(count2 = 0; count2 < nrows; count2++)
{
printf("*");
}
printf("\n");
}

return 0;
}
b) *
**
***
****
*****
#include<stdio.h>

void astrsk(int dummy_num){


int count_1 , count_2;
printf("\n Here's the Pattern !!\n");

for(count_1 = 1; count_1<=dummy_num ; count_1++){


printf("\n");

for( count_2 = 1 ; count_2<=count_1 ; count_2++){


printf("* ");
} } }

int main(){
int num;

printf(" Enter the number of rows, * to draw: ");


scanf("%d" , &num);

astrsk(num);
return 0;
}
c) 1
121
12321
1234321
#include<stdio.h>

void pattern(int x){


int count_1 , count_2 , k , forwd_count;

for(count_1 = 1 ; count_1<=x ; count_1++){

for(count_2 = 1 ; count_2 <= x-count_1; count_2++){


printf(" ");}

for(k = 1; k <= count_1 ; k++){


printf(" %d ", k);}

for(forwd_count = count_1-1; forwd_count>= 1; forwd_count--){


printf(" %d " , forwd_count);}

printf("\n");
}}

int main(){
int num ;
printf(" Enter the number limit: ");
scanf("%d" ,&num);

pattern(num);
printf("\n That's how it look...");
return 0;
}
Q.9 : Write a program to print the reverse of a number.
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf(" Enter an integer: ");
scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}

printf("\n Reversed number = %d", rev);

return 0;
}
Q.10 : Program to find if a number is palindrome.
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, original;
printf(" Enter an integer: ");
scanf("%d", &num);
original = num;

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

if (original == reversed){
printf(" %d is a palindrome.", original);}
else{
printf(" %d is not a palindrome.", original);}

return 0;
}
Q.11: Program to find if a number is prime number or composite.
#include <stdio.h>
int main() {

int num, count, check = 0;


printf(" Enter a natural number: ");
scanf("%d", &num);

for (count = 2; count <= num / 2; ++count) {


if (num % count == 0) {
check = 1;
break;}
}

if (num == 1) {
printf("\n 1 is neither prime nor composite.");
}
else if (num <0){
printf ("\n %d is not a natural number", num);
}
else {
if (check == 0)
printf("\n %d is a prime number.", num);
else
printf("\n %d is not a prime number.", num);
}

return 0;
}
Q.12: Write a program to find the factorial of a number.

#include <stdio.h>

int main(){
int i, n, facto = 1;

printf(" Enter a number to calculate its factorial: ");


scanf("%d", &n);
for (i = 1; i <= n; i++)
facto = facto * i;

printf("\n So the Factorial of %d is %d", n, facto);

return 0;
}
Q.13: Write a program to calculate xn.
#include<stdio.h>
#include<math.h>

int main(){
float num;
int power;

printf (" Enter the number: ");


scanf ("%f", &num);

printf (" Enter the exponent: ");


scanf ("%d", &power);

float expo = pow(num,power);

printf("\n So %f to the power %d is: %f",num, power, expo);


return 0;
}
Q.14: Write a program to convert Decimal to Binary.
#include <stdio.h>
int main()
{
int a[10], number, count_1, count_2;
printf(" Enter the Number to Convert : ");
scanf("%d", &number);

for(count_1 = 0; number > 0; count_1++){


a[count_1] = number % 2;
number = number / 2;
}

printf("\n Binary Number of a Given Number = ");


for(count_2 = count_1 - 1; count_2 >= 0; count_2--) {
printf(" %d ", a[count_2]);
}
return 0;
}
Q.15: Write a program to convert Decimal to Octal.
#include <stdio.h>

int main()
{
long deci_num, remainder, quotient;
int oct_num[100], count_1 = 1, count_2;

printf(" Enter the decimal number: ");


scanf("%ld", &deci_num);
quotient = deci_num;
while (quotient != 0){
oct_num[count_1++] = quotient % 8;
quotient = quotient / 8;
}
printf("\n Octal number of decimal no %d: ", deci_num);
for (count_2 = count_1 - 1; count_2 > 0; count_2--)
printf("%d", oct_num[count_2]);
return 0;
}
Q.16: Write a program to convert Binary to Decimal.
#include<stdio.h>
#include<math.h>

void deci_num(int);
void deci_num(int a){
int deci_num = 0 , rem , i = 0 ;
while(a!=0){
rem = a % 10;
deci_num += rem * pow(2,i);
a /= 10;
i++;
}
printf("\n Decimal No. is: %d", deci_num);
}
int main(){
int bin_num;
printf(" Enter the Binary number: ");
scanf("%d", &bin_num);
deci_num( bin_num);
return 0;
}
Q.17: Write a program to print the sum of series.
a) 1 + ½ + 1/3+ …….1/n
#include<stdio.h>
int main () {
int count , i ;
float sum_series;
printf(" Enter the number: ");
scanf("%d", &count );
for(i = 1; I <= count ; i++)
{
sum_series += (float)1/i;
}
printf("\n The Sum of Series 1 + 1/2 + 1/3+ .... 1/%d is %.2f", count
,sum_series);

return 0;
}
b) 1/1^2 + ½^2 + …..1/n^2
#include<stdio.h>
#include<math.h>

int main(){
int count_1, count_2;
float sum_series;

printf(" Enter the number: ");


scanf("%d", &count_1);

for( count_2 = 1 ; count_2 <=count_1 ;count_2++)


{
sum_series += 1/pow(count_2 ,2);
}
printf("\n The sum of 1 + 1/2^2 + 1/3^2 +..... 1/%d^2 is %.2f",count
_1, sum_series);

return 0;
}
c)½ + 2/3 + n/(n+1)

#include<stdio.h>
int main(){
int num , count_2, count_1;
float iter, sum_series = 0 ;

printf(" Enter the number: ");


scanf("%d", &num);

for(count_1 = 1 ; count_1<=num ; count_1++){


iter = (float) count_1/(count_1+1);
sum_series += iter;
}
count_2 = num + 1;
printf("\n The Sum of 1/2 + 2/3+.....%d/%d is %.2f", num ,count_2,
sum_series);

return 0;
}
Q.18: Write a program to determine if a number is an Armstrong number.
int main(){
int num , sum = 0, arit, num_clone;

printf(" Enter the number: ");


scanf("%d", &num);
num_clone = num;

while(num_clone!=0){
arit = num_clone%10;
sum += (arit * arit * arit);
num_clone /= 10;
}

if(num == sum){
printf("\n %d is an Armstrong Number. ", num);
}
else{
printf("\n %d is not an Armstrong number.", num);
}
return 0;
}
Q.19: Write a program to swap two numbers.
a) using call by value
#include <stdio.h>
void swap(int, int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
return 0;
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
b) by using call by reference
#include <stdio.h>

swap (int *x, int *y)


{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a, b;
printf("\n Enter value of a & b: \n");
scanf("%d %d", &a, &b);
printf("\n Before Swapping: \n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);

printf("\n After Swapping: \n");


printf("\na = %d\n\nb = %d", a, b);

return 0;
}
Q.20: Write a program to find the factorial of a number using recursive function.
#include<stdio.h>

long int facto(int n){


if (n>=1)
return n * facto(n-1);
else
return 1;
}
int main() {
int num;
printf(" Enter a positive integer: ");
scanf("%d",&num);
printf(" Factorial of %d = %ld", num, facto(num));
return 0;
}
Q.21: Write a program to find the smallest number among n numbers in the array.
#include<stdio.h>

int main() {
int a[30], i, num, smallest;

printf("\nEnter no of elements :");


scanf("%d", &num);
printf (" Enter the %d numbers: \n", num);

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


scanf ("%d", &a[i]);

smallest = a[0];

for (i = 0; i < num; i++) {


if (a[i] < smallest) {
smallest = a[i];
}
}

// Print out the Result


printf("\nSmallest Element : %d", smallest);

return (0);
}
Q.22: Write a program to find the largest number among n numbers in the array.
#include<stdio.h>

int main() {
int a[30], i, num, largest;

printf("\n Enter no of elements: ");


scanf("%d", &num);

printf("\n Enter the %d elements: ", num);


for (i = 0; i < num; i++)
scanf("%d", &a[i]);

largest = a[0];

for (i = 0; i < num; i++) {


if (a[i] > largest) {
largest = a[i];
}
}

printf("\nLargest Element : %d", largest);

return (0);
}
Q.23: Write a program to find whether the array contains duplicate element in a given
array.
#include <stdio.h>

int main()
{

int arr[] = {1, 2, 5, 4, 6, 7, 8, 8, 10};

int length = sizeof(arr)/sizeof(arr[0]);

printf("Duplicate elements in given array: \n");

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


for(int j = i + 1; j < length; j++) {
if(arr[i] == arr[j])
printf("%d\n", arr[j]);
}
}
return 0;
}
Q.24: Write a program to find whether the array contains duplicate element in a given
array.
#include <stdio.h>
struct complex_numbers
{
int real_num;
int imagn_num;
};

int main()
{

struct complex_numbers c1, c2, sum, diff;

printf(" Enter the 1st complex number, real part: ");


scanf ("%d", &c1.real_num);
printf(" Enter the 1st complex number, imaginary part: ");
scanf ("%d", &c1.imagn_num);
printf(" Enter the 2nd complex number, real part: ");
scanf ("%d", &c2.real_num);
printf(" Enter the 2nd complex number, imaginary part: ");
scanf ("%d", &c2.imagn_num);

sum.real_num = c1.real_num + c2.real_num;


sum.imagn_num = c1.imagn_num + c2.imagn_num;

diff.real_num = c1.real_num - c2.real_num;


diff.imagn_num = c1.imagn_num - c2.imagn_num;

printf ("\n The Sum is: %d + %di", sum.real_num, sum.imagn_num);


printf ("\n The Diff is: %d + %di", diff.real_num, diff.imagn_num);

return 0;
}
Q.25: Write a program to store data of two student using structure which include roll no,
cgpa and address.

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float cgpa;
char address[50];

} s[10];

int main() {
int i;
printf(" Enter information of students: \n");

for (i = 0; i < 5; ++i) {


s[i].roll = i + 1;
printf("\n For roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf(" Enter CGPA: ");
scanf("%f", &s[i].cgpa);
printf(" Enter the address: ");
scanf("%s", &s[i].address);
}
printf(" Displaying Information: \n\n");

for (i = 0; i < 5; ++i) {


printf("\n Roll number: %d\n", i + 1);
printf(" First name: ");
puts(s[i].firstName);
printf(" Marks: %.1f", s[i].cgpa);
printf(" Address: ");
puts(s[i].address);

printf("\n");
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy