0% found this document useful (0 votes)
6 views4 pages

C Programming Lab Exercises and Solutions

This document lists several programs that have been done in a lab, including programs to: calculate the sum and product of three numbers; swap two variables; check if a year is a leap year; determine if a number is even or odd; convert case of characters; use increment and decrement operators; find the greatest of three numbers; print patterns using loops; check if a number is prime; calculate factorials using loops; find the sum of natural numbers using loops; use a switch statement to print numbers; calculate a geometric series sum; print squares, cubes, and fourth powers; print tables using loops; swap numbers using functions; find largest of three numbers using functions; use arrays to store and display numbers; perform linear search;

Uploaded by

Anubhav Vij
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

C Programming Lab Exercises and Solutions

This document lists several programs that have been done in a lab, including programs to: calculate the sum and product of three numbers; swap two variables; check if a year is a leap year; determine if a number is even or odd; convert case of characters; use increment and decrement operators; find the greatest of three numbers; print patterns using loops; check if a number is prime; calculate factorials using loops; find the sum of natural numbers using loops; use a switch statement to print numbers; calculate a geometric series sum; print squares, cubes, and fourth powers; print tables using loops; swap numbers using functions; find largest of three numbers using functions; use arrays to store and display numbers; perform linear search;

Uploaded by

Anubhav Vij
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

These Programs have been done in the lab:

**Program to input three numbers. Find their sum and Product


**Program For Swapping of two variables
** To Check If Input Year Is Leap Year Or Not (using If else)
*Program To Check If The Number Input By User Is Even Or Odd
*Program to convert lower case character into upper case character
*Program to convert lower case character into upper case character2
*Program to convert lower case character into upper case character3
*To Use Pre Increment and Decrement Operators
*To Use Post Increment and Decrement Operators
**Program To Find The Greatest Of The Three Numbers (using IF Else and Nested IF)
*Program to get a formatted pattern
**Program to print formatted output
**To Find If The Number Is Prime Or Not
**To Find The Factorial Of A Natural Numbers Using For Loop
*To Find The Factorial Of A Natural Numbers Using While
*To Find The Sum Of N Natural Numbers Using do while
*To Find The Sum Of N Natural Numbers Using for For Loop
*To Find The Sum Of N Natural Numbers using while
*To Print 5 Numbers Using Switch
**To Find Geometric Sum of series
1. To Print A Pattern Of Asterisks
2. To Print A Pattern Of Numbers
***To Print The FIBONACCI series up to n terms
*To Print The Number Of Days In A Particular Month using Switch
*Program to make Calculator using switch Case
3. To Print The Square, Cube & Fourth Power Of An Integer

**To Print The Table Of A Number using For Loop


****To Swap Two Numbers Using Functions By Reference
4. **To Swap Two Numbers Using Functions
**Find the largest of the three numbers using functions

**(*Program of an array to accept a list of numbers into 1-D array, find their sum, average and
display them.

To search for a number in a list of numbers using linear search .

***To sort a list of numbers using bubbl


// WAP TO CHECK WHETHER A NUMBER IS PRIME OR NOT

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

void main()
{
int n,i,p;
p=0;
clrscr();

printf("enter the number \n");


scanf("%d",&n);

for(i=2;i<n/2;i++)
{if(n%i==0)
p++;
}

if(p==0)
printf("%d is prime ",n);

else if(p!=0)
printf("%d is not prime ",n);
getch();
}
// Created on: 6th Apr'11
// Objective: To Print A Pattern Of Asterisks '*'
/* Comment: In this program we use 'for' to print
a 5 by 5 pattern of ASTERISK*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows,cols;
clrscr();

printf("\t\t PROGRAM TO PRINT A PATTERN USING '*'");


printf("\n\nENTER THE NUMBER OF ROWS & COLUMNS:: ");
scanf("%d%d",&rows,&cols);
for(i=0;i<rows;i++)
{
printf("\n\t\t\t\t");
for(j=0;j<cols;j++)
{
printf("%c",'*');
}
}
getch();
}
// Author: SAgar bansal
// Created on: 6th Apr'11
// Objective: To Print A Pattern Of Numbers
/* Comment: In this program we use 'for' to print
a pattern of numbers as:
1
22
333
4444
55555 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,rows;
clrscr();

printf("\t\t PROGRAM TO PRINT A PATTERN OF NUMBERS");


printf("\n\nENTER THE NUMBER OF ROWS:: ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
printf("\n\t\t\t");
for(j=1;j<=i;j++)
{
printf("%d",i);
}
}
getch();
}

Common questions

Powered by AI

The pre-increment operator (++variable) increases the variable's value before it is used in an expression, while the post-increment operator (variable++) uses the variable's current value in the expression and then increments it. For example, if a variable x is 5, then using ++x in an expression will make it 6 before use, whereas x++ will use 5 and then update x to 6 afterward .

To find the greatest of three numbers a, b, and c using if-else statements, compare each number: First, check if a is greater than or equal to both b and c. If true, a is the greatest. If not, check if b is greater than or equal to both a and c. If true, b is the greatest. Otherwise, c must be the greatest. This approach uses logical comparison to evaluate the largest number incrementally .

To calculate the factorial of a number using a while loop, initialize the result variable to 1 and a counter variable to the number itself. Then, loop while the counter is greater than 0, multiplying the result by the counter and decrementing the counter each iteration. This multiplication continues until the counter reduces to zero, leaving the result variable containing the factorial of the original number .

To calculate the geometric sum of a series programmatically, define the first term, the common ratio, and the number of terms. Utilize the formula: sum = a(1-r^n)/(1-r) when r ≠ 1, where 'a' is the first term and 'r' the common ratio. Consider edge cases like r=1 where the sum simplifies to a*n (n terms of 'a'). Implement checks for invalid input or where r approaches 1 to ensure precision and avoid division errors .

Using a switch case structure in a calculator program allows for clear organization of distinct operations such as addition, subtraction, multiplication, and division. Each operation is a case, making it easy to navigate and expand. The switch case improves readability and maintainability compared to multiple if-else statements, particularly when there are many operations, by separating logic into discrete sections .

To generate a Fibonacci sequence up to n terms iteratively, initialize two variables with the first two numbers of the sequence (e.g., 0 and 1). Loop from 2 to n, maintaining a third variable to hold the sum of the previous two. After printing each term, shift the two main variables forward: the current second number becomes the first, and the sum becomes the second, continuing this pattern until n terms are generated .

Nested if statements in a prime number checking program serve to validate multiple conditions in an orderly manner. The outer if statement usually checks initial criteria, such as whether the number is less than 2, while inner if statements iteratively check divisibility up to the half of the number. If any number divides the candidate number without a remainder, it’s not prime. The nesting allows compartmentalization of checks, ensuring efficiency and clarity in logical flow .

To convert a lowercase character to its uppercase equivalent, use the ASCII value of the character. Subtract 32 from the ASCII value of the lowercase character (since lowercase 'a' is 97 in ASCII and uppercase 'A' is 65, differing by 32) and convert this back to a character type. This approach is straightforward and efficiently leverages the fixed distance in the ASCII table between lowercase and uppercase letters .

To determine if a year is a leap year using if-else statements, check if the year is divisible by 4. If it is, then further check if it is not divisible by 100 unless it is also divisible by 400. These conditions ensure that years like 2000 are leap years, while years like 1900 are not .

Swapping two numbers using functions by reference allows the original variables to be modified directly in the calling function, maintaining changes outside the scope of the called function. This avoids the need for additional memory to return values and allows operations on the original data, making the swap more efficient and applicable in places where direct access to original variables is necessary .

You might also like