SlideShare a Scribd company logo
2
2
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:
b). Write a program to find sum of two integers.
Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:
(2). Write two code examples of if-else.
a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximumn");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximumn");
}
else
{
printf("Second is maximumn");
}
return 0;
}
Most read
10
10
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: n");
scanf("%d", &size);
printf("Enter %d elements of the array: n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %dn", largest);
return 0;
}
Output:
Most read
1
Assignment #4
Subject: Programming Fundamentals
Semester: 1st
Submitted To: Sir. Junaid
Submitted By: Zohaib Zeeshan
Roll No: BSSE-F17-57
Date: 22/01/2018
Department of CS&IT
(BSSE)
University Of Sargodha Mandi Bahauddin Campus
2
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:
b). Write a program to find sum of two integers.
Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:
(2). Write two code examples of if-else.
a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximumn");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximumn");
}
else
{
printf("Second is maximumn");
}
return 0;
}
3
Output:
b). Write a program to check an integer is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer to check even or oddn");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("Evenn");
}
else
{
printf("Oddn");
}
}
Output:
(3). Write two code examples of switch statement.
a). Write a code to check an alphabet is vowel or consonant.
Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Enter an alphabetn");
scanf("%c", &ch);
switch (ch)
{
case'a':
printf("a is voweln");
break;
case 'e':
printf("e is voweln");
break;
4
case 'i':
printf("i is voweln");
break;
case 'o':
printf("o is voweln");
break;
case 'u':
printf("u is woweln");
break;
case 'A':
printf("A is voweln");
break;
case 'E':
printf("E is voweln");
break;
case 'I':
printf("I is voweln");
break;
case 'O':
printf("O is voweln");
break;
case 'U':
printf("U is voweln");
break;
default:
printf("is consonant");
}
return 0;
}
Output:
b). Write a code to check number is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number to check even or oddn");
scanf("%d", &num);
switch(num % 2){
5
case 0:
printf("Number is Evenn");
break;
case 1:
printf("Number is Oddn");
break;
}
}
Output:
(4). Write two code examples of For Loop.
a). C program to find power ofa number using for loop.
Code:
#include<stdio.h>
int main(){
int base,exponent;
int power = 1;
int i;
printf("Enter base: n");
scanf("%d", &base);
printf("Enter exponenet: n");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++){
power=power*base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}
Output:
b). C program to print all even numbers from 1 to n.
Code:
#include<stdio.h>
int main(){
int i,n;
printf("Print all even numbers: n");
scanf("%d", &n);
printf("Even numbers from 1 to %d are n", n);
6
for(i=1; i<=n; i++){
if(i%2 == 0){
printf("%d ", i);
}
}
return 0;
}
Output:
(5). Write two code examples using while loop.
a). C program to print multiplication table ofa number using while loop.
Code:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
while(i <=10)
{
printf("%d * %d = %dn", num, i, (num*i));
i++;
}
return 0;
}
Output:
b). Write a program to genrate star pattern as shown below using while loop.
Code:
#include<stdio.h>
int main()
{
int i,j;
7
i=1;
while(i<=5){
printf("");
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("n");
i++;
}
return 0;
}
Output:
(6).Write two code examples using do while loop.
a). Value of a using do while loop.
Code:
#include <stdio.h>
int main(){
int a = 0;
// do loop execution
do {
printf("value of a: %dn", a);
a++;
}
while( a <= 5 );
return 0;
}
Output:
b). C program to print the table of 5 from 1 to 10.
Code:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %dn",i,5*i);
8
i++;
}
while(i<=10);
return 0;
}
Output:
(7). Write two code examples of Functions.
a). C program to find cube ofa number using function.
Code:
#include <stdio.h>
/* Function declaration */
int cube(int num);
int main(){
int num;
int c;
printf("Enter any number: ");
scanf("%d", &num);
c = cube(num);
printf("Cube of %d is %d", num, c);
return 0;
}
int cube(int num)
{
return (num * num * num);
}
Output:
b). Find factorial of a number using function.
Code:
#include<stdio.h>
int factorial(int);
int main(){
int fact;
int numbr;
printf("Enter a number: ");
scanf("%d",&numbr);
9
fact= factorial(numbr);
printf("Factorial of %d is: %d",numbr,fact);
return 0;
}
int factorial(int n){
int i;
int factorial;
factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
return(factorial);
}
Output:
(8). Write two code examples of Array.
a). Write a program to find repeated elements using array.
Code:
#include<stdio.h>
int main(){
int i,arr[20],j,num;
printf("Enter size of array: ");
scanf("%d",&num);
printf("Enter any %d elements in array: ",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
printf("Repeated elements are: n");
for(i=0; i<num; i++)
{
for(j=i+1;j<num;j++)
{
if(arr[i]==arr[j])
{
printf("%dn",arr[i]);
}
}
}
return 0;
}
Output:
10
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: n");
scanf("%d", &size);
printf("Enter %d elements of the array: n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %dn", largest);
return 0;
}
Output:

More Related Content

What's hot (20)

Practical no 6
Practical no 6Practical no 6
Practical no 6
Kshitija Dalvi
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
Anil Bishnoi
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
C programs
C programsC programs
C programs
Minu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
Anil Bishnoi
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
C programs
C programsC programs
C programs
Minu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 

Similar to Programming fundamentals (20)

C-programs
C-programsC-programs
C-programs
SSGMCE SHEGAON
 
Fuzail_File_C.docx
Fuzail_File_C.docxFuzail_File_C.docx
Fuzail_File_C.docx
SyedFuzail14
 
Najmul
Najmul  Najmul
Najmul
Najmul Ashik
 
C file
C fileC file
C file
simarsimmygrewal
 
Subject:Programming in C - Lab Programmes
Subject:Programming in C - Lab ProgrammesSubject:Programming in C - Lab Programmes
Subject:Programming in C - Lab Programmes
vasukir11
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Hargun
HargunHargun
Hargun
Mukund Trivedi
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
Neil Mathew
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
Srikanth
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
manoj11manu
 
C
CC
C
Mukund Trivedi
 
Basics of C programming - day 2
Basics of C programming - day 2Basics of C programming - day 2
Basics of C programming - day 2
Ajay Khatri
 
C programs
C programsC programs
C programs
Vikram Nandini
 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.com
Akanchha Agrawal
 
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrtasdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
sadman190214
 
Fuzail_File_C.docx
Fuzail_File_C.docxFuzail_File_C.docx
Fuzail_File_C.docx
SyedFuzail14
 
Subject:Programming in C - Lab Programmes
Subject:Programming in C - Lab ProgrammesSubject:Programming in C - Lab Programmes
Subject:Programming in C - Lab Programmes
vasukir11
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
Neil Mathew
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
Srikanth
 
Basics of C programming - day 2
Basics of C programming - day 2Basics of C programming - day 2
Basics of C programming - day 2
Ajay Khatri
 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.com
Akanchha Agrawal
 
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrtasdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
asdasdqwdsadasdsadsadasdqwrrweffscxv wsfrt
sadman190214
 
Ad

More from Zaibi Gondal (8)

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
Zaibi Gondal
 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
Zaibi Gondal
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
Zaibi Gondal
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
Zaibi Gondal
 
Backup data
Backup data Backup data
Backup data
Zaibi Gondal
 
Functional english
Functional englishFunctional english
Functional english
Zaibi Gondal
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
Zaibi Gondal
 
Model Verbs
Model VerbsModel Verbs
Model Verbs
Zaibi Gondal
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
Zaibi Gondal
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
Zaibi Gondal
 
Functional english
Functional englishFunctional english
Functional english
Zaibi Gondal
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
Zaibi Gondal
 
Ad

Recently uploaded (20)

Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
How to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time OffHow to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time Off
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
How to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRMHow to Create a Stage or a Pipeline in Odoo 18 CRM
How to Create a Stage or a Pipeline in Odoo 18 CRM
Celine George
 
How to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time OffHow to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time Off
Celine George
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad LevelLDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDMMIA Reiki Yoga S8 Free Workshop Grad Level
LDM & Mia eStudios
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 

Programming fundamentals

  • 1. 1 Assignment #4 Subject: Programming Fundamentals Semester: 1st Submitted To: Sir. Junaid Submitted By: Zohaib Zeeshan Roll No: BSSE-F17-57 Date: 22/01/2018 Department of CS&IT (BSSE) University Of Sargodha Mandi Bahauddin Campus
  • 2. 2 Programming Fundamentals (1). Printing text on screen: a). Write a simple C program. Code: #include<stdio.h> int main(void) { printf(“Welcome to C!”); return 0; } Output: b). Write a program to find sum of two integers. Code: #include<stdio.h> int main() { int a=5; int b=7; int sum; sum = a+b; printf("The sum is = %d", sum); } Output: (2). Write two code examples of if-else. a). Find maximum between two numbers. Code: #include<stdio.h> int main() { int num1, num2; printf("Enter two integers to find which is maximumn"); scanf("%d%d", &num1, &num2); if(num1 > num2){ printf("First numbers is maximumn"); } else { printf("Second is maximumn"); } return 0; }
  • 3. 3 Output: b). Write a program to check an integer is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter an integer to check even or oddn"); scanf("%d", &num); if(num % 2 == 0) { printf("Evenn"); } else { printf("Oddn"); } } Output: (3). Write two code examples of switch statement. a). Write a code to check an alphabet is vowel or consonant. Code: #include<stdio.h> int main () { char ch; printf("Enter an alphabetn"); scanf("%c", &ch); switch (ch) { case'a': printf("a is voweln"); break; case 'e': printf("e is voweln"); break;
  • 4. 4 case 'i': printf("i is voweln"); break; case 'o': printf("o is voweln"); break; case 'u': printf("u is woweln"); break; case 'A': printf("A is voweln"); break; case 'E': printf("E is voweln"); break; case 'I': printf("I is voweln"); break; case 'O': printf("O is voweln"); break; case 'U': printf("U is voweln"); break; default: printf("is consonant"); } return 0; } Output: b). Write a code to check number is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter a number to check even or oddn"); scanf("%d", &num); switch(num % 2){
  • 5. 5 case 0: printf("Number is Evenn"); break; case 1: printf("Number is Oddn"); break; } } Output: (4). Write two code examples of For Loop. a). C program to find power ofa number using for loop. Code: #include<stdio.h> int main(){ int base,exponent; int power = 1; int i; printf("Enter base: n"); scanf("%d", &base); printf("Enter exponenet: n"); scanf("%d", &exponent); for(i=1; i<=exponent; i++){ power=power*base; } printf("%d ^ %d = %d", base, exponent, power); return 0; } Output: b). C program to print all even numbers from 1 to n. Code: #include<stdio.h> int main(){ int i,n; printf("Print all even numbers: n"); scanf("%d", &n); printf("Even numbers from 1 to %d are n", n);
  • 6. 6 for(i=1; i<=n; i++){ if(i%2 == 0){ printf("%d ", i); } } return 0; } Output: (5). Write two code examples using while loop. a). C program to print multiplication table ofa number using while loop. Code: #include <stdio.h> int main() { int i, num; printf("Enter number to print table: "); scanf("%d", &num); while(i <=10) { printf("%d * %d = %dn", num, i, (num*i)); i++; } return 0; } Output: b). Write a program to genrate star pattern as shown below using while loop. Code: #include<stdio.h> int main() { int i,j;
  • 7. 7 i=1; while(i<=5){ printf(""); j=1; while(j<=i) { printf("*"); j++; } printf("n"); i++; } return 0; } Output: (6).Write two code examples using do while loop. a). Value of a using do while loop. Code: #include <stdio.h> int main(){ int a = 0; // do loop execution do { printf("value of a: %dn", a); a++; } while( a <= 5 ); return 0; } Output: b). C program to print the table of 5 from 1 to 10. Code: #include<stdio.h> int main() { int i=1; do { printf("5 * %d = %dn",i,5*i);
  • 8. 8 i++; } while(i<=10); return 0; } Output: (7). Write two code examples of Functions. a). C program to find cube ofa number using function. Code: #include <stdio.h> /* Function declaration */ int cube(int num); int main(){ int num; int c; printf("Enter any number: "); scanf("%d", &num); c = cube(num); printf("Cube of %d is %d", num, c); return 0; } int cube(int num) { return (num * num * num); } Output: b). Find factorial of a number using function. Code: #include<stdio.h> int factorial(int); int main(){ int fact; int numbr; printf("Enter a number: "); scanf("%d",&numbr);
  • 9. 9 fact= factorial(numbr); printf("Factorial of %d is: %d",numbr,fact); return 0; } int factorial(int n){ int i; int factorial; factorial =1; for(i=1;i<=n;i++) factorial=factorial*i; return(factorial); } Output: (8). Write two code examples of Array. a). Write a program to find repeated elements using array. Code: #include<stdio.h> int main(){ int i,arr[20],j,num; printf("Enter size of array: "); scanf("%d",&num); printf("Enter any %d elements in array: ",num); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } printf("Repeated elements are: n"); for(i=0; i<num; i++) { for(j=i+1;j<num;j++) { if(arr[i]==arr[j]) { printf("%dn",arr[i]); } } } return 0; } Output:
  • 10. 10 b). Find largest element using array. Code: #include <stdio.h> int main() { int array[50], size, i, largest; printf("Enter the size of the array: n"); scanf("%d", &size); printf("Enter %d elements of the array: n", size); for(i=0; i<size; i++){ scanf("%d", &array[i]);} largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("The largest element is : %dn", largest); return 0; } Output: