0% found this document useful (0 votes)
14 views10 pages

Basic C Programs for Lab Practice

The document contains a collection of basic C programming exercises for lab practice, including programs for arithmetic operations, user login, finding averages, and determining prime numbers. Each program is provided with source code and comments explaining its functionality. The exercises cover a range of topics such as loops, conditionals, and string manipulation.
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)
14 views10 pages

Basic C Programs for Lab Practice

The document contains a collection of basic C programming exercises for lab practice, including programs for arithmetic operations, user login, finding averages, and determining prime numbers. Each program is provided with source code and comments explaining its functionality. The exercises cover a range of topics such as loops, conditionals, and string manipulation.
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/ 10

BASIC C PROGRAMS FOR LAB PRACTICE

1) WAP to print the quotient and remainder

#include <stdio.h>

int main() {
int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");


scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);

quotient = dividend / divisor;


remainder = dividend % divisor;

printf("Quotient: %d\n", quotient);


printf("Remainder: %d", remainder);

return 0;

2) WAP to find the average of three numbers

#include <stdio.h>

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

printf("Enter three numbers: ");


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

average = (num1 + num2 + num3) / 3;

printf("The average of the three numbers is: %.2f\n", average);

return 0;
}

3) WAP to make a login screen with user id and password


#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
char studentID[]="Clarance", password[]="123456", id[8], p[6];
int n=1, x, y;

do{
printf("\nStudent_ID:");
scanf("%s", &id);

printf("\nPassword:");
scanf("%s", &p);

x=strcmp(id, studentID);
y=strcmp(p, password);

if(x==0 && y==0){


printf("\nSucessfully Logged In");
}else {
printf("\nWrong Password, try again", 5-n);
getch();
n++;}

if(n>5){
printf("\nAccess Denied");
getch();
}

}while (n<=5);

4) WAP to find the greatest of three numbers

// C program to find the largest number among three number


// using nested if-else
#include <stdio.h>

int main()
{
int c = 10, b = 22, a = 9;

// Finding largest by comparing using relational operators


if (a >= b) {
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else {
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}

return 0;

5) WAP to multiply two numbers


#include <stdio.h>
int main(void)
{
int a = 5;
int b = 7;
int c = a * b; /* c now holds the value 35 */
printf("%d * %d = %d",a,b,c); /* will output "5 * 7 = 35" */
return 0;
}

6) WAP to divide numbers (Practice)


#include <stdio.h>
int main (void)
{
int a = 19 / 2 ; /* a holds value 9 */
int b = 18 / 2 ; /* b holds value 9 */
int c = 255 / 2; /* c holds value 127 */
int d = 44 / 4 ; /* d holds value 11 */
double e = 19 / 2.0 ; /* e holds value 9.5 */
double f = 18.0 / 2 ; /* f holds value 9.0 */
double g = 255 / 2.0; /* g holds value 127.5 */
double h = 45.0 / 4 ; /* h holds value 11.25 */
printf("19 / 2 = %d\n", a); /* Will output "19 / 2 = 9" */
printf("18 / 2 = %d\n", b); /* Will output "18 / 2 = 9" */
printf("255 / 2 = %d\n", c); /* Will output "255 / 2 = 127" */
printf("44 / 4 = %d\n", d); /* Will output "44 / 4 = 11" */
printf("19 / 2.0 = %g\n", e); /* Will output "19 / 2.0 = 9.5" */
printf("18.0 / 2 = %g\n", f); /* Will output "18.0 / 2 = 9" */
printf("255 / 2.0 = %g\n", g); /* Will output "255 / 2.0 = 127.5" */
printf("45.0 / 4 = %g\n", h); /* Will output "45.0 / 4 = 11.25" */
return 0;
}

7) MODULO Operator
#include <stdio.h>
int main (void)
{
int a = 25 % 2; /* a holds value 1 */
int b = 24 % 2; /* b holds value 0 */
int c = 155 % 5; /* c holds value 0 */
int d = 49 % 25; /* d holds value 24 */
printf("25 % 2 = %d\n", a); /* Will output "25 % 2 = 1" */
printf("24 % 2 = %d\n", b); /* Will output "24 % 2 = 0" */
printf("155 % 5 = %d\n", c); /* Will output "155 % 5 = 0" */
printf("49 % 25 = %d\n", d); /* Will output "49 % 25 = 24" */
return 0;
}
8) INCREMENT/ DECREMENT OPERATORS
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 4;
int c = 1;
int d = 4;
a++;
printf("a = %d\n",a); /* Will output "a = 2" */
b--;
printf("b = %d\n",b); /* Will output "b = 3" */
if (++c > 1) { /* c is incremented by 1 before being compared in the condition */
printf("This will print\n"); /* This is printed */
} else {
printf("This will never print\n"); /* This is not printed */
}
if (d-- < 4) { /* d is decremented after being compared */
printf("This will never print\n"); /* This is not printed */
} else {
printf("This will print\n"); /* This is printed */
}
}

9) WAP to find the HCF of two numbers


#include <stdio.h>
int main()
{
int num1, num2, i, gcd;

printf("Enter two integers: ");


//Storing user input into num1 and num2
scanf("%d %d", &num1, &num2);

for(i=1; i <= num1 && i <= num2; ++i)


{
// Checks if the current value of i is
// factor of both the integers num1 & num2
if(num1%i==0 && num2%i==0)
gcd = i;
}

printf("GCD of input numbers %d and %d is: %d", num1, num2, gcd);

return 0;
}

10) WAP to find the LCM of two numbers


#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);

// greater number between num1 and num2 is stored in max


max = (num1 > num2) ? num1 : num2;

while (1) {
if (max % num1 == 0 && max % num2 == 0) {
printf("LCM of input numbers %d and %d is %d.", num1, num2, max);
break;
}
++max;
}
return 0;
}

11) WAP to check whether a number is prime or not


#include <stdio.h>

int main() {

//Here flag value 0 means prime number and


//1 means non-prime number
int num, i, flag = 0;
printf("Enter a number to check whether prime or not: ");
scanf("%d", &num);

// 0 and 1 are not the prime numbers so setting


// the flag to 1 if entered number is 0 or 1
if (num == 0 || num == 1)
flag = 1;

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

// if num is divisible by i, then num is not prime


// change the flag value to 1
if (num % i == 0) {
flag = 1;
break;
}
}

// If flag is 0 it means the number is prime


if (flag == 0)
printf("Entered number %d is a prime number.", num);
else
printf("Entered number %d is not a prime number.", num);

return 0;
}
12) WAP to count the number of digits in an integer
#include <stdio.h>
int main() {
long number, temp;
int count = 0;
printf("Enter an integer: ");
scanf("%ld", &number);

//copying the number in a temporary variable to perform


//calculations on it.
temp = number;

//Here we are counting the digits of the input


//number. At every loop iteration, we are increasing
//the counter by 1 and removing the last digit from the
//number by dividing it by 10. This goes on until number
//becomes 0.
do {
temp /= 10;
++count;
} while (temp != 0);

printf("Number of digits in the number %ld is: %d", number,count);


}

13) WAP to print prime number from 1 to N


#include <stdio.h>

int checkPrimeNum(int num)


{
// Any number less than 2 is not a prime number
if(num < 2){
return 0;
}
//if greater than 2 then check for prime number
else{
int temp = num/2;
for(int i = 2; i <=temp; i++)
{
if(num % i == 0)
{
//return 0 if number is not prime
return 0;
}
}
}
// return 1 if number is prime
return 1;
}

int main()
{
// change this number as per the requirement. Here, we are printing
// the prime numbers from 1 to 100 so the n is 100. If you
// want to display first 50 prime numbers then change it to 50
int n = 100;

printf("Prime numbers from 1 to %d: ", n);


for(int i=1; i <= n; i++){
if(checkPrimeNum(i))
printf("%d ",i);
}
return 0;
}

14) WAP to find whether a number is divisible by both 3 and 5


#include <stdio.h>

int main() {
int num;

// Input number from user


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

// Check if the number is divisible by both 3 and 5


if (num % 3 == 0 && num % 5 == 0) {
printf("%d is divisible by both 3 and 5.\n", num);
} else {
printf("%d is not divisible by both 3 and 5.\n", num);
}

return 0;
}

15) WAP to find the sum of the digits of a number


#include <stdio.h>

int main() {
int number, sum = 0, digit;

// Input the number


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

// Loop to add all the digits of number to sum


while (number != 0) {
digit = number % 10; // find the last digit
sum += digit; // Add this digit to sum
number /= 10; // Remove the last digit
}

// Display the sum


printf("Sum of digits: %d\n", sum);
return 0;
}

16) WAP to print all the factors of a number


#include <stdio.h>

int main() {
int number, i;

// Input the number


printf("Enter a positive integer: ");
scanf("%d", &number);

// Displaying factors
printf("Factors of %d are: ", number);
for (i = 1; i <= number; ++i) {
// Check if 'i' is a factor of 'number'
if (number % i == 0) {
// If 'i' is a factor, print it
printf("%d ", i);
}
}

return 0;
}

17) WAP to check whether a number is odd or even

/* Program to check whether the input integer number


* is even or odd using the modulus operator (%)
*/
#include<stdio.h>
int main()
{
// This variable is to store the input number
int num;

printf("Enter an integer: ");


scanf("%d",&num);

// Modulus (%) returns remainder


if ( num%2 == 0 )
printf("%d is an even number", num);
else
printf("%d is an odd number", num);

return 0;
}

18) WAP to check whether a letter is a vowel or consonant


#include <stdio.h>
int main()
{
char ch;
bool isVowel = false;

printf("Enter an alphabet: ");


scanf("%c",&ch);

if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
isVowel = true;

}
if (isVowel == true)
printf("%c is a Vowel", ch);
else
printf("%c is a Consonant", ch);
return 0;
}

19) WAP to generate the multiplication table of a number


#include <stdio.h>
int main() {
int number, i;
printf("Enter an integer: ");
scanf("%d", &number);
printf("Multiplication table of %d: \n", number);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", number, i, number * i);
}
return 0;
}

20) WAP to convert a number from decimal to binary


#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
int decimalnum = 0, temp = 0, remainder;
while (binarynum!=0)
{
remainder = binarynum % 10;
binarynum = binarynum / 10;
decimalnum = decimalnum + remainder*pow(2,temp);
temp++;
}
return decimalnum;
}

int main()
{
long binarynum;
printf("Enter a binary number: ");
scanf("%ld", &binarynum);

printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));


return 0;
}

21) WAP to convert a string from lower case to upper case


/* C Program to convert Lower case
* String to Upper case.
* Written by: Chaitanya
*/

#include<stdio.h>
#include<string.h>
int main(){
char str[25];
int i;

printf("Enter the string:");


scanf("%s",str);

for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nUpper Case String is: %s",str);
return 0;
}

You might also like